Skip to content

Instantly share code, notes, and snippets.

@janispritzkau
Created November 8, 2022 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janispritzkau/3ff31e2878e8c3e64ad9f60400f86130 to your computer and use it in GitHub Desktop.
Save janispritzkau/3ff31e2878e8c3e64ad9f60400f86130 to your computer and use it in GitHub Desktop.
Deno AES-128-CFB8 encryption using OpenSSL and FFI
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