0

I found similar question here, but it lacks information about the "binary" encoding. TextEncoder is (seemingly) not an equivalent for "binary" in Deno.

Here is an example:

Deno

const str = "ºRFl¶é(÷LõÎW0 Náò8ìÉPPv\0";
const bytes = new TextEncoder().encode(str);
console.log(crypto.createHash("sha256").update(bytes).digest("hex"));

Outputs: 65e16c433fdc795b29668dc1d189b79f2b809dc4623b03c0b9c551bd83d67069

Node

const str = "ºRFl¶é(÷LõÎW0 Náò8ìÉPPv\0";
const buffer = Buffer.from(str, "binary");
console.log(crypto.createHash("sha256").update(buffer).digest("hex"));

Node outputs: fb6d4a2f86e91b13fe2d5a6d2e6ebb9b6f66e18a733b68acbf9ac3c5e56571d0

Sean
  • 4,524
  • 2
  • 11
  • 12

1 Answers1

3

Node's (deprecated) binary encoding is actually latin-1 (ISO-8859-1).

Assuming that your string does not use characters outside that range, you can create a byte array by converting the individual characters to their UTF-16 code unit values:

import { createHash } from "https://deno.land/std/hash/mod.ts";

const str = "ºRFl¶é(÷LõÎW0 Náò8ìÉPPv\0";
const bytes = Uint8Array.from([...str].map(c => c.charCodeAt(0)));

console.log(createHash("sha256").update(bytes).toString());

Which, as required, outputs:

fb6d4a2f86e91b13fe2d5a6d2e6ebb9b6f66e18a733b68acbf9ac3c5e56571d0
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156