aes 수정, 전통적으로.
This commit is contained in:
+6
-15
@@ -1,15 +1,9 @@
|
|||||||
import { webcrypto } from "crypto";
|
import { webcrypto } from "crypto";
|
||||||
|
|
||||||
const subtle = webcrypto.subtle;
|
const subtle = webcrypto.subtle;
|
||||||
|
type BufferSource = ArrayBufferView | ArrayBuffer;
|
||||||
|
|
||||||
type EncryptedData = {
|
export async function AES_GCM_Encrypt(key: BufferSource, iv: BufferSource, msg: BufferSource, aad?: BufferSource): Promise<ArrayBuffer> {
|
||||||
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));
|
|
||||||
const keyObj = await subtle.importKey("raw", key, {
|
const keyObj = await subtle.importKey("raw", key, {
|
||||||
name: "AES-GCM",
|
name: "AES-GCM",
|
||||||
}, false, ["encrypt"]);
|
}, false, ["encrypt"]);
|
||||||
@@ -20,23 +14,20 @@ export async function AES_GCM_Encrypt(key: ArrayBuffer, msg: ArrayBuffer, aad?:
|
|||||||
additionalData: aad
|
additionalData: aad
|
||||||
}, keyObj, msg);
|
}, keyObj, msg);
|
||||||
|
|
||||||
return {
|
return ciphertext;
|
||||||
iv,
|
|
||||||
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, {
|
const keyObj = await subtle.importKey("raw", key, {
|
||||||
name: "AES-GCM",
|
name: "AES-GCM",
|
||||||
}, false, ["decrypt"]);
|
}, false, ["decrypt"]);
|
||||||
|
|
||||||
const plaintext = await subtle.decrypt({
|
const plaintext = await subtle.decrypt({
|
||||||
name: "AES-GCM",
|
name: "AES-GCM",
|
||||||
iv: data.iv,
|
iv: iv,
|
||||||
additionalData: aad
|
additionalData: aad
|
||||||
}, keyObj, data.ciphertext);
|
}, keyObj, ciphertext);
|
||||||
//아마도 tag 검증 실패시 예외가 발생할 것으로 예상
|
//아마도 tag 검증 실패시 예외가 발생할 것으로 예상
|
||||||
|
|
||||||
return plaintext;
|
return plaintext;
|
||||||
|
|||||||
Reference in New Issue
Block a user