Skip to content

Latest commit

 

History

171 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sha1-uint8array

Node.js CI npm version gzip size

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

SYNOPSIS

import {createHash} from "sha1-uint8array";

const text = "";
const hex = createHash().update(text).digest("hex");
// => "da39a3ee5e6b4b0d3255bfef95601890afd80709"

const data = new Uint8Array(0);
const hash = createHash().update(data).digest();
// => <Uint8Array da 39 a3 ee 5e 6b 4b 0d 32 55 bf ef 95 60 18 90 af d8 07 09>

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

COMMONJS

Both ES Modules and CommonJS supported.

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

COMPATIBILITY

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

module string IN Uint8Array IN TypedArray IN hex OUT Uint8Array OUT minified
crypto ✅ OK ✅ OK ✅ OK ✅ OK ✅ OK -
sha1-uint8array ✅ OK ✅ OK ✅ OK ✅ OK ✅ OK 4KB
hash.js ✅ OK ✅ OK 🚫 NO ✅ OK ✅ OK 6KB
jssha ✅ OK ✅ OK 🚫 NO ✅ OK ✅ OK 9KB
crypto-js ✅ OK 🚫 NO 🚫 NO ✅ OK 🚫 NO 66KB
sha.js ✅ OK ✅ OK 🚫 NO ✅ OK ✅ OK 51KB
@noble/hashes 🚫 NO ✅ OK 🚫 NO ✅ OK ✅ OK 7KB
node-forge ✅ OK 🚫 NO 🚫 NO ✅ OK 🚫 NO 27KB
crypto.subtle.digest() 🚫 NO ✅ OK ✅ OK 🚫 NO 🚫 NO -

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

The W3C standard crypto.subtle.digest() API has a different interface which returns Promise<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.

module version Node.js v24 string Node.js v24 U8A Safari 26 string Safari 26 U8A
crypto - 27ms 🥇 21ms 🥇 ▫️ ▫️
sha1-uint8array 0.11.0 82ms 🥈 59ms 🥈 109ms 🥇 98ms 🥇
hash.js 1.1.7 464ms 461ms 596ms 574ms
jssha 3.3.2 507ms 257ms 334ms 🥈 237ms
crypto-js 4.2.0 568ms ▫️ 482ms ▫️
sha.js 2.4.12 346ms 346ms 377ms 143ms
@noble/hashes 2.3.0 ▫️ 95ms ▫️ 126ms 🥈
node-forge 1.4.0 452ms ▫️ 392ms ▫️
crypto.subtle.digest() - ▫️ 1474ms ▫️ 450ms

The benchmark above shows the median of ten sets, normalized to milliseconds per 20,000 SHA-1 hex digests. Each cell uses a fixed operation count calibrated towards 500ms. Each repeat hashes two samples, a 2KB JSON string and a 1KB UTF-8 text; every measurement is preceded by one untimed repeat to absorb first-load effects. ▫️ marks an unsupported input shape. It is tested on Apple M4, Node.js v24.19.0 and Safari 26.5.2.

You could run the benchmark as below.

git clone https://github.com/kawanet/sha1-uint8array.git
cd sha1-uint8array
npm install
npm run build

# run the benchmark on Node.js
npm run bench

# options via environment variables
DURATION=500 SETS=10 TARGET=sha1-uint8array,crypto-js npm run bench

# run the benchmark on a browser, options via the query string
open browser/bench.html   # ?DURATION=500&SETS=10&TARGET=sha1-uint8array

The runner calibrates each cell towards DURATION milliseconds. It prints one JSON line per cell — ops per set, its median duration in milliseconds, the measured sets in microseconds per op, their median and median absolute deviation — followed by a Markdown table normalized to milliseconds per 20,000 ops.

WEB BROWSERS

<script src="https://cdn.jsdelivr.net/npm/sha1-uint8array/dist/sha1-uint8array.min.js"></script>
<script>
    const text = "";
    const hex = SHA1.createHash().update(text).digest("hex");
    // => "da39a3ee5e6b4b0d3255bfef95601890afd80709"
    
    const data = new Uint8Array(0);
    const hash = SHA1.createHash().update(data).digest();
    // => <Uint8Array da 39 a3 ee 5e 6b 4b 0d 32 55 bf ef 95 60 18 90 af d8 07 09>
</script>

BROWSERIFY

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

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

It costs only about 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 sha1-uint8array.min.js which is small and fast.
const crypto = require("crypto");

const hash = crypto.createHash("sha1").update("").digest("hex");
// => "da39a3ee5e6b4b0d3255bfef95601890afd80709"

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-1 digest hash based on Uint8Array, pure JavaScript.

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages