forge icon indicating copy to clipboard operation
forge copied to clipboard

result as Uint8Array

Open psvz opened this issue 2 years ago • 2 comments

var md = forge.md.sha256.create()
md.update(data)
const hash = md.digest()

This gives me some weird Object. How do I get 32 bytes in Uint8Array in the most performant way?

psvz avatar Apr 17 '23 17:04 psvz

Okay, so, basically, to operate on bytes properly:

// msg is Uint8Array
var yuck = ''
msg.forEach(b => yuck += String.fromCharCode(b))

var md = forge.md.sha256.create()
md.update(yuck)

const hash = Uint8Array.from(md.digest().data, c => c.charCodeAt(0))

I am wondering if there is more standard way...

psvz avatar Apr 17 '23 19:04 psvz

Kind of wish they removed all kind of NodeJS specific stuff and used web api's instead... meaning removing all node:buffer and node:crypto and instead used only Uint8Array & "web crypto" (and also DataView to modify the data)

NodeJS have web crypto built right in now on the global scope, but requires v18+ earlier version (v15+) can use

import { webcrypto as crypto } from 'node:crypto'

jimmywarting avatar Apr 27 '23 16:04 jimmywarting