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

fix(ext/node): implement aes-128-ctr and aes-256-ctr #27630

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

nathanwhit
Copy link
Member

Fixes #24864

Need to add some tests, also unsure about the right counter size (went with 128 bit to be safe)

@nathanwhit nathanwhit requested review from littledivy and kt3k January 10, 2025 23:52
@nathanwhit nathanwhit changed the title implement aes-128-ctr and aes-256-ctr fix(ext/node): implement aes-128-ctr and aes-256-ctr Jan 11, 2025
@kt3k
Copy link
Member

kt3k commented Jan 15, 2025

I confirmed this works in the same way as Node.js. Nice work!

import crypto from "node:crypto";
import { Buffer } from "node:buffer";

var algorithm = "aes-128-ctr",
  key = Buffer.from("5ebe2294ecd0e0f08eab7690d2a6ee69", "hex"),
  iv = Buffer.from("26ae5cc854e36b6bdfca366848dea6bb", "hex");

function encrypt(text) {
  const cipher = crypto.createCipheriv(algorithm, key, iv);
  var crypted = cipher.update(text, "utf8", "hex");
  crypted += cipher.final("hex");
  return crypted;
}

function decrypt(text) {
  var decipher = crypto.createDecipheriv(algorithm, key, iv);
  var dec = decipher.update(text, "hex", "utf8");
  dec += decipher.final("utf8");
  return dec;
}

var hw = encrypt("hello world");
console.log(hw);
console.log(decrypt(hw));
$ node a.mjs 
e50b8429a7297331661217
hello world
$ debug_deno a.mjs 
e50b8429a7297331661217
hello world

also unsure about the right counter size (went with 128 bit to be safe)

This seems fine to me. Node.js seems throwing TypeError: Invalid initialization vector when IV is not exactly 16 bytes long.

@nathanwhit Can you add some simple test cases?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

crypto.createCipheriv('aes-256-ctr', …) fails with Unknown cipher
2 participants