ox
Version:
Ethereum Standard Library
164 lines • 5.22 kB
JavaScript
import { hmac } from '@noble/hashes/hmac.js';
import { ripemd160 as noble_ripemd160 } from '@noble/hashes/legacy.js';
import { sha256 as noble_sha256 } from '@noble/hashes/sha2.js';
import { keccak_256 as noble_keccak256 } from '@noble/hashes/sha3.js';
import * as Bytes from './Bytes.js';
import * as Hex from './Hex.js';
/**
* Calculates the [Keccak256](https://en.wikipedia.org/wiki/SHA-3) hash of a {@link ox#Bytes.Bytes} or {@link ox#Hex.Hex} value.
*
* This function is a re-export of `keccak_256` from [`@noble/hashes`](https://github.com/paulmillr/noble-hashes), an audited & minimal JS hashing library.
*
* @example
* ```ts twoslash
* import { Hash } from 'ox'
*
* Hash.keccak256('0xdeadbeef')
* // @log: '0xd4fd4e189132273036449fc9e11198c739161b4c0116a9a2dccdfa1c492006f1'
* ```
*
* @example
* ### Calculate Hash of a String
*
* ```ts twoslash
* import { Hash, Hex } from 'ox'
*
* Hash.keccak256(Hex.fromString('hello world'))
* // @log: '0x3ea2f1d0abf3fc66cf29eebb70cbd4e7fe762ef8a09bcc06c8edf641230afec0'
* ```
*
* @example
* ### Configure Return Type
*
* ```ts twoslash
* import { Hash } from 'ox'
*
* Hash.keccak256('0xdeadbeef', { as: 'Bytes' })
* // @log: Uint8Array [...]
* ```
*
* @param value - {@link ox#Bytes.Bytes} or {@link ox#Hex.Hex} value.
* @param options - Options.
* @returns Keccak256 hash.
*/
export function keccak256(value, options = {}) {
const isBytes = value instanceof Uint8Array;
const { as = isBytes ? 'Bytes' : 'Hex' } = options;
const bytes = noble_keccak256(isBytes ? value : Bytes.from(value));
if (as === 'Bytes')
return bytes;
return Hex.fromBytes(bytes);
}
/**
* Calculates the [HMAC-SHA256](https://en.wikipedia.org/wiki/HMAC) of a {@link ox#Bytes.Bytes} or {@link ox#Hex.Hex} value.
*
* This function is a re-export of `hmac` from [`@noble/hashes`](https://github.com/paulmillr/noble-hashes), an audited & minimal JS hashing library.
*
* @example
* ```ts twoslash
* import { Hash, Hex } from 'ox'
*
* Hash.hmac256(Hex.fromString('key'), '0xdeadbeef')
* // @log: '0x...'
* ```
*
* @example
* ### Configure Return Type
*
* ```ts twoslash
* import { Hash, Hex } from 'ox'
*
* Hash.hmac256(Hex.fromString('key'), '0xdeadbeef', {
* as: 'Bytes'
* })
* // @log: Uint8Array [...]
* ```
*
* @param key - {@link ox#Bytes.Bytes} or {@link ox#Hex.Hex} key.
* @param value - {@link ox#Bytes.Bytes} or {@link ox#Hex.Hex} value.
* @param options - Options.
* @returns HMAC-SHA256 hash.
*/
export function hmac256(key, value, options = {}) {
const isBytes = value instanceof Uint8Array;
const { as = isBytes ? 'Bytes' : 'Hex' } = options;
const keyBytes = key instanceof Uint8Array ? key : Bytes.from(key);
const valueBytes = isBytes ? value : Bytes.from(value);
const bytes = hmac(noble_sha256, keyBytes, valueBytes);
if (as === 'Bytes')
return bytes;
return Hex.fromBytes(bytes);
}
/**
* Calculates the [Ripemd160](https://en.wikipedia.org/wiki/RIPEMD) hash of a {@link ox#Bytes.Bytes} or {@link ox#Hex.Hex} value.
*
* This function is a re-export of `ripemd160` from [`@noble/hashes`](https://github.com/paulmillr/noble-hashes), an audited & minimal JS hashing library.
*
* @example
* ```ts twoslash
* import { Hash } from 'ox'
*
* Hash.ripemd160('0xdeadbeef')
* // '0x226821c2f5423e11fe9af68bd285c249db2e4b5a'
* ```
*
* @param value - {@link ox#Bytes.Bytes} or {@link ox#Hex.Hex} value.
* @param options - Options.
* @returns Ripemd160 hash.
*/
export function ripemd160(value, options = {}) {
const isBytes = value instanceof Uint8Array;
const { as = isBytes ? 'Bytes' : 'Hex' } = options;
const bytes = noble_ripemd160(isBytes ? value : Bytes.from(value));
if (as === 'Bytes')
return bytes;
return Hex.fromBytes(bytes);
}
/**
* Calculates the [Sha256](https://en.wikipedia.org/wiki/SHA-256) hash of a {@link ox#Bytes.Bytes} or {@link ox#Hex.Hex} value.
*
* This function is a re-export of `sha256` from [`@noble/hashes`](https://github.com/paulmillr/noble-hashes), an audited & minimal JS hashing library.
*
* @example
* ```ts twoslash
* import { Hash } from 'ox'
*
* Hash.sha256('0xdeadbeef')
* // '0x5f78c33274e43fa9de5659265c1d917e25c03722dcb0b8d27db8d5feaa813953'
* ```
*
* @param value - {@link ox#Bytes.Bytes} or {@link ox#Hex.Hex} value.
* @param options - Options.
* @returns Sha256 hash.
*/
export function sha256(value, options = {}) {
const isBytes = value instanceof Uint8Array;
const { as = isBytes ? 'Bytes' : 'Hex' } = options;
const bytes = noble_sha256(isBytes ? value : Bytes.from(value));
if (as === 'Bytes')
return bytes;
return Hex.fromBytes(bytes);
}
/**
* Checks if a string is a valid hash value.
*
* @example
* ```ts twoslash
* import { Hash } from 'ox'
*
* Hash.validate('0x')
* // @log: false
*
* Hash.validate(
* '0x3ea2f1d0abf3fc66cf29eebb70cbd4e7fe762ef8a09bcc06c8edf641230afec0'
* )
* // @log: true
* ```
*
* @param value - Value to check.
* @returns Whether the value is a valid hash.
*/
export function validate(value) {
return Hex.validate(value) && Hex.size(value) === 32;
}
//# sourceMappingURL=Hash.js.map