mirror of
https://github.com/hanzoai/sign.git
synced 2026-10-21 01:21:51 +00:00
The product is branded Hanzo eSign (esign.hanzo.ai, repo hanzoai/esign). Rename all workspace packages @hanzo/sign-* → @hanzo/esign-* (1052 files) and align docker/Dockerfile turbo --scope/--filter to the actual remix package name @hanzo/esign (was a pre-existing mismatch with -remix that broke the build). Protected deps (@hanzo/insights, @hanzo/logo) untouched.
38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { SIGN_ENCRYPTION_SECONDARY_KEY } from '@hanzo/esign-lib/constants/crypto';
|
|
import { ZEncryptedDataSchema } from '@hanzo/esign-lib/server-only/crypto/encrypt';
|
|
import { symmetricDecrypt } from '@hanzo/esign-lib/universal/crypto';
|
|
|
|
/**
|
|
* Decrypt the passed in data. This uses the secondary encrypt key for miscellaneous data.
|
|
*
|
|
* @param encryptedData The data encrypted with the `encryptSecondaryData` function.
|
|
* @returns The decrypted value, or `null` if the data is invalid or expired.
|
|
*/
|
|
export const decryptSecondaryData = (encryptedData: string): string | null => {
|
|
if (!SIGN_ENCRYPTION_SECONDARY_KEY) {
|
|
throw new Error('Missing encryption key');
|
|
}
|
|
|
|
try {
|
|
const decryptedBufferValue = symmetricDecrypt({
|
|
key: SIGN_ENCRYPTION_SECONDARY_KEY,
|
|
data: encryptedData,
|
|
});
|
|
|
|
const decryptedValue = Buffer.from(decryptedBufferValue).toString('utf-8');
|
|
const result = ZEncryptedDataSchema.safeParse(JSON.parse(decryptedValue));
|
|
|
|
if (!result.success) {
|
|
return null;
|
|
}
|
|
|
|
if (result.data.expiresAt !== undefined && result.data.expiresAt < Date.now()) {
|
|
return null;
|
|
}
|
|
|
|
return result.data.data;
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|