A simple SHA-256 / SHA-224 hash function for JavaScript supports UTF-8 encoding.
npx @tessl/cli install tessl/npm-js-sha256@0.11.0js-sha256 is a simple SHA-256 / SHA-224 hash function library for JavaScript that supports UTF-8 encoding. It provides both direct hashing functions and streaming hash objects with update capabilities, supports HMAC authentication, and provides multiple output formats including hex strings, byte arrays, and ArrayBuffer.
npm install js-sha256// Main import - provides sha256 function directly, sha224 as property
const sha256 = require('js-sha256');
const sha224 = sha256.sha224;// Named imports
const { sha256, sha224 } = require('js-sha256');// Explicit property access
const sha256 = require('js-sha256').sha256;
const sha224 = require('js-sha256').sha224;ES6 modules:
import { sha256, sha224 } from 'js-sha256';import { sha256, sha224 } from 'js-sha256';
// Direct hashing
const hash1 = sha256('Hello, World!');
const hash2 = sha224('Hello, World!');
// Incremental hashing
const hasher = sha256.create();
hasher.update('Hello, ');
hasher.update('World!');
const result = hasher.hex();
// HMAC authentication
const hmac = sha256.hmac('secret-key', 'message to authenticate');Core SHA-256 hash functionality with support for multiple input formats and output types.
/**
* Hash a message and return hex string
* @param message - Data to hash (string, Array, Uint8Array, ArrayBuffer)
* @returns 64-character hex string
*/
function sha256(message: Message): string;
/**
* Create a hash object for incremental hashing
* @returns Hasher instance for chaining operations
*/
sha256.create(): Hasher;
/**
* Create a hash object and add initial message
* @param message - Initial data to hash
* @returns Hasher instance for chaining operations
*/
sha256.update(message: Message): Hasher;
/**
* Hash a message and return hex string
* @param message - Data to hash
* @returns 64-character hex string
*/
sha256.hex(message: Message): string;
/**
* Hash a message and return byte array
* @param message - Data to hash
* @returns Array of 32 integers (0-255)
*/
sha256.array(message: Message): number[];
/**
* Hash a message and return byte array (alias for array)
* @param message - Data to hash
* @returns Array of 32 integers (0-255)
*/
sha256.digest(message: Message): number[];
/**
* Hash a message and return ArrayBuffer
* @param message - Data to hash
* @returns ArrayBuffer with 32 bytes
*/
sha256.arrayBuffer(message: Message): ArrayBuffer;Core SHA-224 hash functionality with identical API to SHA-256 but shorter output.
/**
* Hash a message and return hex string
* @param message - Data to hash (string, Array, Uint8Array, ArrayBuffer)
* @returns 56-character hex string
*/
function sha224(message: Message): string;
/**
* Create a hash object for incremental hashing
* @returns Hasher instance for chaining operations
*/
sha224.create(): Hasher;
/**
* Create a hash object and add initial message
* @param message - Initial data to hash
* @returns Hasher instance for chaining operations
*/
sha224.update(message: Message): Hasher;
/**
* Hash a message and return hex string
* @param message - Data to hash
* @returns 56-character hex string
*/
sha224.hex(message: Message): string;
/**
* Hash a message and return byte array
* @param message - Data to hash
* @returns Array of 28 integers (0-255)
*/
sha224.array(message: Message): number[];
/**
* Hash a message and return byte array (alias for array)
* @param message - Data to hash
* @returns Array of 28 integers (0-255)
*/
sha224.digest(message: Message): number[];
/**
* Hash a message and return ArrayBuffer
* @param message - Data to hash
* @returns ArrayBuffer with 28 bytes
*/
sha224.arrayBuffer(message: Message): ArrayBuffer;Hasher interface for streaming and incremental hash computation.
interface Hasher {
/**
* Add data to the hash computation
* @param message - Data to add to hash
* @returns this (for method chaining)
*/
update(message: Message): Hasher;
/**
* Finalize hash and return hex string
* @returns Hex string representation of hash
*/
hex(): string;
/**
* Finalize hash and return hex string (alias for hex)
* @returns Hex string representation of hash
*/
toString(): string;
/**
* Finalize hash and return byte array
* @returns Array of integers (0-255)
*/
array(): number[];
/**
* Finalize hash and return byte array (alias for array)
* @returns Array of integers (0-255)
*/
digest(): number[];
/**
* Finalize hash and return ArrayBuffer
* @returns ArrayBuffer with hash bytes
*/
arrayBuffer(): ArrayBuffer;
}Usage Examples:
import { sha256 } from 'js-sha256';
// Stream large data
const hasher = sha256.create();
hasher.update('chunk1');
hasher.update('chunk2');
hasher.update('chunk3');
const finalHash = hasher.hex();
// Method chaining
const hash = sha256.update('initial data')
.update('more data')
.hex();
// Different output formats
const hasher2 = sha256.create().update('test data');
const hexOutput = hasher2.hex(); // '...' (64 chars)
const arrayOutput = hasher2.array(); // [n1, n2, ..., n32]
const bufferOutput = hasher2.arrayBuffer(); // ArrayBuffer(32)Hash-based Message Authentication Code functionality for both SHA-256 and SHA-224.
/**
* Compute HMAC-SHA256 and return hex string
* @param secretKey - Secret key for authentication
* @param message - Message to authenticate
* @returns 64-character hex string
*/
sha256.hmac(secretKey: Message, message: Message): string;
/**
* Create HMAC hasher with secret key
* @param secretKey - Secret key for authentication
* @returns Hasher instance for incremental HMAC computation
*/
sha256.hmac.create(secretKey: Message): Hasher;
/**
* Create HMAC hasher with secret key and initial message
* @param secretKey - Secret key for authentication
* @param message - Initial message to authenticate
* @returns Hasher instance for chaining operations
*/
sha256.hmac.update(secretKey: Message, message: Message): Hasher;
/**
* Compute HMAC-SHA256 and return hex string
* @param secretKey - Secret key for authentication
* @param message - Message to authenticate
* @returns 64-character hex string
*/
sha256.hmac.hex(secretKey: Message, message: Message): string;
/**
* Compute HMAC-SHA256 and return byte array
* @param secretKey - Secret key for authentication
* @param message - Message to authenticate
* @returns Array of 32 integers (0-255)
*/
sha256.hmac.array(secretKey: Message, message: Message): number[];
/**
* Compute HMAC-SHA256 and return byte array (alias for array)
* @param secretKey - Secret key for authentication
* @param message - Message to authenticate
* @returns Array of 32 integers (0-255)
*/
sha256.hmac.digest(secretKey: Message, message: Message): number[];
/**
* Compute HMAC-SHA256 and return ArrayBuffer
* @param secretKey - Secret key for authentication
* @param message - Message to authenticate
* @returns ArrayBuffer with 32 bytes
*/
sha256.hmac.arrayBuffer(secretKey: Message, message: Message): ArrayBuffer;
/**
* Compute HMAC-SHA224 and return hex string
* @param secretKey - Secret key for authentication
* @param message - Message to authenticate
* @returns 56-character hex string
*/
sha224.hmac(secretKey: Message, message: Message): string;
/**
* Create HMAC-SHA224 hasher with secret key
* @param secretKey - Secret key for authentication
* @returns Hasher instance for incremental HMAC computation
*/
sha224.hmac.create(secretKey: Message): Hasher;
/**
* Create HMAC-SHA224 hasher with secret key and initial message
* @param secretKey - Secret key for authentication
* @param message - Initial message to authenticate
* @returns Hasher instance for chaining operations
*/
sha224.hmac.update(secretKey: Message, message: Message): Hasher;
/**
* Compute HMAC-SHA224 and return hex string
* @param secretKey - Secret key for authentication
* @param message - Message to authenticate
* @returns 56-character hex string
*/
sha224.hmac.hex(secretKey: Message, message: Message): string;
/**
* Compute HMAC-SHA224 and return byte array
* @param secretKey - Secret key for authentication
* @param message - Message to authenticate
* @returns Array of 28 integers (0-255)
*/
sha224.hmac.array(secretKey: Message, message: Message): number[];
/**
* Compute HMAC-SHA224 and return byte array (alias for array)
* @param secretKey - Secret key for authentication
* @param message - Message to authenticate
* @returns Array of 28 integers (0-255)
*/
sha224.hmac.digest(secretKey: Message, message: Message): number[];
/**
* Compute HMAC-SHA224 and return ArrayBuffer
* @param secretKey - Secret key for authentication
* @param message - Message to authenticate
* @returns ArrayBuffer with 28 bytes
*/
sha224.hmac.arrayBuffer(secretKey: Message, message: Message): ArrayBuffer;Usage Examples:
import { sha256, sha224 } from 'js-sha256';
// Direct HMAC computation
const hmac256 = sha256.hmac('my-secret-key', 'data to authenticate');
const hmac224 = sha224.hmac('my-secret-key', 'data to authenticate');
// Incremental HMAC computation
const hmacHasher = sha256.hmac.create('my-secret-key');
hmacHasher.update('chunk 1');
hmacHasher.update('chunk 2');
const authenticatedHash = hmacHasher.hex();
// Different output formats
const hmacHex = sha256.hmac.hex('key', 'message'); // hex string
const hmacArray = sha256.hmac.array('key', 'message'); // byte array
const hmacBuffer = sha256.hmac.arrayBuffer('key', 'message'); // ArrayBuffer/**
* Supported message input types
*/
type Message = string | number[] | ArrayBuffer | Uint8Array;
/**
* Hash computation interface for incremental operations
*/
interface Hasher {
update(message: Message): Hasher;
hex(): string;
toString(): string;
array(): number[];
digest(): number[];
arrayBuffer(): ArrayBuffer;
}
/**
* Hash function interface with multiple output methods
*/
interface Hash {
(message: Message): string;
create(): Hasher;
update(message: Message): Hasher;
hex(message: Message): string;
array(message: Message): number[];
digest(message: Message): number[];
arrayBuffer(message: Message): ArrayBuffer;
hmac: Hmac;
}
/**
* HMAC function interface
*/
interface Hmac {
(secretKey: Message, message: Message): string;
create(secretKey: Message): Hasher;
update(secretKey: Message, message: Message): Hasher;
hex(secretKey: Message, message: Message): string;
array(secretKey: Message, message: Message): number[];
digest(secretKey: Message, message: Message): number[];
arrayBuffer(secretKey: Message, message: Message): ArrayBuffer;
}The library throws an Error with the message "input is invalid type" when:
null or undefinedValid input types:
The library automatically detects and optimizes for different JavaScript environments:
window.sha256 and window.sha224 global variablescrypto module optimizations when availableself.sha256 and self.sha224 global variablesmodule.exports support