Hashing string with sha-384 always returns an empty ArrayBuffer
See original GitHub issueDescribe the bug When hashing something with sha-384, the await subtle.digest(“sha-384”, stringToHash) alway results in an empty ArrayBuffer. Steps to Reproduce
- Create a file with the example usage from std@0.112.0/crypto
- Replace the
"hello world"
string withnew TextEncoder().encode("hello world")
(because the function does not take strings anymore) - 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 as1.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:
- Created 2 years ago
- Comments:6 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@lmx-Hexagram This is not an issue with
std/crypto
. Deno’s console implementation chose not print thebyteLength
forArrayBuffer
s. 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).
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.