question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hashing string with sha-384 always returns an empty ArrayBuffer

See original GitHub issue

Describe the bug When hashing something with sha-384, the await subtle.digest(“sha-384”, stringToHash) alway results in an empty ArrayBuffer. Steps to Reproduce

  1. Create a file with the example usage from std@0.112.0/crypto
  2. Replace the "hello world" string with new TextEncoder().encode("hello world") (because the function does not take strings anymore)
  3. Run the Script

Expected behavior

Something like

Check file:///PathToFile/cryptoTest.ts
Uint8Array(48) [
  253, 189, 142, 117, 166, 127,  41, 247,   1,
  164, 224,  64,  56,  94,  46,  35, 152,  99,
    3, 234,  16,  35, 146,  17, 175, 144, 127,
  203, 184,  53, 120, 179, 228,  23, 203, 113,
  206, 100, 110, 253,   8,  25, 221, 140,   8,
  141, 225, 189
]
Uint8Array(32) [
  215,  73, 129, 239, 167,  10,  12,
  136,  11, 141, 140,  25, 133, 208,
  117, 219, 203, 246, 121, 185, 154,
   95, 153,  20, 229, 170, 249, 107,
  131,  26, 158,  36
]

instead of

Check file:///PathToFile/cryptoTest.ts
ArrayBuffer {}
Uint8Array(32) [
  215,  73, 129, 239, 167,  10,  12,
  136,  11, 141, 140,  25, 133, 208,
  117, 219, 203, 246, 121, 185, 154,
   95, 153,  20, 229, 170, 249, 107,
  131,  26, 158,  36
]

Environment

  • OS: Windows 11 (AMD64) as well as Debian (aarch64)
  • deno version: 1.15.2 (release, x86_64-pc-windows-msvc) as well as 1.15.2 (debug, aarch64-unknown-linux-gnu)
  • std version: both 0.112.0 (Most recent one at time of posting) and the main branch of this repo (Last Commit before my testing)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
littledivycommented, Jan 1, 2022

@lmx-Hexagram This is not an issue with std/crypto. Deno’s console implementation chose not print the byteLength for ArrayBuffers. You can get the underlying bytes by putting into a view (a TypedArray)

Anyways, https://github.com/denoland/deno_std/pull/1515 already fixed the original issue by always returning an ArrayBuffer (in accordance with WebCrypto).

1reaction
herkulessicommented, Oct 22, 2021

Apparently converting the empty ArrayBuffer to a UInt8Array with new Uint8Array(await crypto.subtle.digest("SHA-384", new TextEncoder().encode("hello world"))) returns the proper Values.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I hash a string with SHA256 in JS? - Stack Overflow
I'm looking to hash a string locally with SHA256 in Javascript. I've been looking around thinking there would be some sort of official...
Read more >
Calculate TOTP in Postman - gists · GitHub
@preserve A JavaScript implementation of the SHA family of hashes, as ... @return {function((string|ArrayBuffer), Array<number>=, number=): {value :.
Read more >
A Guide to the JavaScript window.crypto Object
It returns a promise that's resolved with the ArrayBuffer object which has the plain text. For example, we can use the encrypt and...
Read more >
SubtleCrypto.digest() - Web APIs - MDN Web Docs - Mozilla
The digest is returned as an ArrayBuffer , but for comparison and display digests are often represented as hex strings.
Read more >
Javascript hash string sha256
The SHA256 () function returns a string with the SHA256 encrypted hash as a 64-character ... Note: The hash value of an empty...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found