Skip to content

Repository files navigation

sha256-uint8array

Node.js CInpm versiongzip size

Fast SHA-256 digest hash based on Uint8Array, pure JavaScript.

SYNOPSIS

import{createHash}from"sha256-uint8array";consttext="";consthex=createHash().update(text).digest("hex");// => "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"constdata=newUint8Array(0);consthash=createHash().update(data).digest();// => <Uint8Array e3 b0 c4 42 98 fc 1c 14 9a fb f4 c8 99 6f b9 24 27 ae 41 e4 64 9b 93 4c a4 95 99 1b 78 52 b8 55>

See TypeScript declaration sha256-uint8array.d.ts for detail.

CJS

Both ES Modules and CommonJS supported.

const{createHash}=require("sha256-uint8array");

COMPATIBILITY

It has a better compatibility with Node.js's crypto module in its smaller footprint.

modulestring INUint8Array INTypedArray INhex OUTUint8Array OUTminified
crypto✅ OK✅ OK✅ OK✅ OK✅ OK-
sha256-uint8array✅ OK✅ OK✅ OK✅ OK✅ OK4KB
crypto-js✅ OK🚫 NO🚫 NO✅ OK🚫 NO66KB
jssha✅ OK✅ OK🚫 NO✅ OK✅ OK10KB
hash.js✅ OK✅ OK🚫 NO✅ OK✅ OK7KB
sha.js✅ OK✅ OK🚫 NO✅ OK✅ OK47KB
@noble/hashes🚫 NO✅ OK🚫 NO✅ OK✅ OK11KB
node-forge✅ OK🚫 NO🚫 NO✅ OK🚫 NO27KB
fast-sha256🚫 NO✅ OK🚫 NO🚫 NO✅ OK6KB
js-sha256✅ OK✅ OK🚫 NO✅ OK✅ OK8KB
@aws-crypto/sha256-js✅ OK✅ OK🚫 NO🚫 NO✅ OK14KB
crypto.subtle.digest()🚫 NO✅ OK✅ OK🚫 NO🚫 NO-

The minified sizes are measured by make -C browser/vendor, which bundles each library for browsers and runs it through terser.

The W3C standard crypto.subtle.digest() API has a different interface which returnsPromise<ArrayBuffer>.

SPEED

It runs well both on Node.js and browsers. Node.js's native crypto module definitely runs faster than any others on Node.js, though.

moduleversionnode.js V24 stringnode.js V24 U8AChromium 151 stringChromium 151 U8A
crypto-31ms 🥇17ms 🥇N/AN/A
sha256-uint8array0.11.0218ms 🥈126ms276ms 🥈168ms
crypto-js4.2.0545msN/A621msN/A
jssha3.3.2655ms395ms538ms337ms
hash.js1.1.7627ms634ms384ms487ms
sha.js2.4.12377ms363ms604ms229ms
@noble/hashes2.3.0N/A124ms 🥈N/A156ms
node-forge1.4.0572msN/A502msN/A
fast-sha2561.3.0N/A132msN/A133ms 🥈
js-sha2561.0.0--327ms307ms
@aws-crypto/sha256-js3.0.0235ms212ms225ms 🥇244ms
crypto.subtle.digest()-N/A1,484msN/A57ms 🥇

The benchmark above shows milliseconds for 20,000 SHA-256 hex digests per cell, each the median of five runs. REPEAT=10000 hashes two samples per round, a 1.9KB JSON string and a 0.9KB UTF-8 text. N/A marks an input shape the library does not accept, and - a library that hands the digest to Node's native crypto there instead of running its own JavaScript. It is tested on Linux aarch64, Node.js v24.19.0 and Chromium 151.

You could run the benchmark as below.

git clone https://github.com/kawanet/sha256-uint8array.git
cd sha256-uint8array
npm install
npm run build
# run the benchmark on Node.js
REPEAT=10000 node --test test/99.benchmark.test.ts
# run tests and the benchmark on browser
open browser/tests.html

WEB BROWSERS

<scriptsrc="https://cdn.jsdelivr.net/npm/sha256-uint8array/dist/sha256-uint8array.min.js"></script><script>consttext="";consthex=SHA256.createHash().update(text).digest("hex");// => "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"constdata=newUint8Array(0);consthash=SHA256.createHash().update(data).digest();// => <Uint8Array e3 b0 c4 42 98 fc 1c 14 9a fb f4 c8 99 6f b9 24 27 ae 41 e4 64 9b 93 4c a4 95 99 1b 78 52 b8 55></script>

BROWSERIFY

It works great with browserify via browser property of package.json of your app if you needs crypto.createHash("sha256").update(data).digest("hex"); syntax only.

{
"browser": {
"crypto": "sha256-uint8array/dist/sha256-uint8array.min.js"
},
"devDependencies": {
"browserify": "^17.0.0",
"sha256-uint8array": "^0.10.0"
}
}

It costs only less than 4KB, whereas browserify's default crypto polyfill costs more than 300KB huge even after minified.

// On Node.js, this loads Node.js's native crypto module which is faster.// On browsers, this uses sha256-uint8array.min.js which is small and fast.constcrypto=require("crypto");consthash=crypto.createHash("sha256").update("").digest("hex");// => "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

LINKS

MIT LICENSE

Copyright (c) 2020-2026 Yusuke Kawasaki

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Fast SHA-256 digest hash based on Uint8Array, pure JavaScript.

Topics

Resources

Stars

30 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages