This commit is contained in:
2023-08-19 04:39:36 +00:00
parent 635d7f167d
commit ad4ac9dfb2
+41
View File
@@ -0,0 +1,41 @@
import { webcrypto } from "crypto";
const subtle = webcrypto.subtle;
type EncryptedData = {
iv: ArrayBuffer;
ciphertext: ArrayBuffer; //tag 포함
}
export async function AES_GCM_Encrypt(key: ArrayBuffer, msg: ArrayBuffer): Promise<EncryptedData> {
const iv = webcrypto.getRandomValues(new Uint8Array(12));
const keyObj = await subtle.importKey("raw", key, {
name: "AES-GCM",
}, false, ["encrypt"]);
const ciphertext = await subtle.encrypt({
name: "AES-GCM",
iv
}, keyObj, msg);
return {
iv,
ciphertext
}
}
export async function AES_GCM_Decrypt(key: ArrayBuffer, data: EncryptedData): Promise<ArrayBuffer> {
const keyObj = await subtle.importKey("raw", key, {
name: "AES-GCM",
}, false, ["decrypt"]);
const plaintext = await subtle.decrypt({
name: "AES-GCM",
iv: data.iv
}, keyObj, data.ciphertext);
//아마도 tag 검증 실패시 예외가 발생할 것으로 예상
return plaintext;
}