or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

asymmetric-cryptography.mddigital-signatures.mdhash-functions.mdhmac.mdindex.mdkey-derivation.mdkey-exchange.mdrandom-generation.mdsymmetric-encryption.md
tile.json

hash-functions.mddocs/

Hash Functions

Cryptographic hash functions for data integrity, checksums, and digital fingerprints. Supports SHA-1, SHA-2 family (224, 256, 384, 512), MD5, and RIPEMD-160.

Capabilities

Create Hash

Creates a hash object for computing cryptographic hashes of data.

/**
 * Create a hash object for the specified algorithm
 * @param {string} algorithm - Hash algorithm name (e.g., 'sha256', 'md5', 'sha1')
 * @returns {Hash} Hash object for computing digest
 */
function createHash(algorithm) { }

/**
 * Hash object for computing cryptographic hash digests
 * @class Hash
 */
/**
 * Update the hash with new data
 * @param {string|Buffer} data - Data to hash (string or Buffer)
 * @param {string} [inputEncoding='utf8'] - Encoding of input data if string
 * @returns {Hash} Hash object for method chaining
 */
Hash.prototype.update = function(data, inputEncoding) { };

/**
 * Compute the final hash digest
 * @param {string} [encoding] - Output encoding ('hex', 'base64', 'latin1', etc.)
 * @returns {Buffer|string} Hash digest as Buffer or encoded string
 */
Hash.prototype.digest = function(encoding) { };

Usage Examples:

const crypto = require('crypto-browserify');

// Basic hash computation
const hash = crypto.createHash('sha256')
  .update('hello world')
  .digest('hex');
console.log(hash); // "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"

// Hash binary data
const data = Buffer.from([0x01, 0x02, 0x03, 0x04]);
const binaryHash = crypto.createHash('sha1')
  .update(data)
  .digest('base64');

// Incremental hashing
const incrementalHash = crypto.createHash('md5');
incrementalHash.update('first chunk');
incrementalHash.update('second chunk');
const result = incrementalHash.digest('hex');

// Hash with different encodings
const utf8Hash = crypto.createHash('sha256')
  .update('café', 'utf8')
  .digest('hex');

const latinHash = crypto.createHash('sha256')
  .update('café', 'latin1')
  .digest('hex');

Get Supported Algorithms

Returns a list of all supported hash algorithms.

/**
 * Get array of supported hash algorithm names
 * @returns {string[]} Array of supported hash algorithm strings
 */
function getHashes() { }

Usage Examples:

const crypto = require('crypto-browserify');

// Get all supported hash algorithms
const algorithms = crypto.getHashes();
console.log(algorithms);
// ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160', ...]

// Check if algorithm is supported
const isSupported = crypto.getHashes().includes('sha256');
if (isSupported) {
  const hash = crypto.createHash('sha256');
}

// Iterate through available algorithms
crypto.getHashes().forEach(algo => {
  if (algo.startsWith('sha')) {
    console.log(`SHA algorithm available: ${algo}`);
  }
});

Hash Class Constructor

Direct access to the Hash class for advanced usage patterns.

/**
 * Hash class constructor (equivalent to createHash result)
 * @constructor
 */
function Hash() { }

Supported Algorithms

The following hash algorithms are supported:

  • SHA-1: 'sha1' - Legacy algorithm, avoid for new applications
  • SHA-2 Family:
    • 'sha224' - 224-bit output
    • 'sha256' - 256-bit output (recommended)
    • 'sha384' - 384-bit output
    • 'sha512' - 512-bit output
  • MD5: 'md5' - Legacy algorithm, avoid for cryptographic purposes
  • RIPEMD-160: 'rmd160' - 160-bit output

Error Handling

Hash functions may throw errors in the following scenarios:

  • Unsupported algorithm: When the specified algorithm is not available
  • Invalid data: When trying to hash invalid data types
  • Double digest: When calling digest() multiple times on the same hash object
try {
  const hash = crypto.createHash('unsupported-algo');
} catch (err) {
  console.error('Unsupported hash algorithm:', err.message);
}

// Hash objects cannot be reused after digest()
const hasher = crypto.createHash('sha256');
hasher.update('data');
const firstDigest = hasher.digest('hex');

try {
  const secondDigest = hasher.digest('hex'); // This will throw
} catch (err) {
  console.error('Cannot reuse hash object:', err.message);
}