@noble/hashes
Advanced tools
Comparing version 0.5.6 to 0.5.7
@@ -48,2 +48,3 @@ // prettier-ignore | ||
T.fill(0); | ||
HKDF_COUNTER.fill(0); | ||
return okm.slice(0, length); | ||
@@ -50,0 +51,0 @@ } |
@@ -5,4 +5,4 @@ { | ||
"crypto": false, | ||
"./crypto": "./esm/cryptoBrowser.js", | ||
"./crypto": "./esm/cryptoBrowser.js" | ||
} | ||
} |
@@ -26,2 +26,24 @@ /*! noble-hashes - MIT License (c) 2021 Paul Miller (paulmillr.com) */ | ||
} | ||
function parseHexByte(hexByte) { | ||
if (hexByte.length !== 2) | ||
throw new Error('Invalid byte sequence'); | ||
const byte = Number.parseInt(hexByte, 16); | ||
if (Number.isNaN(byte)) | ||
throw new Error('Invalid byte sequence'); | ||
return byte; | ||
} | ||
// Buffer.from(hex, 'hex') -> hexToBytes(hex) | ||
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); | ||
for (let i = 0; i < array.length; i++) { | ||
const j = i * 2; | ||
array[i] = parseHexByte(hex.slice(j, j + 2)); | ||
} | ||
return array; | ||
} | ||
// Currently avoid insertion of polyfills with packers (browserify/webpack/etc) | ||
@@ -55,5 +77,11 @@ // But setTimeout is pretty slow, maybe worth to investigate howto do minimal polyfill here | ||
} | ||
export function utf8ToBytes(str) { | ||
if (typeof str !== 'string') { | ||
throw new TypeError(`utf8ToBytes expected string, got ${typeof str}`); | ||
} | ||
return new TextEncoder().encode(str); | ||
} | ||
export function toBytes(data) { | ||
if (typeof data === 'string') | ||
data = new TextEncoder().encode(data); | ||
data = utf8ToBytes(data); | ||
if (!(data instanceof Uint8Array)) | ||
@@ -63,5 +91,19 @@ throw new TypeError(`Expected input type is Uint8Array (got ${typeof data})`); | ||
} | ||
// Buffer.concat([buf1, buf2]) -> concatBytes(buf1, buf2) | ||
export function concatBytes(...arrays) { | ||
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; | ||
} | ||
export function assertNumber(n) { | ||
if (!Number.isSafeInteger(n)) | ||
throw new Error(`Wrong integer: ${n}`); | ||
if (!Number.isSafeInteger(n) || n < 0) | ||
throw new Error(`Wrong positive integer: ${n}`); | ||
} | ||
@@ -74,4 +116,3 @@ export function assertBool(b) { | ||
export function assertBytes(bytes, ...lengths) { | ||
if (bytes instanceof Uint8Array && | ||
(!lengths.length || lengths.includes(bytes.length))) { | ||
if (bytes instanceof Uint8Array && (!lengths.length || lengths.includes(bytes.length))) { | ||
return; | ||
@@ -78,0 +119,0 @@ } |
@@ -52,2 +52,3 @@ "use strict"; | ||
T.fill(0); | ||
HKDF_COUNTER.fill(0); | ||
return okm.slice(0, length); | ||
@@ -54,0 +55,0 @@ } |
{ | ||
"name": "@noble/hashes", | ||
"version": "0.5.6", | ||
"version": "0.5.7", | ||
"description": "Fast 0-dependency JS implementation of SHA2, SHA3, RIPEMD, BLAKE2/3, HMAC, HKDF, PBKDF2, Scrypt", | ||
@@ -5,0 +5,0 @@ "directories": { |
@@ -9,6 +9,9 @@ /*! noble-hashes - MIT License (c) 2021 Paul Miller (paulmillr.com) */ | ||
export declare function bytesToHex(uint8a: Uint8Array): string; | ||
export declare function hexToBytes(hex: string): Uint8Array; | ||
export declare const nextTick: () => Promise<unknown>; | ||
export declare function asyncLoop(iters: number, tick: number, cb: (i: number) => void): Promise<void>; | ||
export declare function utf8ToBytes(str: string): Uint8Array; | ||
export declare type Input = Uint8Array | string; | ||
export declare function toBytes(data: Input): Uint8Array; | ||
export declare function concatBytes(...arrays: Uint8Array[]): Uint8Array; | ||
export declare function assertNumber(n: number): void; | ||
@@ -15,0 +18,0 @@ export declare function assertBool(b: boolean): void; |
56
utils.js
"use strict"; | ||
/*! noble-hashes - MIT License (c) 2021 Paul Miller (paulmillr.com) */ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.randomBytes = exports.wrapConstructorWithOpts = exports.wrapConstructor = exports.checkOpts = exports.Hash = exports.assertHash = exports.assertBytes = exports.assertBool = exports.assertNumber = exports.toBytes = exports.asyncLoop = exports.nextTick = exports.bytesToHex = exports.isLE = exports.rotr = exports.createView = exports.u32 = exports.u8 = void 0; | ||
exports.randomBytes = exports.wrapConstructorWithOpts = exports.wrapConstructor = exports.checkOpts = exports.Hash = exports.assertHash = exports.assertBytes = exports.assertBool = exports.assertNumber = 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; | ||
// The import here is via the package name. This is to ensure | ||
@@ -34,2 +34,25 @@ // that exports mapping/resolution does fall into place. | ||
exports.bytesToHex = bytesToHex; | ||
function parseHexByte(hexByte) { | ||
if (hexByte.length !== 2) | ||
throw new Error('Invalid byte sequence'); | ||
const byte = Number.parseInt(hexByte, 16); | ||
if (Number.isNaN(byte)) | ||
throw new Error('Invalid byte sequence'); | ||
return byte; | ||
} | ||
// Buffer.from(hex, 'hex') -> hexToBytes(hex) | ||
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); | ||
for (let i = 0; i < array.length; i++) { | ||
const j = i * 2; | ||
array[i] = parseHexByte(hex.slice(j, j + 2)); | ||
} | ||
return array; | ||
} | ||
exports.hexToBytes = hexToBytes; | ||
// Currently avoid insertion of polyfills with packers (browserify/webpack/etc) | ||
@@ -64,5 +87,12 @@ // But setTimeout is pretty slow, maybe worth to investigate howto do minimal polyfill here | ||
exports.asyncLoop = asyncLoop; | ||
function utf8ToBytes(str) { | ||
if (typeof str !== 'string') { | ||
throw new TypeError(`utf8ToBytes expected string, got ${typeof str}`); | ||
} | ||
return new TextEncoder().encode(str); | ||
} | ||
exports.utf8ToBytes = utf8ToBytes; | ||
function toBytes(data) { | ||
if (typeof data === 'string') | ||
data = new TextEncoder().encode(data); | ||
data = utf8ToBytes(data); | ||
if (!(data instanceof Uint8Array)) | ||
@@ -73,5 +103,20 @@ throw new TypeError(`Expected input type is Uint8Array (got ${typeof data})`); | ||
exports.toBytes = toBytes; | ||
// Buffer.concat([buf1, buf2]) -> concatBytes(buf1, buf2) | ||
function concatBytes(...arrays) { | ||
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; | ||
} | ||
exports.concatBytes = concatBytes; | ||
function assertNumber(n) { | ||
if (!Number.isSafeInteger(n)) | ||
throw new Error(`Wrong integer: ${n}`); | ||
if (!Number.isSafeInteger(n) || n < 0) | ||
throw new Error(`Wrong positive integer: ${n}`); | ||
} | ||
@@ -86,4 +131,3 @@ exports.assertNumber = assertNumber; | ||
function assertBytes(bytes, ...lengths) { | ||
if (bytes instanceof Uint8Array && | ||
(!lengths.length || lengths.includes(bytes.length))) { | ||
if (bytes instanceof Uint8Array && (!lengths.length || lengths.includes(bytes.length))) { | ||
return; | ||
@@ -90,0 +134,0 @@ } |
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
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
259665
0
5765