Files
rss-nostr-lambda/node_modules/@noble/ciphers/esm/webcrypto/aes.js
T

42 lines
1.7 KiB
JavaScript

import { ensureBytes } from '../utils.js';
import { getWebcryptoSubtle } from './utils.js';
function generate(algo, length) {
const keyLength = length / 8;
const keyParams = { name: algo, length };
const cryptParams = { name: algo };
// const params: Record<string, any> = ({ e: algo, i: { name: algo, length } });
return (key, nonce) => {
ensureBytes(key, keyLength);
if (algo === 'AES-CTR') {
cryptParams.counter = nonce;
cryptParams.length = 64;
}
else {
cryptParams.iv = nonce;
}
return {
keyLength,
async encrypt(plaintext) {
ensureBytes(plaintext);
const cr = getWebcryptoSubtle();
const iKey = await cr.importKey('raw', key, keyParams, true, ['encrypt']);
const cipher = await cr.encrypt(cryptParams, iKey, plaintext);
return new Uint8Array(cipher);
},
async decrypt(ciphertext) {
ensureBytes(ciphertext);
const cr = getWebcryptoSubtle();
const iKey = await cr.importKey('raw', key, keyParams, true, ['decrypt']);
const plaintext = await cr.decrypt(cryptParams, iKey, ciphertext);
return new Uint8Array(plaintext);
},
};
};
}
export const aes_128_ctr = generate('AES-CTR', 128);
export const aes_256_ctr = generate('AES-CTR', 256);
export const aes_128_cbc = generate('AES-CBC', 128);
export const aes_256_cbc = generate('AES-CBC', 256);
export const aes_128_gcm = generate('AES-GCM', 128);
export const aes_256_gcm = generate('AES-GCM', 256);
//# sourceMappingURL=aes.js.map