aes 수정, 전통적으로.

This commit is contained in:
2023-08-19 05:42:56 +00:00
parent b850eb285b
commit 0ee7066679
+6 -15
View File
@@ -1,15 +1,9 @@
import { webcrypto } from "crypto";
const subtle = webcrypto.subtle;
type BufferSource = ArrayBufferView | ArrayBuffer;
type EncryptedData = {
iv: ArrayBuffer;
ciphertext: ArrayBuffer; //tag 포함
}
export async function AES_GCM_Encrypt(key: ArrayBuffer, msg: ArrayBuffer, aad?: ArrayBuffer): Promise<EncryptedData> {
const iv = webcrypto.getRandomValues(new Uint8Array(12));
export async function AES_GCM_Encrypt(key: BufferSource, iv: BufferSource, msg: BufferSource, aad?: BufferSource): Promise<ArrayBuffer> {
const keyObj = await subtle.importKey("raw", key, {
name: "AES-GCM",
}, false, ["encrypt"]);
@@ -20,23 +14,20 @@ export async function AES_GCM_Encrypt(key: ArrayBuffer, msg: ArrayBuffer, aad?:
additionalData: aad
}, keyObj, msg);
return {
iv,
ciphertext
}
return ciphertext;
}
export async function AES_GCM_Decrypt(key: ArrayBuffer, data: EncryptedData, aad?: ArrayBuffer): Promise<ArrayBuffer> {
export async function AES_GCM_Decrypt(key: BufferSource, iv: BufferSource, ciphertext: BufferSource, aad?: BufferSource): 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,
iv: iv,
additionalData: aad
}, keyObj, data.ciphertext);
}, keyObj, ciphertext);
//아마도 tag 검증 실패시 예외가 발생할 것으로 예상
return plaintext;