Created
November 8, 2022 13:56
-
-
Save janispritzkau/3ff31e2878e8c3e64ad9f60400f86130 to your computer and use it in GitHub Desktop.
Deno AES-128-CFB8 encryption using OpenSSL and FFI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const lib = Deno.dlopen( | |
Deno.env.get("DENO_SSL_PATH")!, | |
{ | |
AES_set_encrypt_key: { | |
parameters: ["buffer", "u32", "buffer"], | |
result: "i32", | |
}, | |
AES_cfb8_encrypt: { | |
parameters: [ | |
"buffer", | |
"buffer", | |
"usize", | |
"buffer", | |
"buffer", | |
"pointer", | |
"u32", | |
], | |
result: "void", | |
}, | |
} as const, | |
); | |
export class Aes128Cfb8 { | |
#key = new Uint32Array(60); | |
#iv: Uint8Array; | |
constructor(key: Uint8Array, iv: Uint8Array) { | |
const code = lib.symbols.AES_set_encrypt_key(key, 128, this.#key); | |
if (code != 0) throw new Error("Invalid or missing key"); | |
this.#iv = iv.slice(); | |
} | |
encrypt(buf: Uint8Array) { | |
lib.symbols.AES_cfb8_encrypt( | |
buf, | |
buf, | |
buf.length, | |
this.#key, | |
this.#iv, | |
0, | |
1, | |
); | |
} | |
decrypt(buf: Uint8Array) { | |
lib.symbols.AES_cfb8_encrypt( | |
buf, | |
buf, | |
buf.length, | |
this.#key, | |
this.#iv, | |
0, | |
0, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment