Gathering detailed insights and metrics for @polkadot/x-noble-hashes
Gathering detailed insights and metrics for @polkadot/x-noble-hashes
Gathering detailed insights and metrics for @polkadot/x-noble-hashes
Gathering detailed insights and metrics for @polkadot/x-noble-hashes
Utilities and base libraries for use across polkadot-js for Polkadot and Substrate. Includes base libraries, crypto helpers and cross-environment helpers.
npm install @polkadot/x-noble-hashes
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (99.73%)
JavaScript (0.16%)
HTML (0.11%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
Apache-2.0 License
257 Stars
3,596 Commits
151 Forks
9 Watchers
6 Branches
76 Contributors
Updated on Jul 10, 2025
Latest Version
8.1.2
Package Id
@polkadot/x-noble-hashes@8.1.2
Unpacked Size
237.26 kB
Size
61.36 kB
File Count
63
NPM Version
8.1.0
Node Version
16.13.0
Published on
Dec 05, 2021
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
Fast, secure & minimal JS implementation of SHA2, SHA3, RIPEMD, BLAKE2/3, HMAC, HKDF, PBKDF2 & Scrypt.
n: 2**22
with 4GB arrays while other implementations crash on 2**21
or even 2**20
, maxmem
security param, onProgress
callbackThe library's initial development was funded by Ethereum Foundation.
noble-crypto — high-security, easily auditable set of contained cryptographic libraries and tools.
Use NPM in node.js / browser, or include single file from GitHub's releases page:
npm install ../noble-hashes
The library does not have an entry point. It allows you to select specific primitives and drop everything else. If you only want to use sha256, just use the library with rollup or other bundlers. This is done to make your bundles tiny.
1const { sha256 } = require('@noble/hashes/lib/sha256'); 2console.log(sha256(new Uint8Array([1, 2, 3]))); 3// Uint8Array(32) [3, 144, 88, 198, 242, 192, 203, 73, ...] 4 5// you could also pass strings that will be UTF8-encoded to Uint8Array 6console.log(sha256('abc'))); // == sha256(new TextEncoder().encode('abc')) 7 8// sha384 is here, because it uses same internals as sha512 9const { sha512, sha512_256, sha384 } = require('@noble/hashes/lib/sha512'); 10// prettier-ignore 11const { 12 sha3_224, sha3_256, sha3_384, sha3_512, 13 keccak_224, keccak_256, keccak_384, keccak_512, 14 shake128, shake256 15} = require('@noble/hashes/lib/sha3'); 16// prettier-ignore 17const { 18 cshake128, cshake256, kmac128, kmac256, 19 k12, m14, 20 tuplehash256, parallelhash256, keccakprg 21} = require('@noble/hashes/lib/sha3-addons'); 22const { ripemd160 } = require('@noble/hashes/lib/ripemd160'); 23const { blake3 } = require('@noble/hashes/lib/blake3'); 24const { blake2b } = require('@noble/hashes/lib/blake2b'); 25const { blake2s } = require('@noble/hashes/lib/blake2s'); 26const { hmac } = require('@noble/hashes/lib/hmac'); 27const { hkdf } = require('@noble/hashes/lib/hkdf'); 28const { pbkdf2, pbkdf2Async } = require('@noble/hashes/lib/pbkdf2'); 29const { scrypt, scryptAsync } = require('@noble/hashes/lib/scrypt'); 30 31// small utility method that converts bytes to hex 32const { bytesToHex as toHex } = require('@noble/hashes/lib/utils'); 33console.log(toHex(sha256('abc'))); 34// ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
All hash functions:
Uint8Array
.Uint8Array
string
, which is automatically converted to Uint8Array
via utf8 encoding (not hex)1function hash(message: Uint8Array | string): Uint8Array; 2hash(new Uint8Array([1, 3])); 3hash('string') == hash(new TextEncoder().encode('string'));
All hash functions can be constructed via hash.create()
method:
Hash
subclass instance, which has update()
and digest()
methodsdigest()
finalizes the hash and makes it no longer usable1hash 2 .create() 3 .update(new Uint8Array([1, 3])) 4 .digest();
Some hash functions can also receive options
object, which can be either passed as a:
blake3('abc', { key: 'd', dkLen: 32 })
blake3.create({ context: 'e', dkLen: 32 })
1import { sha256 } from '@noble/hashes/lib/sha256.js'; 2const h1a = sha256('abc'); 3const h1b = sha256 4 .create() 5 .update(Uint8Array.from([1, 2, 3])) 6 .digest();
1import { sha512 } from '@noble/hashes/lib/sha512.js'; 2const h2a = sha512('abc'); 3const h2b = sha512 4 .create() 5 .update(Uint8Array.from([1, 2, 3])) 6 .digest(); 7 8// SHA512/256 variant 9import { sha512_256 } from '@noble/hashes/lib/sha512.js'; 10const h3a = sha512_256('abc'); 11const h3b = sha512_256 12 .create() 13 .update(Uint8Array.from([1, 2, 3])) 14 .digest(); 15 16// SHA384 17import { sha384 } from '@noble/hashes/lib/sha512.js'; 18const h4a = sha384('abc'); 19const h4b = sha384 20 .create() 21 .update(Uint8Array.from([1, 2, 3])) 22 .digest();
See RFC 4634 and the paper on SHA512/256.
1import { 2 sha3_224, 3 sha3_256, 4 sha3_384, 5 sha3_512, 6 keccak_224, 7 keccak_256, 8 keccak_384, 9 keccak_512, 10 shake128, 11 shake256, 12} from '@noble/hashes/lib/sha3.js'; 13const h5a = sha3_256('abc'); 14const h5b = sha3_256 15 .create() 16 .update(Uint8Array.from([1, 2, 3])) 17 .digest(); 18const h6a = keccak_256('abc'); 19const h7a = shake128('abc', { dkLen: 512 }); 20const h7b = shake256('abc', { dkLen: 512 });
See (FIPS PUB 202, Website).
Check out the differences between SHA-3 and Keccak
1import { 2 cshake128, 3 cshake256, 4 kmac128, 5 kmac256, 6 k12, 7 m14, 8 tuplehash128, 9 tuplehash256, 10 parallelhash128, 11 parallelhash256, 12 keccakprg, 13} from '@noble/hashes/lib/sha3-addons.js'; 14const h7c = cshake128('abc', { personalization: 'def' }); 15const h7d = cshake256('abc', { personalization: 'def' }); 16const h7e = kmac128('key', 'message'); 17const h7f = kmac256('key', 'message'); 18const h7h = k12('abc'); 19const h7g = m14('abc'); 20const h7i = tuplehash128(['ab', 'c']); // tuplehash(['ab', 'c']) !== tuplehash(['a', 'bc']) !== tuplehash(['abc']) 21// Same as k12/blake3, but without reduced number of rounds. Doesn't speedup anything due lack of SIMD and threading, 22// added for compatibility. 23const h7j = parallelhash128('abc', { blockLen: 8 }); 24// pseudo-random generator, first argument is capacity. XKCP recommends 254 bits capacity for 128-bit security strength. 25// * with a capacity of 254 bits. 26const p = keccakprg(254); 27p.feed('test'); 28const rand1b = p.fetch(1);
1import { ripemd160 } from '@noble/hashes/lib/ripemd160.js'; 2// function ripemd160(data: Uint8Array): Uint8Array; 3const hash8 = ripemd160('abc'); 4const hash9 = ripemd160() 5 .create() 6 .update(Uint8Array.from([1, 2, 3])) 7 .digest();
1import { blake2b } from '@noble/hashes/lib/blake2b.js'; 2import { blake2s } from '@noble/hashes/lib/blake2s.js'; 3const h10a = blake2s('abc'); 4const b2params = { key: new Uint8Array([1]), personalization: t, salt: t, dkLen: 32 }; 5const h10b = blake2s('abc', b2params); 6const h10c = blake2s 7 .create(b2params) 8 .update(Uint8Array.from([1, 2, 3])) 9 .digest();
1import { blake3 } from '@noble/hashes/lib/blake3.js'; 2// All params are optional 3const h11 = blake3('abc', { dkLen: 256, key: 'def', context: 'fji' });
See Website.
1import { hmac } from '@noble/hashes/lib/hmac.js'; 2import { sha256 } from '@noble/hashes/lib/sha256.js'; 3const mac1 = hmac(sha256, 'key', 'message'); 4const mac2 = hmac.create(sha256, Uint8Array.from([1, 2, 3])).update(Uint8Array.from([4, 5, 6]).digest();
Matches RFC 2104.
1import { hkdf } from '@noble/hashes/lib/kdf.js'; 2import { sha256 } from '@noble/hashes/lib/sha256.js'; 3import { randomBytes } from '../noble-hashes/utils.js'; 4const inputKey = randomBytes(32); 5const salt = randomBytes(32); 6const info = 'abc'; 7const dkLen = 32; 8const hk1 = hkdf(sha256, inputKey, salt, info, dkLen); 9 10// == same as 11import { hkdf_extract, hkdf_expand } from '@noble/hashes/lib/kdf.js'; 12import { sha256 } from '@noble/hashes/lib/sha256.js'; 13const prk = hkdf_extract(sha256, inputKey, salt); 14const hk2 = hkdf_expand(sha256, prk, info, dkLen);
Matches RFC 5869.
1import { pbkdf2, pbkdf2Async } from '@noble/hashes/lib/pbkdf2.js'; 2import { sha256 } from '@noble/hashes/lib/sha256.js'; 3const pbkey1 = pbkdf2(sha256, 'password', 'salt', { c: 32, dkLen: 32 }); 4const pbkey2 = await pbkdf2Async(sha256, 'password', 'salt', { c: 32, dkLen: 32 }); 5const pbkey3 = await pbkdf2Async(sha256, Uint8Array.from([1, 2, 3]), Uint8Array.from([4, 5, 6]), { 6 c: 32, 7 dkLen: 32, 8});
Matches RFC 2898.
1import { scrypt, scryptAsync } from '@noble/hashes/lib/scrypt.js'; 2const scr1 = scrypt('password', 'salt', { N: 2 ** 16, r: 8, p: 1, dkLen: 32 }); 3const scr2 = await scryptAsync('password', 'salt', { N: 2 ** 16, r: 8, p: 1, dkLen: 32 }); 4const scr3 = await scryptAsync(Uint8Array.from([1, 2, 3]), Uint8Array.from([4, 5, 6]), { 5 N: 2 ** 22, 6 r: 8, 7 p: 1, 8 dkLen: 32, 9 onProgress(percentage) { 10 console.log('progress', percentage); 11 }, 12 maxmem: 2 ** 32 + 128 * 8 * 1, // N * r * p * 128 + (128*r*p) 13});
N, r, p
are work factors. To understand them, see the blog post.dkLen
is the length of output bytes2**10
to 2**22
and {r: 8, p: 1, dkLen: 32}
onProgress
can be used with async version of the function to report progress to a user.Memory usage of scrypt is calculated with the formula N * r * p * 128 + (128 * r * p)
, which means
{N: 2 ** 22, r: 8, p: 1}
will use 4GB + 1KB of memory. To prevent DoS, we limit scrypt to 1GB + 1KB
of RAM used,
which corresponds to {N: 2 ** 20, r: 8, p: 1}
. If you want to use higher values, increase maxmem
using the formula above.
Note: noble supports 2**22
(4GB RAM) which is the highest amount amongst JS libs. Many other implementations don't support it.
We cannot support 2**23
, because there is a limitation in JS engines that makes allocating
arrays bigger than 4GB impossible, but we're looking into other possible solutions.
1import { bytesToHex as toHex, randomBytes } from '@noble/hashes/lib/scrypt.js'; 2console.log(toHex(randomBytes(32)));
bytesToHex
will convert Uint8Array
to a hex stringrandomBytes(bytes)
will produce cryptographically secure random Uint8Array
of length bytes
Noble is production-ready.
The library will be audited by an independent security firm in the next few months.
The library has been fuzzed by Guido Vranken's cryptofuzz. You can run the fuzzer by yourself to check it.
A note on timing attacks: JIT-compiler and Garbage Collector make "constant time" extremely hard to achieve in a scripting language. Which means any other JS library can't have constant-timeness. Even statically typed Rust, a language without GC, makes it harder to achieve constant-time for some cases. If your goal is absolute security, don't use any JS lib — including bindings to native ones. Use low-level libraries & languages. Nonetheless we're targetting algorithmic constant time.
We consider infrastructure attacks like rogue NPM modules very important; that's why it's crucial to minimize the amount of 3rd-party dependencies & native bindings. If your app uses 500 dependencies, any dep could get hacked and you'll be downloading rootkits with every npm install
. Our goal is to minimize this attack vector.
Benchmarks measured on Apple M1 with macOS 12 using 32-byte inputs.
Note that PBKDF2 and Scrypt are tested with extremely high work factor.
To run benchmarks, execute npm run bench-install
and then npm run bench
SHA256 x 1,131,221 ops/sec @ 884ns/op
SHA384 x 452,284 ops/sec @ 2μs/op
SHA512 x 451,059 ops/sec @ 2μs/op
SHA3-256, keccak256, shake256 x 185,494 ops/sec @ 5μs/op
Kangaroo12 x 300,480 ops/sec @ 3μs/op
Marsupilami14 x 269,614 ops/sec @ 3μs/op
BLAKE2b x 291,375 ops/sec @ 3μs/op
BLAKE2s x 505,561 ops/sec @ 1μs/op
BLAKE3 x 576,036 ops/sec @ 1μs/op
HMAC-SHA256 x 342,583 ops/sec @ 2μs/op
RIPEMD160 x 1,191,895 ops/sec @ 839ns/op
HKDF-SHA256 x 115,500 ops/sec @ 8μs/op
PBKDF2-HMAC-SHA256 262144 x 2 ops/sec @ 338ms/op
PBKDF2-HMAC-SHA512 262144 x 0 ops/sec @ 1024ms/op
Scrypt r: 8, p: 1, n: 262144 x 1 ops/sec @ 637ms/op
Compare to native node.js implementation that uses C bindings instead of pure-js code:
SHA256 32B native x 1,164,144 ops/sec @ 859ns/op
SHA384 32B native x 938,086 ops/sec @ 1μs/op
SHA512 32B native x 946,969 ops/sec @ 1μs/op
SHA3 32B native x 879,507 ops/sec @ 1μs/op
keccak, k12, m14 are not implemented
BLAKE2b 32B native x 879,507 ops/sec @ 1μs/op
BLAKE2s 32B native x 977,517 ops/sec @ 1μs/op
BLAKE3 is not implemented
RIPEMD160 32B native x 913,242 ops/sec @ 1μs/op
HMAC-SHA256 32B native x 755,287 ops/sec @ 1μs/op
HKDF-SHA256 32B native x 207,856 ops/sec @ 4μs/op
PBKDF2-HMAC-SHA256 262144 native x 23 ops/sec @ 42ms/op
Scrypt 262144 native x 1 ops/sec @ 564ms/op
Scrypt 262144 scrypt.js x 0 ops/sec @ 1678ms/op
It is possible to make this library 4x+ faster by doing code generation of full loop unrolls. We've decided against it. Reasons:
The current performance is good enough when compared to other projects; SHA256 takes only 900 nanoseconds to run.
npm install
to install build dependencies like TypeScriptnpm run build
to compile TypeScript codenpm run test
will execute all main tests. See our approach to testingnpm run test-dos
will test against DoS; by measuring function complexity. Takes ~20 minutesnpm run test-big
will execute hashing on 4GB inputs,
scrypt with 1024 different N, r, p
combinations, etc. Takes several hours. Using 8-32+ core CPU helps.The MIT License (MIT)
Copyright (c) 2021 Paul Miller (https://paulmillr.com)
See LICENSE file.
No vulnerabilities found.
Reason
24 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 6
Details
Reason
Found 15/30 approved changesets -- score normalized to 5
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
52 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More