## @strpc 기존 RPC를 package화 ### @strpc/express express의 middleware + router 결함 ## @sammo 게임 전체 - server, client - gateway_server, gateway_client
61 lines
1.9 KiB
TypeScript
61 lines
1.9 KiB
TypeScript
import type { BufferSource } from "@sammo/util";
|
|
|
|
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;
|
|
} |