first commit
This commit is contained in:
commit
b6256abbfb
397 files changed
+537630
No files matched your search
Vendored
+33
@@ -0,0 +1,33 @@
|
||||
import { createCipheriv, createDecipheriv, randomBytes, } from "node:crypto";
|
||||
const GCM_ALGORITHM = "aes-128-gcm";
|
||||
/** GCM nonce size in bytes */
|
||||
export const NONCE_SIZE = 12;
|
||||
/** GCM authentication tag size in bytes */
|
||||
export const TAG_SIZE = 16;
|
||||
/**
|
||||
* Encrypt with AES-128-GCM.
|
||||
* Returns ciphertext + 16-byte auth tag appended.
|
||||
*/
|
||||
export function aesEncryptGCM(plaintext, key, nonce) {
|
||||
const cipher = createCipheriv(GCM_ALGORITHM, key, nonce);
|
||||
const encrypted = Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
||||
const tag = cipher.getAuthTag();
|
||||
return Buffer.concat([encrypted, tag]);
|
||||
}
|
||||
/**
|
||||
* Decrypt AES-128-GCM. Input is ciphertext with 16-byte auth tag appended.
|
||||
*/
|
||||
export function aesDecryptGCM(ciphertextWithTag, key, nonce) {
|
||||
const ciphertext = ciphertextWithTag.subarray(0, ciphertextWithTag.length - TAG_SIZE);
|
||||
const tag = ciphertextWithTag.subarray(ciphertextWithTag.length - TAG_SIZE);
|
||||
const decipher = createDecipheriv(GCM_ALGORITHM, key, nonce);
|
||||
decipher.setAuthTag(tag);
|
||||
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
||||
}
|
||||
export function generateAESKey() {
|
||||
return randomBytes(16);
|
||||
}
|
||||
export function generateNonce() {
|
||||
return randomBytes(NONCE_SIZE);
|
||||
}
|
||||
//# sourceMappingURL=aes.js.map
|
||||
Reference in new issue
Block a user