Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

131
Views
What is the Deno equivalent of Node's Buffer.from(string, "binary")?

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

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

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
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.