Hash.js provides a comprehensive set of utility functions for data conversion, manipulation, and internal operations. These utilities handle various data formats and encodings used throughout the hashing operations.
Core utilities for converting between different data representations.
/**
* Convert various input formats to byte arrays
* @param msg - Input data (string, array, Uint8Array, etc.)
* @param enc - Encoding for string input ('hex' or undefined for UTF-8)
* @returns Array of bytes
*/
function toArray(msg: any, enc?: 'hex'): number[];
/**
* Convert byte array to hexadecimal string
* @param msg - Array of bytes
* @returns Hex string representation
*/
function toHex(msg: number[]): string;
interface Utils {
toArray: typeof toArray;
toHex: typeof toHex;
inherits: (ctor: Function, superCtor: Function) => void;
}
const utils: Utils;Usage Examples:
const hash = require('hash.js');
// Convert string to byte array
const utf8Bytes = hash.utils.toArray('hello world');
console.log(utf8Bytes); // [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
// Convert hex string to byte array
const hexBytes = hash.utils.toArray('deadbeef', 'hex');
console.log(hexBytes); // [222, 173, 190, 239]
// Convert byte array to hex string
const hexString = hash.utils.toHex([222, 173, 190, 239]);
console.log(hexString); // "deadbeef"
// Handle various input types
const arrayBytes = hash.utils.toArray([1, 2, 3, 4]);
console.log(arrayBytes); // [1, 2, 3, 4]
const uint8Bytes = hash.utils.toArray(new Uint8Array([5, 6, 7, 8]));
console.log(uint8Bytes); // [5, 6, 7, 8]Advanced string processing with Unicode support and UTF-8 encoding.
UTF-8 Encoding:
const hash = require('hash.js');
// Basic ASCII characters
const ascii = hash.utils.toArray('Hello');
console.log(ascii); // [72, 101, 108, 108, 111]
// Unicode characters
const unicode = hash.utils.toArray('Hello δΈη');
console.log(unicode); // [72, 101, 108, 108, 111, 32, 228, 184, 150, 231, 149, 140]
// Emoji and surrogate pairs
const emoji = hash.utils.toArray('ππ');
console.log(emoji); // UTF-8 encoded bytes for emojiHex String Processing:
const hash = require('hash.js');
// Clean hex string processing
const cleanHex = hash.utils.toArray('DEADBEEF', 'hex');
console.log(cleanHex); // [222, 173, 190, 239]
// Hex with non-hex characters (automatically filtered)
const dirtyHex = hash.utils.toArray('DE-AD-BE-EF', 'hex');
console.log(dirtyHex); // [222, 173, 190, 239]
// Odd-length hex strings (automatically padded)
const oddHex = hash.utils.toArray('abc', 'hex');
console.log(oddHex); // [10, 188] (padded to "0abc")Internal utility functions used by hash implementations.
/**
* Convert 32-bit words to hex string with endianness control
* @param msg - Array of 32-bit words
* @param endian - Byte order ('big' or 'little')
* @returns Hex string
*/
function toHex32(msg: number[], endian: 'big' | 'little'): string;
/**
* Convert word to network byte order (big-endian)
* @param w - 32-bit word
* @returns Word in network byte order
*/
function htonl(w: number): number;
/**
* Join bytes into 32-bit words
* @param msg - Byte array
* @param start - Start index
* @param end - End index
* @param endian - Byte order ('big' or 'little')
* @returns Array of 32-bit words
*/
function join32(msg: number[], start: number, end: number, endian: 'big' | 'little'): number[];
/**
* Split 32-bit words into bytes
* @param msg - Array of 32-bit words
* @param endian - Byte order ('big' or 'little')
* @returns Byte array
*/
function split32(msg: number[], endian: 'big' | 'little'): number[];Mathematical utility functions for cryptographic operations.
/**
* 32-bit left rotation
* @param w - 32-bit word
* @param b - Number of bits to rotate
* @returns Rotated word
*/
function rotl32(w: number, b: number): number;
/**
* 32-bit right rotation
* @param w - 32-bit word
* @param b - Number of bits to rotate
* @returns Rotated word
*/
function rotr32(w: number, b: number): number;
/**
* 32-bit addition (with overflow handling)
* @param a - First operand
* @param b - Second operand
* @returns Sum modulo 2^32
*/
function sum32(a: number, b: number): number;
/**
* 32-bit addition of 3 values
* @param a - First operand
* @param b - Second operand
* @param c - Third operand
* @returns Sum modulo 2^32
*/
function sum32_3(a: number, b: number, c: number): number;
/**
* 32-bit addition of 4 values
* @param a - First operand
* @param b - Second operand
* @param c - Third operand
* @param d - Fourth operand
* @returns Sum modulo 2^32
*/
function sum32_4(a: number, b: number, c: number, d: number): number;
/**
* 32-bit addition of 5 values
* @param a - First operand
* @param b - Second operand
* @param c - Third operand
* @param d - Fourth operand
* @param e - Fifth operand
* @returns Sum modulo 2^32
*/
function sum32_5(a: number, b: number, c: number, d: number, e: number): number;Extended precision operations for SHA-384 and SHA-512.
/**
* 64-bit addition (in-place)
* @param buf - Buffer to store result
* @param pos - Position in buffer
* @param ah - High word of first operand
* @param al - Low word of first operand
*/
function sum64(buf: number[], pos: number, ah: number, al: number): void;
/**
* 64-bit addition high word
* @param ah - High word of first operand
* @param al - Low word of first operand
* @param bh - High word of second operand
* @param bl - Low word of second operand
* @returns High word of sum
*/
function sum64_hi(ah: number, al: number, bh: number, bl: number): number;
/**
* 64-bit addition low word
* @param ah - High word of first operand
* @param al - Low word of first operand
* @param bh - High word of second operand
* @param bl - Low word of second operand
* @returns Low word of sum
*/
function sum64_lo(ah: number, al: number, bh: number, bl: number): number;
/**
* 64-bit right rotation high word
* @param ah - High word
* @param al - Low word
* @param num - Number of bits to rotate
* @returns High word of rotated value
*/
function rotr64_hi(ah: number, al: number, num: number): number;
/**
* 64-bit right rotation low word
* @param ah - High word
* @param al - Low word
* @param num - Number of bits to rotate
* @returns Low word of rotated value
*/
function rotr64_lo(ah: number, al: number, num: number): number;
/**
* 64-bit addition of 4 values (high word)
* @param ah - High word of first operand
* @param al - Low word of first operand
* @param bh - High word of second operand
* @param bl - Low word of second operand
* @param ch - High word of third operand
* @param cl - Low word of third operand
* @param dh - High word of fourth operand
* @param dl - Low word of fourth operand
* @returns High word of sum
*/
function sum64_4_hi(ah: number, al: number, bh: number, bl: number, ch: number, cl: number, dh: number, dl: number): number;
/**
* 64-bit addition of 4 values (low word)
* @param ah - High word of first operand
* @param al - Low word of first operand
* @param bh - High word of second operand
* @param bl - Low word of second operand
* @param ch - High word of third operand
* @param cl - Low word of third operand
* @param dh - High word of fourth operand
* @param dl - Low word of fourth operand
* @returns Low word of sum
*/
function sum64_4_lo(ah: number, al: number, bh: number, bl: number, ch: number, cl: number, dh: number, dl: number): number;
/**
* 64-bit addition of 5 values (high word)
* @param ah - High word of first operand
* @param al - Low word of first operand
* @param bh - High word of second operand
* @param bl - Low word of second operand
* @param ch - High word of third operand
* @param cl - Low word of third operand
* @param dh - High word of fourth operand
* @param dl - Low word of fourth operand
* @param eh - High word of fifth operand
* @param el - Low word of fifth operand
* @returns High word of sum
*/
function sum64_5_hi(ah: number, al: number, bh: number, bl: number, ch: number, cl: number, dh: number, dl: number, eh: number, el: number): number;
/**
* 64-bit addition of 5 values (low word)
* @param ah - High word of first operand
* @param al - Low word of first operand
* @param bh - High word of second operand
* @param bl - Low word of second operand
* @param ch - High word of third operand
* @param cl - Low word of third operand
* @param dh - High word of fourth operand
* @param dl - Low word of fourth operand
* @param eh - High word of fifth operand
* @param el - Low word of fifth operand
* @returns Low word of sum
*/
function sum64_5_lo(ah: number, al: number, bh: number, bl: number, ch: number, cl: number, dh: number, dl: number, eh: number, el: number): number;
/**
* 64-bit right shift (high word)
* @param ah - High word
* @param al - Low word
* @param num - Number of bits to shift
* @returns High word of shifted value
*/
function shr64_hi(ah: number, al: number, num: number): number;
/**
* 64-bit right shift (low word)
* @param ah - High word
* @param al - Low word
* @param num - Number of bits to shift
* @returns Low word of shifted value
*/
function shr64_lo(ah: number, al: number, num: number): number;Utility functions for consistent hex string formatting.
/**
* Pad hex string to 2 characters with leading zero
* @param word - Hex string
* @returns Zero-padded string
*/
function zero2(word: string): string;
/**
* Pad hex string to 8 characters with leading zeros
* @param word - Hex string
* @returns Zero-padded string
*/
function zero8(word: string): string;Usage Examples:
const hash = require('hash.js');
// Access internal utilities (not typically needed by end users)
const utils = hash.utils;
// 32-bit operations
const rotated = utils.rotl32(0x12345678, 8);
console.log(rotated.toString(16)); // "34567812"
const sum = utils.sum32(0xFFFFFFFF, 1);
console.log(sum); // 0 (overflow wraps around)
// Hex formatting
const padded2 = utils.zero2('a');
console.log(padded2); // "0a"
const padded8 = utils.zero8('123');
console.log(padded8); // "00000123"JavaScript inheritance utility for extending hash classes.
/**
* Set up inheritance between constructor functions
* @param ctor - Child constructor
* @param superCtor - Parent constructor
*/
function inherits(ctor: Function, superCtor: Function): void;Usage Examples:
const hash = require('hash.js');
// Used internally for hash class inheritance
// hash.utils.inherits(SHA256, BlockHash);
// Available for custom hash implementations
function CustomHash() {
// Custom hash constructor
}
hash.utils.inherits(CustomHash, hash.common.BlockHash);All utilities are available through the main hash object:
const hash = require('hash.js');
// Primary utilities
const bytes = hash.utils.toArray('test data');
const hex = hash.utils.toHex(bytes);
// Advanced utilities (typically for internal use)
const words = hash.utils.join32(bytes, 0, bytes.length, 'big');
const backToBytes = hash.utils.split32(words, 'big');