Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node): crypto.{Cipheriv,Decipheriv}.setAutoPadding() #22228

Closed
wants to merge 21 commits into from
Closed
Prev Previous commit
Next Next commit
work
  • Loading branch information
iuioiua committed Feb 7, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 96c7eb83449c15713b9768716bf33d5de1dd5f4a
52 changes: 52 additions & 0 deletions cli/tests/unit_node/crypto/crypto_cipher_test.ts
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import { Readable } from "node:stream";
import { buffer, text } from "node:stream/consumers";
import {
assertEquals,
assertStrictEquals,
assertThrows,
} from "../../../../test_util/std/assert/mod.ts";

@@ -259,3 +260,54 @@ Deno.test({
);
},
});

const iv = Buffer.from("00000000000000000000000000000000", "hex");
const key = Buffer.from(
"0123456789abcdef0123456789abcdef" +
"0123456789abcdef0123456789abcdef",
"hex",
);

function encrypt(alg: string, val: string, pad: boolean): string {
const c = crypto.createCipheriv(alg, key, iv);
c.setAutoPadding(pad);
return c.update(val, "utf8", "latin1") + c.final("latin1");
}

function decrypt(alg: string, val: string, pad: boolean): string {
const c = crypto.createDecipheriv(alg, key, iv);
c.setAutoPadding(pad);
return c.update(val, "latin1", "utf8") + c.final("utf8");
}

// Taken from parallel/test-crypto-padding-aes256.js
Deno.test("setAutoPadding()", () => {
const algs = [
"aes-128-cbc",
"aes-128-ecb",
"aes-192-ecb",
"aes-256-ecb",
"aes-128-gcm",
"aes-256-gcm",
"aes256",
"aes-256-cbc",
];

for (const alg of algs) {
// echo 0123456789abcdef0123456789abcdef \
// | openssl enc -e -<alg> -nopad -K <key> -iv <iv> \
// | openssl enc -d -<alg> -nopad -K <key> -iv <iv>
let plaintext = "0123456789abcdef0123456789abcdef"; // Multiple of block size
let encrypted = encrypt(alg, plaintext, false);
let decrypted = decrypt(alg, encrypted, false);
assertStrictEquals(decrypted, plaintext);

// echo 0123456789abcdef0123456789abcde \
// | openssl enc -e -<alg> -K <key> -iv <iv> \
// | openssl enc -d -<alg> -K <key> -iv <iv>
plaintext = "0123456789abcdef0123456789abcde"; // not a multiple
encrypted = encrypt(alg, plaintext, true);
decrypted = decrypt(alg, encrypted, true);
assertStrictEquals(decrypted, plaintext);
}
});
Loading