코드 이식

This commit is contained in:
2023-09-22 20:46:23 +09:00
parent 0f9d5c2dc9
commit b8d57a014a
41 changed files with 4202 additions and 7 deletions
+61
View File
@@ -0,0 +1,61 @@
import type { BufferSource } from "./types.js";
const subtle = globalThis.crypto.subtle;
export async function AES_GCM_Encrypt(key: BufferSource | CryptoKey, iv: BufferSource, msg: BufferSource, aad?: BufferSource): Promise<ArrayBuffer> {
const keyObj = key instanceof CryptoKey ? key : await subtle.importKey("raw", key, {
name: "AES-GCM",
}, false, ["encrypt"]);
const ciphertext = await subtle.encrypt({
name: "AES-GCM",
iv,
additionalData: aad
}, keyObj, msg);
return ciphertext;
}
export async function AES_GCM_Decrypt(key: BufferSource | CryptoKey, iv: BufferSource, ciphertext: BufferSource, aad?: BufferSource): Promise<ArrayBuffer> {
const keyObj = key instanceof CryptoKey ? key : await subtle.importKey("raw", key, {
name: "AES-GCM",
}, false, ["decrypt"]);
const plaintext = await subtle.decrypt({
name: "AES-GCM",
iv: iv,
additionalData: aad
}, keyObj, ciphertext);
//아마도 tag 검증 실패시 예외가 발생할 것으로 예상
return plaintext;
}
export async function AES_CBC_Encrypt(key: BufferSource | CryptoKey, iv: BufferSource, msg: BufferSource): Promise<ArrayBuffer> {
const keyObj = key instanceof CryptoKey ? key : await subtle.importKey("raw", key, {
name: "AES-CBC",
}, false, ["encrypt"]);
const ciphertext = await subtle.encrypt({
name: "AES-CBC",
iv,
}, keyObj, msg);
return ciphertext;
}
export async function AES_CBC_Decrypt(key: BufferSource | CryptoKey, iv: BufferSource, ciphertext: BufferSource): Promise<ArrayBuffer> {
const keyObj = key instanceof CryptoKey ? key : await subtle.importKey("raw", key, {
name: "AES-CBC",
}, false, ["decrypt"]);
const plaintext = await subtle.decrypt({
name: "AES-CBC",
iv: iv,
}, keyObj, ciphertext);
return plaintext;
}