mirror of
https://github.com/hanzoai/sign.git
synced 2026-10-21 01:21:51 +00:00
@noble/ciphers 2 moved managedNonce from webcrypto.js to utils.js. The import left behind named an export that no longer exists, so crypto.ts — and everything that stores or reads an encrypted secret through it — could not load: in ESM a missing named export fails the module at link time. crypto.test.ts round-trips a secret, checks each write draws a new nonce and that another key is refused, and decrypts a secret written by @noble/ciphers 0.6.0 through this module's old code, since those are what the database holds. Against the old import all four fail. Co-authored-by: Hanzo Dev <[email protected]>
34 lines
954 B
TypeScript
34 lines
954 B
TypeScript
import { xchacha20poly1305 } from '@noble/ciphers/chacha.js';
|
|
import { bytesToHex, hexToBytes, managedNonce, utf8ToBytes } from '@noble/ciphers/utils.js';
|
|
import { sha256 } from '@noble/hashes/sha2.js';
|
|
|
|
export type SymmetricEncryptOptions = {
|
|
key: string;
|
|
data: string;
|
|
};
|
|
|
|
export const symmetricEncrypt = ({ key, data }: SymmetricEncryptOptions) => {
|
|
const keyAsBytes = sha256(key);
|
|
const dataAsBytes = utf8ToBytes(data);
|
|
|
|
const chacha = managedNonce(xchacha20poly1305)(keyAsBytes); // manages nonces for you
|
|
|
|
return bytesToHex(chacha.encrypt(dataAsBytes));
|
|
};
|
|
|
|
export type SymmetricDecryptOptions = {
|
|
key: string;
|
|
data: string;
|
|
};
|
|
|
|
export const symmetricDecrypt = ({ key, data }: SymmetricDecryptOptions) => {
|
|
const keyAsBytes = sha256(key);
|
|
const dataAsBytes = hexToBytes(data);
|
|
|
|
const chacha = managedNonce(xchacha20poly1305)(keyAsBytes); // manages nonces for you
|
|
|
|
return chacha.decrypt(dataAsBytes);
|
|
};
|
|
|
|
export { sha256 };
|