@noble/hashes
Advanced tools
Comparing version 1.3.0 to 1.3.1
@@ -16,5 +16,5 @@ "use strict"; | ||
if (!(b instanceof Uint8Array)) | ||
throw new TypeError('Expected Uint8Array'); | ||
throw new Error('Expected Uint8Array'); | ||
if (lengths.length > 0 && !lengths.includes(b.length)) | ||
throw new TypeError(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`); | ||
throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`); | ||
} | ||
@@ -21,0 +21,0 @@ exports.bytes = bytes; |
@@ -53,2 +53,4 @@ "use strict"; | ||
const len = data.length; | ||
const offset = data.byteOffset; | ||
const buf = data.buffer; | ||
for (let pos = 0; pos < len;) { | ||
@@ -61,6 +63,6 @@ // If buffer is full and we still have input (don't process last block, same as blake2s) | ||
const take = Math.min(blockLen - this.pos, len - pos); | ||
const dataOffset = data.byteOffset + pos; | ||
const dataOffset = offset + pos; | ||
// full block && aligned to 4 bytes && not last in input | ||
if (take === blockLen && !(dataOffset % 4) && pos + take < len) { | ||
const data32 = new Uint32Array(data.buffer, dataOffset, Math.floor((len - pos) / 4)); | ||
const data32 = new Uint32Array(buf, dataOffset, Math.floor((len - pos) / 4)); | ||
for (let pos32 = 0; pos + blockLen < len; pos32 += buffer32.length, pos += blockLen) { | ||
@@ -67,0 +69,0 @@ this.length += blockLen; |
@@ -8,5 +8,3 @@ "use strict"; | ||
const _u64_js_1 = require("./_u64.js"); | ||
// Experimental implementation of argon2. | ||
// Could be broken & slow. May be removed at a later time. | ||
// RFC 9106 | ||
// Experimental Argon2 RFC 9106 implementation. It may be removed at any time. | ||
var Types; | ||
@@ -13,0 +11,0 @@ (function (Types) { |
@@ -44,4 +44,4 @@ import { BLAKE2 } from './_blake2.js'; | ||
blockLen: number; | ||
create(opts: Blake3Opts): import("./utils.js").Hash<BLAKE3>; | ||
create(opts: Blake3Opts): HashXOF<BLAKE3>; | ||
}; | ||
export {}; |
@@ -53,3 +53,3 @@ "use strict"; | ||
else if (opts.key !== undefined) { | ||
const key = (0, utils_js_1.toBytes)(opts.key); | ||
const key = (0, utils_js_1.toBytes)(opts.key).slice(); | ||
if (key.length !== 32) | ||
@@ -240,3 +240,3 @@ throw new Error('Blake3: key should be 32 byte'); | ||
*/ | ||
exports.blake3 = (0, utils_js_1.wrapConstructorWithOpts)((opts) => new BLAKE3(opts)); | ||
exports.blake3 = (0, utils_js_1.wrapXOFConstructorWithOpts)((opts) => new BLAKE3(opts)); | ||
//# sourceMappingURL=blake3.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.crypto = void 0; | ||
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+. | ||
// See utils.ts for details. | ||
// The file will throw on node.js 14 and earlier. | ||
// @ts-ignore | ||
const nc = require("node:crypto"); | ||
exports.crypto = nc && typeof nc === 'object' && 'webcrypto' in nc ? nc.webcrypto : undefined; | ||
//# sourceMappingURL=cryptoNode.js.map |
@@ -11,5 +11,5 @@ export function number(n) { | ||
if (!(b instanceof Uint8Array)) | ||
throw new TypeError('Expected Uint8Array'); | ||
throw new Error('Expected Uint8Array'); | ||
if (lengths.length > 0 && !lengths.includes(b.length)) | ||
throw new TypeError(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`); | ||
throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`); | ||
} | ||
@@ -16,0 +16,0 @@ export function hash(hash) { |
@@ -50,2 +50,4 @@ import assert from './_assert.js'; | ||
const len = data.length; | ||
const offset = data.byteOffset; | ||
const buf = data.buffer; | ||
for (let pos = 0; pos < len;) { | ||
@@ -58,6 +60,6 @@ // If buffer is full and we still have input (don't process last block, same as blake2s) | ||
const take = Math.min(blockLen - this.pos, len - pos); | ||
const dataOffset = data.byteOffset + pos; | ||
const dataOffset = offset + pos; | ||
// full block && aligned to 4 bytes && not last in input | ||
if (take === blockLen && !(dataOffset % 4) && pos + take < len) { | ||
const data32 = new Uint32Array(data.buffer, dataOffset, Math.floor((len - pos) / 4)); | ||
const data32 = new Uint32Array(buf, dataOffset, Math.floor((len - pos) / 4)); | ||
for (let pos32 = 0; pos + blockLen < len; pos32 += buffer32.length, pos += blockLen) { | ||
@@ -64,0 +66,0 @@ this.length += blockLen; |
@@ -5,5 +5,3 @@ import assert from './_assert.js'; | ||
import u64 from './_u64.js'; | ||
// Experimental implementation of argon2. | ||
// Could be broken & slow. May be removed at a later time. | ||
// RFC 9106 | ||
// Experimental Argon2 RFC 9106 implementation. It may be removed at any time. | ||
var Types; | ||
@@ -10,0 +8,0 @@ (function (Types) { |
@@ -5,3 +5,3 @@ import assert from './_assert.js'; | ||
import { compress, IV } from './blake2s.js'; | ||
import { u8, u32, toBytes, wrapConstructorWithOpts } from './utils.js'; | ||
import { u8, u32, toBytes, wrapXOFConstructorWithOpts } from './utils.js'; | ||
// Flag bitset | ||
@@ -51,3 +51,3 @@ var Flags; | ||
else if (opts.key !== undefined) { | ||
const key = toBytes(opts.key); | ||
const key = toBytes(opts.key).slice(); | ||
if (key.length !== 32) | ||
@@ -238,3 +238,3 @@ throw new Error('Blake3: key should be 32 byte'); | ||
*/ | ||
export const blake3 = wrapConstructorWithOpts((opts) => new BLAKE3(opts)); | ||
export const blake3 = wrapXOFConstructorWithOpts((opts) => new BLAKE3(opts)); | ||
//# sourceMappingURL=blake3.js.map |
@@ -0,3 +1,7 @@ | ||
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+. | ||
// See utils.ts for details. | ||
// The file will throw on node.js 14 and earlier. | ||
// @ts-ignore | ||
import * as nc from 'node:crypto'; | ||
export const crypto = nc && typeof nc === 'object' && 'webcrypto' in nc ? nc.webcrypto : undefined; | ||
//# sourceMappingURL=cryptoNode.js.map |
import assert from './_assert.js'; | ||
import { Hash, toBytes } from './utils.js'; | ||
// HMAC (RFC 2104) | ||
class HMAC extends Hash { | ||
export class HMAC extends Hash { | ||
constructor(hash, _key) { | ||
@@ -13,3 +13,3 @@ super(); | ||
if (typeof this.iHash.update !== 'function') | ||
throw new TypeError('Expected instance of class which extends utils.Hash'); | ||
throw new Error('Expected instance of class which extends utils.Hash'); | ||
this.blockLen = this.iHash.blockLen; | ||
@@ -16,0 +16,0 @@ this.outputLen = this.iHash.outputLen; |
@@ -47,4 +47,4 @@ import { number as assertNumber } from './_assert.js'; | ||
const gencShake = (suffix, blockLen, outputLen) => wrapConstructorWithOpts((opts = {}) => cshakePers(new Keccak(blockLen, suffix, chooseLen(opts, outputLen), true), opts)); | ||
export const cshake128 = gencShake(0x1f, 168, 128 / 8); | ||
export const cshake256 = gencShake(0x1f, 136, 256 / 8); | ||
export const cshake128 = /* @__PURE__ */ (() => gencShake(0x1f, 168, 128 / 8))(); | ||
export const cshake256 = /* @__PURE__ */ (() => gencShake(0x1f, 136, 256 / 8))(); | ||
class KMAC extends Keccak { | ||
@@ -87,6 +87,6 @@ constructor(blockLen, outputLen, enableXOF, key, opts = {}) { | ||
} | ||
export const kmac128 = genKmac(168, 128 / 8); | ||
export const kmac256 = genKmac(136, 256 / 8); | ||
export const kmac128xof = genKmac(168, 128 / 8, true); | ||
export const kmac256xof = genKmac(136, 256 / 8, true); | ||
export const kmac128 = /* @__PURE__ */ (() => genKmac(168, 128 / 8))(); | ||
export const kmac256 = /* @__PURE__ */ (() => genKmac(136, 256 / 8))(); | ||
export const kmac128xof = /* @__PURE__ */ (() => genKmac(168, 128 / 8, true))(); | ||
export const kmac256xof = /* @__PURE__ */ (() => genKmac(136, 256 / 8, true))(); | ||
// TupleHash | ||
@@ -129,6 +129,6 @@ // Usage: tuple(['ab', 'cd']) != tuple(['a', 'bcd']) | ||
} | ||
export const tuplehash128 = genTuple(168, 128 / 8); | ||
export const tuplehash256 = genTuple(136, 256 / 8); | ||
export const tuplehash128xof = genTuple(168, 128 / 8, true); | ||
export const tuplehash256xof = genTuple(136, 256 / 8, true); | ||
export const tuplehash128 = /* @__PURE__ */ (() => genTuple(168, 128 / 8))(); | ||
export const tuplehash256 = /* @__PURE__ */ (() => genTuple(136, 256 / 8))(); | ||
export const tuplehash128xof = /* @__PURE__ */ (() => genTuple(168, 128 / 8, true))(); | ||
export const tuplehash256xof = /* @__PURE__ */ (() => genTuple(136, 256 / 8, true))(); | ||
class ParallelHash extends Keccak { | ||
@@ -196,3 +196,3 @@ constructor(blockLen, outputLen, leafCons, enableXOF, opts = {}) { | ||
} | ||
function genParallel(blockLen, outputLen, leaf, xof = false) { | ||
function genPrl(blockLen, outputLen, leaf, xof = false) { | ||
const parallel = (message, opts) => parallel.create(opts).update(message).digest(); | ||
@@ -202,6 +202,6 @@ parallel.create = (opts = {}) => new ParallelHash(blockLen, chooseLen(opts, outputLen), () => leaf.create({ dkLen: 2 * outputLen }), xof, opts); | ||
} | ||
export const parallelhash128 = genParallel(168, 128 / 8, cshake128); | ||
export const parallelhash256 = genParallel(136, 256 / 8, cshake256); | ||
export const parallelhash128xof = genParallel(168, 128 / 8, cshake128, true); | ||
export const parallelhash256xof = genParallel(136, 256 / 8, cshake256, true); | ||
export const parallelhash128 = /* @__PURE__ */ (() => genPrl(168, 128 / 8, cshake128))(); | ||
export const parallelhash256 = /* @__PURE__ */ (() => genPrl(136, 256 / 8, cshake256))(); | ||
export const parallelhash128xof = /* @__PURE__ */ (() => genPrl(168, 128 / 8, cshake128, true))(); | ||
export const parallelhash256xof = /* @__PURE__ */ (() => genPrl(136, 256 / 8, cshake256, true))(); | ||
// Kangaroo | ||
@@ -290,5 +290,5 @@ // Same as NIST rightEncode, but returns [0] for zero string | ||
// Default to 32 bytes, so it can be used without opts | ||
export const k12 = wrapConstructorWithOpts((opts = {}) => new KangarooTwelve(168, 32, chooseLen(opts, 32), 12, opts)); | ||
export const k12 = /* @__PURE__ */ (() => wrapConstructorWithOpts((opts = {}) => new KangarooTwelve(168, 32, chooseLen(opts, 32), 12, opts)))(); | ||
// MarsupilamiFourteen | ||
export const m14 = wrapConstructorWithOpts((opts = {}) => new KangarooTwelve(136, 64, chooseLen(opts, 64), 14, opts)); | ||
export const m14 = /* @__PURE__ */ (() => wrapConstructorWithOpts((opts = {}) => new KangarooTwelve(136, 64, chooseLen(opts, 64), 14, opts)))(); | ||
// https://keccak.team/files/CSF-0.1.pdf | ||
@@ -295,0 +295,0 @@ // + https://github.com/XKCP/XKCP/tree/master/lib/high/Keccak/PRG |
import assert from './_assert.js'; | ||
import u64 from './_u64.js'; | ||
import { Hash, u32, toBytes, wrapConstructor, wrapConstructorWithOpts, } from './utils.js'; | ||
import { Hash, u32, toBytes, wrapConstructor, wrapXOFConstructorWithOpts, } from './utils.js'; | ||
// Various per round constants calculations | ||
@@ -203,5 +203,5 @@ const [SHA3_PI, SHA3_ROTL, _SHA3_IOTA] = [[], [], []]; | ||
export const keccak_512 = gen(0x01, 72, 512 / 8); | ||
const genShake = (suffix, blockLen, outputLen) => wrapConstructorWithOpts((opts = {}) => new Keccak(blockLen, suffix, opts.dkLen === undefined ? outputLen : opts.dkLen, true)); | ||
const genShake = (suffix, blockLen, outputLen) => wrapXOFConstructorWithOpts((opts = {}) => new Keccak(blockLen, suffix, opts.dkLen === undefined ? outputLen : opts.dkLen, true)); | ||
export const shake128 = genShake(0x1f, 168, 128 / 8); | ||
export const shake256 = genShake(0x1f, 136, 256 / 8); | ||
//# sourceMappingURL=sha3.js.map |
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
// We use `globalThis.crypto`, but node.js versions earlier than v19 don't | ||
// declare it in global scope. For node.js, package.json#exports field mapping | ||
// rewrites import from `crypto` to `cryptoNode`, which imports native module. | ||
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+. | ||
// node.js versions earlier than v19 don't declare it in global scope. | ||
// For node.js, package.json#exports field mapping rewrites import | ||
// from `crypto` to `cryptoNode`, which imports native module. | ||
// Makes the utils un-importable in browsers without a bundler. | ||
// Once node.js 18 is deprecated, we can just drop the import. | ||
import { crypto } from '@noble/hashes/crypto'; | ||
const u8a = (a) => a instanceof Uint8Array; | ||
// Cast array to different type | ||
@@ -22,11 +24,11 @@ export const u8 = (arr) => new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength); | ||
/** | ||
* @example bytesToHex(Uint8Array.from([0xde, 0xad, 0xbe, 0xef])) // 'deadbeef' | ||
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123' | ||
*/ | ||
export function bytesToHex(uint8a) { | ||
export function bytesToHex(bytes) { | ||
if (!u8a(bytes)) | ||
throw new Error('Uint8Array expected'); | ||
// pre-caching improves the speed 6x | ||
if (!(uint8a instanceof Uint8Array)) | ||
throw new Error('Uint8Array expected'); | ||
let hex = ''; | ||
for (let i = 0; i < uint8a.length; i++) { | ||
hex += hexes[uint8a[i]]; | ||
for (let i = 0; i < bytes.length; i++) { | ||
hex += hexes[bytes[i]]; | ||
} | ||
@@ -36,11 +38,11 @@ return hex; | ||
/** | ||
* @example hexToBytes('deadbeef') // Uint8Array.from([0xde, 0xad, 0xbe, 0xef]) | ||
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23]) | ||
*/ | ||
export function hexToBytes(hex) { | ||
if (typeof hex !== 'string') { | ||
throw new TypeError('hexToBytes: expected string, got ' + typeof hex); | ||
} | ||
if (hex.length % 2) | ||
throw new Error('hexToBytes: received invalid unpadded hex'); | ||
const array = new Uint8Array(hex.length / 2); | ||
if (typeof hex !== 'string') | ||
throw new Error('hex string expected, got ' + typeof hex); | ||
const len = hex.length; | ||
if (len % 2) | ||
throw new Error('padded hex string expected, got unpadded hex of length ' + len); | ||
const array = new Uint8Array(len / 2); | ||
for (let i = 0; i < array.length; i++) { | ||
@@ -73,32 +75,35 @@ const j = i * 2; | ||
} | ||
/** | ||
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99]) | ||
*/ | ||
export function utf8ToBytes(str) { | ||
if (typeof str !== 'string') { | ||
throw new TypeError(`utf8ToBytes expected string, got ${typeof str}`); | ||
} | ||
return new TextEncoder().encode(str); | ||
if (typeof str !== 'string') | ||
throw new Error(`utf8ToBytes expected string, got ${typeof str}`); | ||
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809 | ||
} | ||
/** | ||
* Normalizes (non-hex) string or Uint8Array to Uint8Array. | ||
* Warning: when Uint8Array is passed, it would NOT get copied. | ||
* Keep in mind for future mutable operations. | ||
*/ | ||
export function toBytes(data) { | ||
if (typeof data === 'string') | ||
data = utf8ToBytes(data); | ||
if (!(data instanceof Uint8Array)) | ||
throw new TypeError(`Expected input type is Uint8Array (got ${typeof data})`); | ||
if (!u8a(data)) | ||
throw new Error(`expected Uint8Array, got ${typeof data}`); | ||
return data; | ||
} | ||
/** | ||
* Concats Uint8Array-s into one; like `Buffer.concat([buf1, buf2])` | ||
* @example concatBytes(buf1, buf2) | ||
* Copies several Uint8Arrays into one. | ||
*/ | ||
export function concatBytes(...arrays) { | ||
if (!arrays.every((a) => a instanceof Uint8Array)) | ||
throw new Error('Uint8Array list expected'); | ||
if (arrays.length === 1) | ||
return arrays[0]; | ||
const length = arrays.reduce((a, arr) => a + arr.length, 0); | ||
const result = new Uint8Array(length); | ||
for (let i = 0, pad = 0; i < arrays.length; i++) { | ||
const arr = arrays[i]; | ||
result.set(arr, pad); | ||
pad += arr.length; | ||
} | ||
return result; | ||
const r = new Uint8Array(arrays.reduce((sum, a) => sum + a.length, 0)); | ||
let pad = 0; // walk through each item, ensure they have proper type | ||
arrays.forEach((a) => { | ||
if (!u8a(a)) | ||
throw new Error('Uint8Array expected'); | ||
r.set(a, pad); | ||
pad += a.length; | ||
}); | ||
return r; | ||
} | ||
@@ -116,12 +121,12 @@ // For runtime check if class implements interface | ||
if (opts !== undefined && (typeof opts !== 'object' || !isPlainObject(opts))) | ||
throw new TypeError('Options should be object or undefined'); | ||
throw new Error('Options should be object or undefined'); | ||
const merged = Object.assign(defaults, opts); | ||
return merged; | ||
} | ||
export function wrapConstructor(hashConstructor) { | ||
const hashC = (message) => hashConstructor().update(toBytes(message)).digest(); | ||
const tmp = hashConstructor(); | ||
export function wrapConstructor(hashCons) { | ||
const hashC = (msg) => hashCons().update(toBytes(msg)).digest(); | ||
const tmp = hashCons(); | ||
hashC.outputLen = tmp.outputLen; | ||
hashC.blockLen = tmp.blockLen; | ||
hashC.create = () => hashConstructor(); | ||
hashC.create = () => hashCons(); | ||
return hashC; | ||
@@ -137,4 +142,12 @@ } | ||
} | ||
export function wrapXOFConstructorWithOpts(hashCons) { | ||
const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest(); | ||
const tmp = hashCons({}); | ||
hashC.outputLen = tmp.outputLen; | ||
hashC.blockLen = tmp.blockLen; | ||
hashC.create = (opts) => hashCons(opts); | ||
return hashC; | ||
} | ||
/** | ||
* Secure PRNG. Uses `globalThis.crypto` or node.js crypto module. | ||
* Secure PRNG. Uses `crypto.getRandomValues`, which defers to OS. | ||
*/ | ||
@@ -141,0 +154,0 @@ export function randomBytes(bytesLength = 32) { |
import { Hash, CHash, Input } from './utils.js'; | ||
declare class HMAC<T extends Hash<T>> extends Hash<HMAC<T>> { | ||
export declare class HMAC<T extends Hash<T>> extends Hash<HMAC<T>> { | ||
oHash: T; | ||
@@ -26,2 +26,1 @@ iHash: T; | ||
}; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.hmac = void 0; | ||
exports.hmac = exports.HMAC = void 0; | ||
const _assert_js_1 = require("./_assert.js"); | ||
@@ -16,3 +16,3 @@ const utils_js_1 = require("./utils.js"); | ||
if (typeof this.iHash.update !== 'function') | ||
throw new TypeError('Expected instance of class which extends utils.Hash'); | ||
throw new Error('Expected instance of class which extends utils.Hash'); | ||
this.blockLen = this.iHash.blockLen; | ||
@@ -73,2 +73,3 @@ this.outputLen = this.iHash.outputLen; | ||
} | ||
exports.HMAC = HMAC; | ||
/** | ||
@@ -75,0 +76,0 @@ * HMAC: RFC2104 message authentication code. |
{ | ||
"name": "@noble/hashes", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "Audited & minimal 0-dependency JS implementation of SHA2, SHA3, RIPEMD, BLAKE2/3, HMAC, HKDF, PBKDF2, Scrypt", | ||
@@ -13,5 +13,5 @@ "files": [ | ||
"scripts": { | ||
"bench": "node test/benchmark/index.js noble", | ||
"bench:all": "node test/benchmark/index.js", | ||
"bench:install": "cd test/benchmark && npm install && cd ../../", | ||
"bench": "node benchmark/index.js noble", | ||
"bench:all": "node benchmark/index.js", | ||
"bench:install": "cd benchmark && npm install && cd ../../", | ||
"build": "npm run build:clean; tsc && tsc -p tsconfig.esm.json", | ||
@@ -38,8 +38,10 @@ "build:release": "cd build; npm i; npm run build", | ||
"devDependencies": { | ||
"@types/node": "18.11.18", | ||
"micro-bmark": "0.3.0", | ||
"micro-bmark": "0.3.1", | ||
"micro-should": "0.4.0", | ||
"prettier": "2.8.3", | ||
"prettier": "2.8.4", | ||
"typescript": "5.0.2" | ||
}, | ||
"engines": { | ||
"node": ">= 16" | ||
}, | ||
"exports": { | ||
@@ -172,8 +174,3 @@ ".": { | ||
], | ||
"funding": [ | ||
{ | ||
"type": "individual", | ||
"url": "https://paulmillr.com/funding/" | ||
} | ||
] | ||
"funding": "https://paulmillr.com/funding/" | ||
} |
@@ -1,12 +0,10 @@ | ||
# noble-hashes ![Node CI](https://github.com/paulmillr/noble-hashes/workflows/Node%20CI/badge.svg) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) | ||
# noble-hashes | ||
Audited & minimal JS implementation of SHA2, SHA3, RIPEMD, BLAKE2/3, HMAC, HKDF, PBKDF2 & Scrypt. | ||
- **noble** family, zero dependencies | ||
- 🔒 [**Audited**](#security) by an independent security firm: no vulnerabilities have been found | ||
- 🔻 Tree-shaking-friendly: there is no entry point, which ensures small size of your app | ||
- 🔒 [**Audited**](#security) by an independent security firm | ||
- 🔻 Tree-shaking-friendly: use only what's necessary, other code won't be included | ||
- 🏎 Ultra-fast, hand-optimized for caveats of JS engines | ||
- 🔍 Unique tests ensure correctness: chained tests, sliding window tests, DoS tests, fuzzing | ||
- 🔁 No unrolled loops: makes it easier to verify and reduces source code size up to 5x | ||
- 🏎 Ultra-fast, hand-optimized for caveats of JS engines | ||
- 🔍 Unique tests ensure correctness: chained tests, sliding window tests, DoS tests | ||
- 🧪 Differential fuzzing ensures even more correctness with [cryptofuzz](https://github.com/guidovranken/cryptofuzz) | ||
- 🐢 Scrypt supports `N: 2**22`, while other implementations are limited to `2**20` | ||
@@ -22,4 +20,4 @@ - 🦘 SHA3 supports Keccak, TupleHash, KangarooTwelve and MarsupilamiFourteen | ||
- No dependencies | ||
- Easily auditable TypeScript/JS code | ||
- No dependencies, protection against supply chain attacks | ||
- Auditable TypeScript / JS code | ||
- Supported in all major browsers and stable node.js versions | ||
@@ -29,3 +27,3 @@ - All releases are signed with PGP keys | ||
[curves](https://github.com/paulmillr/noble-curves) | ||
([secp256k1](https://github.com/paulmillr/noble-secp256k1), | ||
(4kb versions [secp256k1](https://github.com/paulmillr/noble-secp256k1), | ||
[ed25519](https://github.com/paulmillr/noble-ed25519)), | ||
@@ -36,13 +34,12 @@ [hashes](https://github.com/paulmillr/noble-hashes) | ||
Browser, deno and node.js are supported: | ||
> npm install @noble/hashes | ||
For [Deno](https://deno.land), use it with npm specifier. | ||
In browser, you could also include the single file from | ||
[GitHub's releases page](https://github.com/paulmillr/noble-hashes/releases). | ||
We support all major platforms and runtimes. | ||
For [Deno](https://deno.land), ensure to use [npm specifier](https://deno.land/manual@v1.28.0/node/npm_specifiers). | ||
For React Native, you may need a [polyfill for getRandomValues](https://github.com/LinusU/react-native-get-random-values). | ||
If you don't like NPM, a standalone [noble-hashes.js](https://github.com/paulmillr/noble-hashes/releases) is also available. | ||
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. | ||
The library is tree-shaking-friendly and does not expose root entry point as | ||
`@noble/hashes`. Instead, you need to import specific primitives. | ||
This is done to ensure small size of your apps. | ||
@@ -383,9 +380,7 @@ ```js | ||
There's experimental argon2 RFC 9106 implementation. It may be removed at any time. | ||
Experimental Argon2 RFC 9106 implementation. It may be removed at any time. | ||
```ts | ||
import { argon2d, argon2i, argon2id } from '@noble/hashes/scrypt'; | ||
const password = 'password'; | ||
const salt = 'salt'; | ||
const result = argon2id(password, salt, { t: 2, m: 65536, p: 1 }); | ||
import { argon2d, argon2i, argon2id } from '@noble/hashes/argon2'; | ||
const result = argon2id('password', 'salt', { t: 2, m: 65536, p: 1 }); | ||
``` | ||
@@ -426,3 +421,3 @@ | ||
```typescript | ||
import { bytesToHex as toHex, randomBytes } from '@noble/hashes/scrypt'; | ||
import { bytesToHex as toHex, randomBytes } from '@noble/hashes/utils'; | ||
console.log(toHex(randomBytes(32))); | ||
@@ -484,13 +479,14 @@ ``` | ||
``` | ||
SHA256 32B x 1,126,126 ops/sec @ 888ns/op | ||
SHA384 32B x 463,606 ops/sec @ 2μs/op | ||
SHA512 32B x 467,945 ops/sec @ 2μs/op | ||
SHA3-256, keccak256, shake256 32B x 192,049 ops/sec @ 5μs/op | ||
Kangaroo12 32B x 318,066 ops/sec @ 3μs/op | ||
Marsupilami14 32B x 283,929 ops/sec @ 3μs/op | ||
BLAKE2b 32B x 352,112 ops/sec @ 2μs/op | ||
BLAKE2s 32B x 511,770 ops/sec @ 1μs/op | ||
BLAKE3 32B x 582,072 ops/sec @ 1μs/op | ||
RIPEMD160 32B x 1,230,012 ops/sec @ 813ns/op | ||
HMAC-SHA256 32B x 238,663 ops/sec @ 4μs/op | ||
SHA256 32B x 1,219,512 ops/sec @ 820ns/op ± 2.58% (min: 625ns, max: 4ms) | ||
SHA384 32B x 512,032 ops/sec @ 1μs/op | ||
SHA512 32B x 509,943 ops/sec @ 1μs/op | ||
SHA3-256, keccak256, shake256 32B x 199,600 ops/sec @ 5μs/op | ||
Kangaroo12 32B x 336,360 ops/sec @ 2μs/op | ||
Marsupilami14 32B x 298,418 ops/sec @ 3μs/op | ||
BLAKE2b 32B x 379,794 ops/sec @ 2μs/op | ||
BLAKE2s 32B x 515,995 ops/sec @ 1μs/op ± 1.07% (min: 1μs, max: 4ms) | ||
BLAKE3 32B x 588,235 ops/sec @ 1μs/op ± 1.36% (min: 1μs, max: 5ms) | ||
RIPEMD160 32B x 1,140,250 ops/sec @ 877ns/op ± 3.12% (min: 708ns, max: 6ms) | ||
HMAC-SHA256 32B x 377,358 ops/sec @ 2μs/op | ||
HKDF-SHA256 32B x 108,377 ops/sec @ 9μs/op | ||
@@ -505,19 +501,19 @@ PBKDF2-HMAC-SHA256 262144 x 3 ops/sec @ 326ms/op | ||
``` | ||
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 | ||
SHA256 32B node x 1,302,083 ops/sec @ 768ns/op ± 10.54% (min: 416ns, max: 7ms) | ||
SHA384 32B node x 975,609 ops/sec @ 1μs/op ± 11.32% (min: 625ns, max: 8ms) | ||
SHA512 32B node x 983,284 ops/sec @ 1μs/op ± 11.24% (min: 625ns, max: 8ms) | ||
SHA3-256 32B node x 910,746 ops/sec @ 1μs/op ± 12.19% (min: 666ns, max: 10ms) | ||
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 | ||
BLAKE2b 32B node x 967,117 ops/sec @ 1μs/op ± 11.26% (min: 625ns, max: 9ms) | ||
BLAKE2s 32B node x 1,055,966 ops/sec @ 947ns/op ± 11.07% (min: 583ns, max: 7ms) | ||
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 | ||
RIPEMD160 32B node x 1,002,004 ops/sec @ 998ns/op ± 10.66% (min: 625ns, max: 7ms) | ||
HMAC-SHA256 32B node x 919,963 ops/sec @ 1μs/op ± 6.13% (min: 833ns, max: 5ms) | ||
HKDF-SHA256 32 node x 369,276 ops/sec @ 2μs/op ± 13.59% (min: 1μs, max: 9ms) | ||
PBKDF2-HMAC-SHA256 262144 node x 25 ops/sec @ 39ms/op | ||
PBKDF2-HMAC-SHA512 262144 node x 7 ops/sec @ 132ms/op | ||
Scrypt r: 8, p: 1, n: 262144 node x 1 ops/sec @ 523ms/op | ||
``` | ||
It is possible to [make this library 4x+ faster](./test/benchmark/README.md) by | ||
It is possible to [make this library 4x+ faster](./benchmark/README.md) by | ||
_doing code generation of full loop unrolls_. We've decided against it. Reasons: | ||
@@ -524,0 +520,0 @@ |
@@ -20,3 +20,3 @@ import { SHA2 } from './_sha2.js'; | ||
export declare const ripemd160: { | ||
(message: import("./utils.js").Input): Uint8Array; | ||
(msg: import("./utils.js").Input): Uint8Array; | ||
outputLen: number; | ||
@@ -23,0 +23,0 @@ blockLen: number; |
@@ -16,3 +16,3 @@ import { SHA2 } from './_sha2.js'; | ||
export declare const sha1: { | ||
(message: import("./utils.js").Input): Uint8Array; | ||
(msg: import("./utils.js").Input): Uint8Array; | ||
outputLen: number; | ||
@@ -19,0 +19,0 @@ blockLen: number; |
@@ -23,3 +23,3 @@ import { SHA2 } from './_sha2.js'; | ||
export declare const sha256: { | ||
(message: import("./utils.js").Input): Uint8Array; | ||
(msg: import("./utils.js").Input): Uint8Array; | ||
outputLen: number; | ||
@@ -30,3 +30,3 @@ blockLen: number; | ||
export declare const sha224: { | ||
(message: import("./utils.js").Input): Uint8Array; | ||
(msg: import("./utils.js").Input): Uint8Array; | ||
outputLen: number; | ||
@@ -33,0 +33,0 @@ blockLen: number; |
@@ -50,4 +50,4 @@ "use strict"; | ||
const gencShake = (suffix, blockLen, outputLen) => (0, utils_js_1.wrapConstructorWithOpts)((opts = {}) => cshakePers(new sha3_js_1.Keccak(blockLen, suffix, chooseLen(opts, outputLen), true), opts)); | ||
exports.cshake128 = gencShake(0x1f, 168, 128 / 8); | ||
exports.cshake256 = gencShake(0x1f, 136, 256 / 8); | ||
exports.cshake128 = (() => gencShake(0x1f, 168, 128 / 8))(); | ||
exports.cshake256 = (() => gencShake(0x1f, 136, 256 / 8))(); | ||
class KMAC extends sha3_js_1.Keccak { | ||
@@ -90,6 +90,6 @@ constructor(blockLen, outputLen, enableXOF, key, opts = {}) { | ||
} | ||
exports.kmac128 = genKmac(168, 128 / 8); | ||
exports.kmac256 = genKmac(136, 256 / 8); | ||
exports.kmac128xof = genKmac(168, 128 / 8, true); | ||
exports.kmac256xof = genKmac(136, 256 / 8, true); | ||
exports.kmac128 = (() => genKmac(168, 128 / 8))(); | ||
exports.kmac256 = (() => genKmac(136, 256 / 8))(); | ||
exports.kmac128xof = (() => genKmac(168, 128 / 8, true))(); | ||
exports.kmac256xof = (() => genKmac(136, 256 / 8, true))(); | ||
// TupleHash | ||
@@ -132,6 +132,6 @@ // Usage: tuple(['ab', 'cd']) != tuple(['a', 'bcd']) | ||
} | ||
exports.tuplehash128 = genTuple(168, 128 / 8); | ||
exports.tuplehash256 = genTuple(136, 256 / 8); | ||
exports.tuplehash128xof = genTuple(168, 128 / 8, true); | ||
exports.tuplehash256xof = genTuple(136, 256 / 8, true); | ||
exports.tuplehash128 = (() => genTuple(168, 128 / 8))(); | ||
exports.tuplehash256 = (() => genTuple(136, 256 / 8))(); | ||
exports.tuplehash128xof = (() => genTuple(168, 128 / 8, true))(); | ||
exports.tuplehash256xof = (() => genTuple(136, 256 / 8, true))(); | ||
class ParallelHash extends sha3_js_1.Keccak { | ||
@@ -199,3 +199,3 @@ constructor(blockLen, outputLen, leafCons, enableXOF, opts = {}) { | ||
} | ||
function genParallel(blockLen, outputLen, leaf, xof = false) { | ||
function genPrl(blockLen, outputLen, leaf, xof = false) { | ||
const parallel = (message, opts) => parallel.create(opts).update(message).digest(); | ||
@@ -205,6 +205,6 @@ parallel.create = (opts = {}) => new ParallelHash(blockLen, chooseLen(opts, outputLen), () => leaf.create({ dkLen: 2 * outputLen }), xof, opts); | ||
} | ||
exports.parallelhash128 = genParallel(168, 128 / 8, exports.cshake128); | ||
exports.parallelhash256 = genParallel(136, 256 / 8, exports.cshake256); | ||
exports.parallelhash128xof = genParallel(168, 128 / 8, exports.cshake128, true); | ||
exports.parallelhash256xof = genParallel(136, 256 / 8, exports.cshake256, true); | ||
exports.parallelhash128 = (() => genPrl(168, 128 / 8, exports.cshake128))(); | ||
exports.parallelhash256 = (() => genPrl(136, 256 / 8, exports.cshake256))(); | ||
exports.parallelhash128xof = (() => genPrl(168, 128 / 8, exports.cshake128, true))(); | ||
exports.parallelhash256xof = (() => genPrl(136, 256 / 8, exports.cshake256, true))(); | ||
// Kangaroo | ||
@@ -293,5 +293,5 @@ // Same as NIST rightEncode, but returns [0] for zero string | ||
// Default to 32 bytes, so it can be used without opts | ||
exports.k12 = (0, utils_js_1.wrapConstructorWithOpts)((opts = {}) => new KangarooTwelve(168, 32, chooseLen(opts, 32), 12, opts)); | ||
exports.k12 = (() => (0, utils_js_1.wrapConstructorWithOpts)((opts = {}) => new KangarooTwelve(168, 32, chooseLen(opts, 32), 12, opts)))(); | ||
// MarsupilamiFourteen | ||
exports.m14 = (0, utils_js_1.wrapConstructorWithOpts)((opts = {}) => new KangarooTwelve(136, 64, chooseLen(opts, 64), 14, opts)); | ||
exports.m14 = (() => (0, utils_js_1.wrapConstructorWithOpts)((opts = {}) => new KangarooTwelve(136, 64, chooseLen(opts, 64), 14, opts)))(); | ||
// https://keccak.team/files/CSF-0.1.pdf | ||
@@ -298,0 +298,0 @@ // + https://github.com/XKCP/XKCP/tree/master/lib/high/Keccak/PRG |
@@ -28,3 +28,3 @@ import { Hash, Input, HashXOF } from './utils.js'; | ||
export declare const sha3_224: { | ||
(message: Input): Uint8Array; | ||
(msg: Input): Uint8Array; | ||
outputLen: number; | ||
@@ -39,3 +39,3 @@ blockLen: number; | ||
export declare const sha3_256: { | ||
(message: Input): Uint8Array; | ||
(msg: Input): Uint8Array; | ||
outputLen: number; | ||
@@ -46,3 +46,3 @@ blockLen: number; | ||
export declare const sha3_384: { | ||
(message: Input): Uint8Array; | ||
(msg: Input): Uint8Array; | ||
outputLen: number; | ||
@@ -53,3 +53,3 @@ blockLen: number; | ||
export declare const sha3_512: { | ||
(message: Input): Uint8Array; | ||
(msg: Input): Uint8Array; | ||
outputLen: number; | ||
@@ -60,3 +60,3 @@ blockLen: number; | ||
export declare const keccak_224: { | ||
(message: Input): Uint8Array; | ||
(msg: Input): Uint8Array; | ||
outputLen: number; | ||
@@ -71,3 +71,3 @@ blockLen: number; | ||
export declare const keccak_256: { | ||
(message: Input): Uint8Array; | ||
(msg: Input): Uint8Array; | ||
outputLen: number; | ||
@@ -78,3 +78,3 @@ blockLen: number; | ||
export declare const keccak_384: { | ||
(message: Input): Uint8Array; | ||
(msg: Input): Uint8Array; | ||
outputLen: number; | ||
@@ -85,3 +85,3 @@ blockLen: number; | ||
export declare const keccak_512: { | ||
(message: Input): Uint8Array; | ||
(msg: Input): Uint8Array; | ||
outputLen: number; | ||
@@ -98,3 +98,3 @@ blockLen: number; | ||
blockLen: number; | ||
create(opts: ShakeOpts): Hash<Keccak>; | ||
create(opts: ShakeOpts): HashXOF<HashXOF<Keccak>>; | ||
}; | ||
@@ -105,3 +105,3 @@ export declare const shake256: { | ||
blockLen: number; | ||
create(opts: ShakeOpts): Hash<Keccak>; | ||
create(opts: ShakeOpts): HashXOF<HashXOF<Keccak>>; | ||
}; |
@@ -208,5 +208,5 @@ "use strict"; | ||
exports.keccak_512 = gen(0x01, 72, 512 / 8); | ||
const genShake = (suffix, blockLen, outputLen) => (0, utils_js_1.wrapConstructorWithOpts)((opts = {}) => new Keccak(blockLen, suffix, opts.dkLen === undefined ? outputLen : opts.dkLen, true)); | ||
const genShake = (suffix, blockLen, outputLen) => (0, utils_js_1.wrapXOFConstructorWithOpts)((opts = {}) => new Keccak(blockLen, suffix, opts.dkLen === undefined ? outputLen : opts.dkLen, true)); | ||
exports.shake128 = genShake(0x1f, 168, 128 / 8); | ||
exports.shake256 = genShake(0x1f, 136, 256 / 8); | ||
//# sourceMappingURL=sha3.js.map |
@@ -44,3 +44,3 @@ import { SHA2 } from './_sha2.js'; | ||
export declare const sha512: { | ||
(message: import("./utils.js").Input): Uint8Array; | ||
(msg: import("./utils.js").Input): Uint8Array; | ||
outputLen: number; | ||
@@ -51,3 +51,3 @@ blockLen: number; | ||
export declare const sha512_224: { | ||
(message: import("./utils.js").Input): Uint8Array; | ||
(msg: import("./utils.js").Input): Uint8Array; | ||
outputLen: number; | ||
@@ -58,3 +58,3 @@ blockLen: number; | ||
export declare const sha512_256: { | ||
(message: import("./utils.js").Input): Uint8Array; | ||
(msg: import("./utils.js").Input): Uint8Array; | ||
outputLen: number; | ||
@@ -65,3 +65,3 @@ blockLen: number; | ||
export declare const sha384: { | ||
(message: import("./utils.js").Input): Uint8Array; | ||
(msg: import("./utils.js").Input): Uint8Array; | ||
outputLen: number; | ||
@@ -68,0 +68,0 @@ blockLen: number; |
@@ -10,5 +10,5 @@ export function number(n: number) { | ||
export function bytes(b: Uint8Array | undefined, ...lengths: number[]) { | ||
if (!(b instanceof Uint8Array)) throw new TypeError('Expected Uint8Array'); | ||
if (!(b instanceof Uint8Array)) throw new Error('Expected Uint8Array'); | ||
if (lengths.length > 0 && !lengths.includes(b.length)) | ||
throw new TypeError(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`); | ||
throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`); | ||
} | ||
@@ -15,0 +15,0 @@ |
@@ -69,2 +69,4 @@ import assert from './_assert.js'; | ||
const len = data.length; | ||
const offset = data.byteOffset; | ||
const buf = data.buffer; | ||
for (let pos = 0; pos < len; ) { | ||
@@ -77,6 +79,6 @@ // If buffer is full and we still have input (don't process last block, same as blake2s) | ||
const take = Math.min(blockLen - this.pos, len - pos); | ||
const dataOffset = data.byteOffset + pos; | ||
const dataOffset = offset + pos; | ||
// full block && aligned to 4 bytes && not last in input | ||
if (take === blockLen && !(dataOffset % 4) && pos + take < len) { | ||
const data32 = new Uint32Array(data.buffer, dataOffset, Math.floor((len - pos) / 4)); | ||
const data32 = new Uint32Array(buf, dataOffset, Math.floor((len - pos) / 4)); | ||
for (let pos32 = 0; pos + blockLen < len; pos32 += buffer32.length, pos += blockLen) { | ||
@@ -83,0 +85,0 @@ this.length += blockLen; |
@@ -6,6 +6,3 @@ import assert from './_assert.js'; | ||
// Experimental implementation of argon2. | ||
// Could be broken & slow. May be removed at a later time. | ||
// RFC 9106 | ||
// Experimental Argon2 RFC 9106 implementation. It may be removed at any time. | ||
enum Types { | ||
@@ -12,0 +9,0 @@ Argond2d = 0, |
@@ -5,3 +5,3 @@ import assert from './_assert.js'; | ||
import { compress, IV } from './blake2s.js'; | ||
import { Input, u8, u32, toBytes, wrapConstructorWithOpts, HashXOF } from './utils.js'; | ||
import { Input, u8, u32, toBytes, HashXOF, wrapXOFConstructorWithOpts } from './utils.js'; | ||
@@ -63,3 +63,3 @@ // Flag bitset | ||
else if (opts.key !== undefined) { | ||
const key = toBytes(opts.key); | ||
const key = toBytes(opts.key).slice(); | ||
if (key.length !== 32) throw new Error('Blake3: key should be 32 byte'); | ||
@@ -248,2 +248,2 @@ this.IV = u32(key); | ||
*/ | ||
export const blake3 = wrapConstructorWithOpts<BLAKE3, Blake3Opts>((opts) => new BLAKE3(opts)); | ||
export const blake3 = wrapXOFConstructorWithOpts<BLAKE3, Blake3Opts>((opts) => new BLAKE3(opts)); |
@@ -0,3 +1,5 @@ | ||
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+. | ||
// See utils.ts for details. | ||
declare const globalThis: Record<string, any> | undefined; | ||
export const crypto = | ||
typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined; |
@@ -0,3 +1,7 @@ | ||
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+. | ||
// See utils.ts for details. | ||
// The file will throw on node.js 14 and earlier. | ||
// @ts-ignore | ||
import * as nc from 'node:crypto'; | ||
export const crypto = | ||
nc && typeof nc === 'object' && 'webcrypto' in nc ? (nc.webcrypto as any) : undefined; |
import assert from './_assert.js'; | ||
import { Hash, CHash, Input, toBytes } from './utils.js'; | ||
// HMAC (RFC 2104) | ||
class HMAC<T extends Hash<T>> extends Hash<HMAC<T>> { | ||
export class HMAC<T extends Hash<T>> extends Hash<HMAC<T>> { | ||
oHash: T; | ||
@@ -18,3 +18,3 @@ iHash: T; | ||
if (typeof this.iHash.update !== 'function') | ||
throw new TypeError('Expected instance of class which extends utils.Hash'); | ||
throw new Error('Expected instance of class which extends utils.Hash'); | ||
this.blockLen = this.iHash.blockLen; | ||
@@ -21,0 +21,0 @@ this.outputLen = this.iHash.outputLen; |
@@ -53,4 +53,4 @@ import { number as assertNumber } from './_assert.js'; | ||
export const cshake128 = gencShake(0x1f, 168, 128 / 8); | ||
export const cshake256 = gencShake(0x1f, 136, 256 / 8); | ||
export const cshake128 = /* @__PURE__ */ (() => gencShake(0x1f, 168, 128 / 8))(); | ||
export const cshake256 = /* @__PURE__ */ (() => gencShake(0x1f, 136, 256 / 8))(); | ||
@@ -103,6 +103,6 @@ class KMAC extends Keccak implements HashXOF<KMAC> { | ||
export const kmac128 = genKmac(168, 128 / 8); | ||
export const kmac256 = genKmac(136, 256 / 8); | ||
export const kmac128xof = genKmac(168, 128 / 8, true); | ||
export const kmac256xof = genKmac(136, 256 / 8, true); | ||
export const kmac128 = /* @__PURE__ */ (() => genKmac(168, 128 / 8))(); | ||
export const kmac256 = /* @__PURE__ */ (() => genKmac(136, 256 / 8))(); | ||
export const kmac128xof = /* @__PURE__ */ (() => genKmac(168, 128 / 8, true))(); | ||
export const kmac256xof = /* @__PURE__ */ (() => genKmac(136, 256 / 8, true))(); | ||
@@ -147,6 +147,6 @@ // TupleHash | ||
export const tuplehash128 = genTuple(168, 128 / 8); | ||
export const tuplehash256 = genTuple(136, 256 / 8); | ||
export const tuplehash128xof = genTuple(168, 128 / 8, true); | ||
export const tuplehash256xof = genTuple(136, 256 / 8, true); | ||
export const tuplehash128 = /* @__PURE__ */ (() => genTuple(168, 128 / 8))(); | ||
export const tuplehash256 = /* @__PURE__ */ (() => genTuple(136, 256 / 8))(); | ||
export const tuplehash128xof = /* @__PURE__ */ (() => genTuple(168, 128 / 8, true))(); | ||
export const tuplehash256xof = /* @__PURE__ */ (() => genTuple(136, 256 / 8, true))(); | ||
@@ -223,3 +223,3 @@ // ParallelHash (same as K12/M14, but without speedup for inputs less 8kb, reduced number of rounds and more simple) | ||
function genParallel( | ||
function genPrl( | ||
blockLen: number, | ||
@@ -243,6 +243,6 @@ outputLen: number, | ||
export const parallelhash128 = genParallel(168, 128 / 8, cshake128); | ||
export const parallelhash256 = genParallel(136, 256 / 8, cshake256); | ||
export const parallelhash128xof = genParallel(168, 128 / 8, cshake128, true); | ||
export const parallelhash256xof = genParallel(136, 256 / 8, cshake256, true); | ||
export const parallelhash128 = /* @__PURE__ */ (() => genPrl(168, 128 / 8, cshake128))(); | ||
export const parallelhash256 = /* @__PURE__ */ (() => genPrl(136, 256 / 8, cshake256))(); | ||
export const parallelhash128xof = /* @__PURE__ */ (() => genPrl(168, 128 / 8, cshake128, true))(); | ||
export const parallelhash256xof = /* @__PURE__ */ (() => genPrl(136, 256 / 8, cshake256, true))(); | ||
@@ -335,9 +335,11 @@ // Kangaroo | ||
// Default to 32 bytes, so it can be used without opts | ||
export const k12 = wrapConstructorWithOpts<KangarooTwelve, KangarooOpts>( | ||
(opts: KangarooOpts = {}) => new KangarooTwelve(168, 32, chooseLen(opts, 32), 12, opts) | ||
); | ||
export const k12 = /* @__PURE__ */ (() => | ||
wrapConstructorWithOpts<KangarooTwelve, KangarooOpts>( | ||
(opts: KangarooOpts = {}) => new KangarooTwelve(168, 32, chooseLen(opts, 32), 12, opts) | ||
))(); | ||
// MarsupilamiFourteen | ||
export const m14 = wrapConstructorWithOpts<KangarooTwelve, KangarooOpts>( | ||
(opts: KangarooOpts = {}) => new KangarooTwelve(136, 64, chooseLen(opts, 64), 14, opts) | ||
); | ||
export const m14 = /* @__PURE__ */ (() => | ||
wrapConstructorWithOpts<KangarooTwelve, KangarooOpts>( | ||
(opts: KangarooOpts = {}) => new KangarooTwelve(136, 64, chooseLen(opts, 64), 14, opts) | ||
))(); | ||
@@ -344,0 +346,0 @@ // https://keccak.team/files/CSF-0.1.pdf |
@@ -9,3 +9,3 @@ import assert from './_assert.js'; | ||
wrapConstructor, | ||
wrapConstructorWithOpts, | ||
wrapXOFConstructorWithOpts, | ||
HashXOF, | ||
@@ -217,3 +217,3 @@ } from './utils.js'; | ||
const genShake = (suffix: number, blockLen: number, outputLen: number) => | ||
wrapConstructorWithOpts<Keccak, ShakeOpts>( | ||
wrapXOFConstructorWithOpts<HashXOF<Keccak>, ShakeOpts>( | ||
(opts: ShakeOpts = {}) => | ||
@@ -220,0 +220,0 @@ new Keccak(blockLen, suffix, opts.dkLen === undefined ? outputLen : opts.dkLen, true) |
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
// We use `globalThis.crypto`, but node.js versions earlier than v19 don't | ||
// declare it in global scope. For node.js, package.json#exports field mapping | ||
// rewrites import from `crypto` to `cryptoNode`, which imports native module. | ||
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+. | ||
// node.js versions earlier than v19 don't declare it in global scope. | ||
// For node.js, package.json#exports field mapping rewrites import | ||
// from `crypto` to `cryptoNode`, which imports native module. | ||
// Makes the utils un-importable in browsers without a bundler. | ||
@@ -14,2 +15,3 @@ // Once node.js 18 is deprecated, we can just drop the import. | ||
const u8a = (a: any): a is Uint8Array => a instanceof Uint8Array; | ||
// Cast array to different type | ||
@@ -34,10 +36,10 @@ export const u8 = (arr: TypedArray) => new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength); | ||
/** | ||
* @example bytesToHex(Uint8Array.from([0xde, 0xad, 0xbe, 0xef])) // 'deadbeef' | ||
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123' | ||
*/ | ||
export function bytesToHex(uint8a: Uint8Array): string { | ||
export function bytesToHex(bytes: Uint8Array): string { | ||
if (!u8a(bytes)) throw new Error('Uint8Array expected'); | ||
// pre-caching improves the speed 6x | ||
if (!(uint8a instanceof Uint8Array)) throw new Error('Uint8Array expected'); | ||
let hex = ''; | ||
for (let i = 0; i < uint8a.length; i++) { | ||
hex += hexes[uint8a[i]]; | ||
for (let i = 0; i < bytes.length; i++) { | ||
hex += hexes[bytes[i]]; | ||
} | ||
@@ -48,10 +50,9 @@ return hex; | ||
/** | ||
* @example hexToBytes('deadbeef') // Uint8Array.from([0xde, 0xad, 0xbe, 0xef]) | ||
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23]) | ||
*/ | ||
export function hexToBytes(hex: string): Uint8Array { | ||
if (typeof hex !== 'string') { | ||
throw new TypeError('hexToBytes: expected string, got ' + typeof hex); | ||
} | ||
if (hex.length % 2) throw new Error('hexToBytes: received invalid unpadded hex'); | ||
const array = new Uint8Array(hex.length / 2); | ||
if (typeof hex !== 'string') throw new Error('hex string expected, got ' + typeof hex); | ||
const len = hex.length; | ||
if (len % 2) throw new Error('padded hex string expected, got unpadded hex of length ' + len); | ||
const array = new Uint8Array(len / 2); | ||
for (let i = 0; i < array.length; i++) { | ||
@@ -88,16 +89,20 @@ const j = i * 2; | ||
declare const TextEncoder: any; | ||
declare const TextDecoder: any; | ||
/** | ||
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99]) | ||
*/ | ||
export function utf8ToBytes(str: string): Uint8Array { | ||
if (typeof str !== 'string') { | ||
throw new TypeError(`utf8ToBytes expected string, got ${typeof str}`); | ||
} | ||
return new TextEncoder().encode(str); | ||
if (typeof str !== 'string') throw new Error(`utf8ToBytes expected string, got ${typeof str}`); | ||
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809 | ||
} | ||
export type Input = Uint8Array | string; | ||
/** | ||
* Normalizes (non-hex) string or Uint8Array to Uint8Array. | ||
* Warning: when Uint8Array is passed, it would NOT get copied. | ||
* Keep in mind for future mutable operations. | ||
*/ | ||
export function toBytes(data: Input): Uint8Array { | ||
if (typeof data === 'string') data = utf8ToBytes(data); | ||
if (!(data instanceof Uint8Array)) | ||
throw new TypeError(`Expected input type is Uint8Array (got ${typeof data})`); | ||
if (!u8a(data)) throw new Error(`expected Uint8Array, got ${typeof data}`); | ||
return data; | ||
@@ -107,16 +112,13 @@ } | ||
/** | ||
* Concats Uint8Array-s into one; like `Buffer.concat([buf1, buf2])` | ||
* @example concatBytes(buf1, buf2) | ||
* Copies several Uint8Arrays into one. | ||
*/ | ||
export function concatBytes(...arrays: Uint8Array[]): Uint8Array { | ||
if (!arrays.every((a) => a instanceof Uint8Array)) throw new Error('Uint8Array list expected'); | ||
if (arrays.length === 1) return arrays[0]; | ||
const length = arrays.reduce((a, arr) => a + arr.length, 0); | ||
const result = new Uint8Array(length); | ||
for (let i = 0, pad = 0; i < arrays.length; i++) { | ||
const arr = arrays[i]; | ||
result.set(arr, pad); | ||
pad += arr.length; | ||
} | ||
return result; | ||
const r = new Uint8Array(arrays.reduce((sum, a) => sum + a.length, 0)); | ||
let pad = 0; // walk through each item, ensure they have proper type | ||
arrays.forEach((a) => { | ||
if (!u8a(a)) throw new Error('Uint8Array expected'); | ||
r.set(a, pad); | ||
pad += a.length; | ||
}); | ||
return r; | ||
} | ||
@@ -173,3 +175,3 @@ | ||
if (opts !== undefined && (typeof opts !== 'object' || !isPlainObject(opts))) | ||
throw new TypeError('Options should be object or undefined'); | ||
throw new Error('Options should be object or undefined'); | ||
const merged = Object.assign(defaults, opts); | ||
@@ -181,8 +183,8 @@ return merged as T1 & T2; | ||
export function wrapConstructor<T extends Hash<T>>(hashConstructor: () => Hash<T>) { | ||
const hashC = (message: Input): Uint8Array => hashConstructor().update(toBytes(message)).digest(); | ||
const tmp = hashConstructor(); | ||
export function wrapConstructor<T extends Hash<T>>(hashCons: () => Hash<T>) { | ||
const hashC = (msg: Input): Uint8Array => hashCons().update(toBytes(msg)).digest(); | ||
const tmp = hashCons(); | ||
hashC.outputLen = tmp.outputLen; | ||
hashC.blockLen = tmp.blockLen; | ||
hashC.create = () => hashConstructor(); | ||
hashC.create = () => hashCons(); | ||
return hashC; | ||
@@ -202,4 +204,15 @@ } | ||
export function wrapXOFConstructorWithOpts<H extends HashXOF<H>, T extends Object>( | ||
hashCons: (opts?: T) => HashXOF<H> | ||
) { | ||
const hashC = (msg: Input, opts?: T): Uint8Array => hashCons(opts).update(toBytes(msg)).digest(); | ||
const tmp = hashCons({} as T); | ||
hashC.outputLen = tmp.outputLen; | ||
hashC.blockLen = tmp.blockLen; | ||
hashC.create = (opts: T) => hashCons(opts); | ||
return hashC; | ||
} | ||
/** | ||
* Secure PRNG. Uses `globalThis.crypto` or node.js crypto module. | ||
* Secure PRNG. Uses `crypto.getRandomValues`, which defers to OS. | ||
*/ | ||
@@ -206,0 +219,0 @@ export function randomBytes(bytesLength = 32): Uint8Array { |
@@ -9,7 +9,7 @@ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
/** | ||
* @example bytesToHex(Uint8Array.from([0xde, 0xad, 0xbe, 0xef])) // 'deadbeef' | ||
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123' | ||
*/ | ||
export declare function bytesToHex(uint8a: Uint8Array): string; | ||
export declare function bytesToHex(bytes: Uint8Array): string; | ||
/** | ||
* @example hexToBytes('deadbeef') // Uint8Array.from([0xde, 0xad, 0xbe, 0xef]) | ||
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23]) | ||
*/ | ||
@@ -19,8 +19,15 @@ export declare function hexToBytes(hex: string): Uint8Array; | ||
export declare function asyncLoop(iters: number, tick: number, cb: (i: number) => void): Promise<void>; | ||
/** | ||
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99]) | ||
*/ | ||
export declare function utf8ToBytes(str: string): Uint8Array; | ||
export type Input = Uint8Array | string; | ||
/** | ||
* Normalizes (non-hex) string or Uint8Array to Uint8Array. | ||
* Warning: when Uint8Array is passed, it would NOT get copied. | ||
* Keep in mind for future mutable operations. | ||
*/ | ||
export declare function toBytes(data: Input): Uint8Array; | ||
/** | ||
* Concats Uint8Array-s into one; like `Buffer.concat([buf1, buf2])` | ||
* @example concatBytes(buf1, buf2) | ||
* Copies several Uint8Arrays into one. | ||
*/ | ||
@@ -63,4 +70,4 @@ export declare function concatBytes(...arrays: Uint8Array[]): Uint8Array; | ||
export type CHash = ReturnType<typeof wrapConstructor>; | ||
export declare function wrapConstructor<T extends Hash<T>>(hashConstructor: () => Hash<T>): { | ||
(message: Input): Uint8Array; | ||
export declare function wrapConstructor<T extends Hash<T>>(hashCons: () => Hash<T>): { | ||
(msg: Input): Uint8Array; | ||
outputLen: number; | ||
@@ -76,6 +83,12 @@ blockLen: number; | ||
}; | ||
export declare function wrapXOFConstructorWithOpts<H extends HashXOF<H>, T extends Object>(hashCons: (opts?: T) => HashXOF<H>): { | ||
(msg: Input, opts?: T): Uint8Array; | ||
outputLen: number; | ||
blockLen: number; | ||
create(opts: T): HashXOF<H>; | ||
}; | ||
/** | ||
* Secure PRNG. Uses `globalThis.crypto` or node.js crypto module. | ||
* Secure PRNG. Uses `crypto.getRandomValues`, which defers to OS. | ||
*/ | ||
export declare function randomBytes(bytesLength?: number): Uint8Array; | ||
export {}; |
100
utils.js
"use strict"; | ||
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.randomBytes = exports.wrapConstructorWithOpts = exports.wrapConstructor = exports.checkOpts = exports.Hash = exports.concatBytes = exports.toBytes = exports.utf8ToBytes = exports.asyncLoop = exports.nextTick = exports.hexToBytes = exports.bytesToHex = exports.isLE = exports.rotr = exports.createView = exports.u32 = exports.u8 = void 0; | ||
// We use `globalThis.crypto`, but node.js versions earlier than v19 don't | ||
// declare it in global scope. For node.js, package.json#exports field mapping | ||
// rewrites import from `crypto` to `cryptoNode`, which imports native module. | ||
exports.randomBytes = exports.wrapXOFConstructorWithOpts = exports.wrapConstructorWithOpts = exports.wrapConstructor = exports.checkOpts = exports.Hash = exports.concatBytes = exports.toBytes = exports.utf8ToBytes = exports.asyncLoop = exports.nextTick = exports.hexToBytes = exports.bytesToHex = exports.isLE = exports.rotr = exports.createView = exports.u32 = exports.u8 = void 0; | ||
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+. | ||
// node.js versions earlier than v19 don't declare it in global scope. | ||
// For node.js, package.json#exports field mapping rewrites import | ||
// from `crypto` to `cryptoNode`, which imports native module. | ||
// Makes the utils un-importable in browsers without a bundler. | ||
// Once node.js 18 is deprecated, we can just drop the import. | ||
const crypto_1 = require("@noble/hashes/crypto"); | ||
const u8a = (a) => a instanceof Uint8Array; | ||
// Cast array to different type | ||
@@ -29,11 +31,11 @@ const u8 = (arr) => new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength); | ||
/** | ||
* @example bytesToHex(Uint8Array.from([0xde, 0xad, 0xbe, 0xef])) // 'deadbeef' | ||
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123' | ||
*/ | ||
function bytesToHex(uint8a) { | ||
function bytesToHex(bytes) { | ||
if (!u8a(bytes)) | ||
throw new Error('Uint8Array expected'); | ||
// pre-caching improves the speed 6x | ||
if (!(uint8a instanceof Uint8Array)) | ||
throw new Error('Uint8Array expected'); | ||
let hex = ''; | ||
for (let i = 0; i < uint8a.length; i++) { | ||
hex += hexes[uint8a[i]]; | ||
for (let i = 0; i < bytes.length; i++) { | ||
hex += hexes[bytes[i]]; | ||
} | ||
@@ -44,11 +46,11 @@ return hex; | ||
/** | ||
* @example hexToBytes('deadbeef') // Uint8Array.from([0xde, 0xad, 0xbe, 0xef]) | ||
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23]) | ||
*/ | ||
function hexToBytes(hex) { | ||
if (typeof hex !== 'string') { | ||
throw new TypeError('hexToBytes: expected string, got ' + typeof hex); | ||
} | ||
if (hex.length % 2) | ||
throw new Error('hexToBytes: received invalid unpadded hex'); | ||
const array = new Uint8Array(hex.length / 2); | ||
if (typeof hex !== 'string') | ||
throw new Error('hex string expected, got ' + typeof hex); | ||
const len = hex.length; | ||
if (len % 2) | ||
throw new Error('padded hex string expected, got unpadded hex of length ' + len); | ||
const array = new Uint8Array(len / 2); | ||
for (let i = 0; i < array.length; i++) { | ||
@@ -84,14 +86,21 @@ const j = i * 2; | ||
exports.asyncLoop = asyncLoop; | ||
/** | ||
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99]) | ||
*/ | ||
function utf8ToBytes(str) { | ||
if (typeof str !== 'string') { | ||
throw new TypeError(`utf8ToBytes expected string, got ${typeof str}`); | ||
} | ||
return new TextEncoder().encode(str); | ||
if (typeof str !== 'string') | ||
throw new Error(`utf8ToBytes expected string, got ${typeof str}`); | ||
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809 | ||
} | ||
exports.utf8ToBytes = utf8ToBytes; | ||
/** | ||
* Normalizes (non-hex) string or Uint8Array to Uint8Array. | ||
* Warning: when Uint8Array is passed, it would NOT get copied. | ||
* Keep in mind for future mutable operations. | ||
*/ | ||
function toBytes(data) { | ||
if (typeof data === 'string') | ||
data = utf8ToBytes(data); | ||
if (!(data instanceof Uint8Array)) | ||
throw new TypeError(`Expected input type is Uint8Array (got ${typeof data})`); | ||
if (!u8a(data)) | ||
throw new Error(`expected Uint8Array, got ${typeof data}`); | ||
return data; | ||
@@ -101,18 +110,14 @@ } | ||
/** | ||
* Concats Uint8Array-s into one; like `Buffer.concat([buf1, buf2])` | ||
* @example concatBytes(buf1, buf2) | ||
* Copies several Uint8Arrays into one. | ||
*/ | ||
function concatBytes(...arrays) { | ||
if (!arrays.every((a) => a instanceof Uint8Array)) | ||
throw new Error('Uint8Array list expected'); | ||
if (arrays.length === 1) | ||
return arrays[0]; | ||
const length = arrays.reduce((a, arr) => a + arr.length, 0); | ||
const result = new Uint8Array(length); | ||
for (let i = 0, pad = 0; i < arrays.length; i++) { | ||
const arr = arrays[i]; | ||
result.set(arr, pad); | ||
pad += arr.length; | ||
} | ||
return result; | ||
const r = new Uint8Array(arrays.reduce((sum, a) => sum + a.length, 0)); | ||
let pad = 0; // walk through each item, ensure they have proper type | ||
arrays.forEach((a) => { | ||
if (!u8a(a)) | ||
throw new Error('Uint8Array expected'); | ||
r.set(a, pad); | ||
pad += a.length; | ||
}); | ||
return r; | ||
} | ||
@@ -132,3 +137,3 @@ exports.concatBytes = concatBytes; | ||
if (opts !== undefined && (typeof opts !== 'object' || !isPlainObject(opts))) | ||
throw new TypeError('Options should be object or undefined'); | ||
throw new Error('Options should be object or undefined'); | ||
const merged = Object.assign(defaults, opts); | ||
@@ -138,8 +143,8 @@ return merged; | ||
exports.checkOpts = checkOpts; | ||
function wrapConstructor(hashConstructor) { | ||
const hashC = (message) => hashConstructor().update(toBytes(message)).digest(); | ||
const tmp = hashConstructor(); | ||
function wrapConstructor(hashCons) { | ||
const hashC = (msg) => hashCons().update(toBytes(msg)).digest(); | ||
const tmp = hashCons(); | ||
hashC.outputLen = tmp.outputLen; | ||
hashC.blockLen = tmp.blockLen; | ||
hashC.create = () => hashConstructor(); | ||
hashC.create = () => hashCons(); | ||
return hashC; | ||
@@ -157,4 +162,13 @@ } | ||
exports.wrapConstructorWithOpts = wrapConstructorWithOpts; | ||
function wrapXOFConstructorWithOpts(hashCons) { | ||
const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest(); | ||
const tmp = hashCons({}); | ||
hashC.outputLen = tmp.outputLen; | ||
hashC.blockLen = tmp.blockLen; | ||
hashC.create = (opts) => hashCons(opts); | ||
return hashC; | ||
} | ||
exports.wrapXOFConstructorWithOpts = wrapXOFConstructorWithOpts; | ||
/** | ||
* Secure PRNG. Uses `globalThis.crypto` or node.js crypto module. | ||
* Secure PRNG. Uses `crypto.getRandomValues`, which defers to OS. | ||
*/ | ||
@@ -161,0 +175,0 @@ function randomBytes(bytesLength = 32) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
742996
4
10277
537