CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-polkadot--util-crypto

A collection of useful crypto utilities for Polkadot ecosystem projects

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

hashing.mddocs/

Hashing Algorithms

Comprehensive hashing functions including Blake2, Keccak, SHA, and xxHash for cryptographic operations, integrity verification, and data fingerprinting.

Capabilities

Blake2 Hashing

Blake2 is a cryptographic hash function optimized for speed and security, commonly used in Polkadot.

/**
 * Blake2b hash as hex string
 * @param data - Data to hash
 * @param bitLength - Output length in bits (64, 128, 256, 512)
 * @param key - Optional key for keyed hashing
 * @returns Hex-encoded hash string
 */
function blake2AsHex(data: Uint8Array, bitLength?: 64 | 128 | 256 | 512, key?: Uint8Array): string;

/**
 * Blake2b hash as Uint8Array
 * @param data - Data to hash
 * @param bitLength - Output length in bits (64, 128, 256, 512)
 * @param key - Optional key for keyed hashing
 * @returns Hash as byte array
 */
function blake2AsU8a(data: Uint8Array, bitLength?: 64 | 128 | 256 | 512, key?: Uint8Array): Uint8Array;

Usage Example:

import { blake2AsHex, blake2AsU8a } from "@polkadot/util-crypto";

const data = new TextEncoder().encode("Hello Polkadot");

// 256-bit Blake2b hash (default)
const hash256 = blake2AsU8a(data);
console.log(hash256.length); // 32 bytes

// 128-bit Blake2b hash as hex
const hashHex = blake2AsHex(data, 128);
console.log(hashHex); // "0x..." 16 bytes as hex

// Keyed Blake2b hash
const key = new Uint8Array(32);
const keyedHash = blake2AsU8a(data, 256, key);

Keccak Hashing

Keccak hashing family including Keccak-256 and Keccak-512, used in Ethereum.

/**
 * Keccak-256 hash as Uint8Array
 * @param value - Data to hash
 * @returns 32-byte Keccak-256 hash
 */
function keccak256AsU8a(value: Uint8Array): Uint8Array;

/**
 * Keccak-512 hash as Uint8Array
 * @param value - Data to hash
 * @returns 64-byte Keccak-512 hash
 */
function keccak512AsU8a(value: Uint8Array): Uint8Array;

/**
 * Keccak hash as hex string
 * @param value - Data to hash
 * @param bitLength - Output length (256 or 512)
 * @returns Hex-encoded hash
 */
function keccakAsHex(value: Uint8Array, bitLength?: 256 | 512): string;

/**
 * Keccak hash as Uint8Array
 * @param value - Data to hash
 * @param bitLength - Output length (256 or 512)
 * @returns Hash as byte array
 */
function keccakAsU8a(value: Uint8Array, bitLength?: 256 | 512): Uint8Array;

Usage Example:

import { keccak256AsU8a, keccakAsHex } from "@polkadot/util-crypto";

const data = new TextEncoder().encode("Hello Ethereum");

// Keccak-256 (commonly used in Ethereum)
const hash256 = keccak256AsU8a(data);
console.log(hash256.length); // 32 bytes

// Keccak-512 as hex
const hash512Hex = keccakAsHex(data, 512);

SHA Hashing

SHA-2 family hashing including SHA-256 and SHA-512.

/**
 * SHA-256 hash as Uint8Array
 * @param value - Data to hash
 * @returns 32-byte SHA-256 hash
 */
function sha256AsU8a(value: Uint8Array): Uint8Array;

/**
 * SHA-512 hash as Uint8Array
 * @param value - Data to hash
 * @returns 64-byte SHA-512 hash
 */
function sha512AsU8a(value: Uint8Array): Uint8Array;

/**
 * SHA hash as Uint8Array
 * @param value - Data to hash
 * @param bitLength - Output length (256 or 512)
 * @returns Hash as byte array
 */
function shaAsU8a(value: Uint8Array, bitLength?: 256 | 512): Uint8Array;

Usage Example:

import { sha256AsU8a, sha512AsU8a } from "@polkadot/util-crypto";

const data = new TextEncoder().encode("Hello SHA");

// SHA-256
const sha256 = sha256AsU8a(data);
console.log(sha256.length); // 32 bytes

// SHA-512
const sha512 = sha512AsU8a(data);
console.log(sha512.length); // 64 bytes

XXHash

Fast non-cryptographic hash function for checksums and data integrity.

/**
 * XXHash as hex string
 * @param data - Data to hash
 * @param bitLength - Output length (64, 128, 256)
 * @returns Hex-encoded hash
 */
function xxhashAsHex(data: Uint8Array, bitLength?: 64 | 128 | 256): string;

/**
 * XXHash as Uint8Array
 * @param data - Data to hash
 * @param bitLength - Output length (64, 128, 256)
 * @returns Hash as byte array
 */
function xxhashAsU8a(data: Uint8Array, bitLength?: 64 | 128 | 256): Uint8Array;

Usage Example:

import { xxhashAsU8a, xxhashAsHex } from "@polkadot/util-crypto";

const data = new TextEncoder().encode("Fast hash");

// 64-bit XXHash (default)
const hash64 = xxhashAsU8a(data);
console.log(hash64.length); // 8 bytes

// 256-bit XXHash as hex
const hash256Hex = xxhashAsHex(data, 256);

HMAC (Hash-based Message Authentication Code)

Keyed hash functions for message authentication and integrity.

/**
 * HMAC-SHA256 as Uint8Array
 * @param key - Secret key for HMAC
 * @param data - Data to authenticate
 * @returns 32-byte HMAC-SHA256
 */
function hmacSha256AsU8a(key: Uint8Array, data: Uint8Array): Uint8Array;

/**
 * HMAC-SHA512 as Uint8Array
 * @param key - Secret key for HMAC
 * @param data - Data to authenticate
 * @returns 64-byte HMAC-SHA512
 */
function hmacSha512AsU8a(key: Uint8Array, data: Uint8Array): Uint8Array;

/**
 * HMAC-SHA as Uint8Array
 * @param key - Secret key for HMAC
 * @param data - Data to authenticate
 * @param bitLength - Output length (256 or 512)
 * @returns HMAC as byte array
 */
function hmacShaAsU8a(key: Uint8Array, data: Uint8Array, bitLength?: 256 | 512): Uint8Array;

Usage Example:

import { hmacSha256AsU8a, hmacSha512AsU8a } from "@polkadot/util-crypto";

const key = new Uint8Array(32); // Secret key
const data = new TextEncoder().encode("Authenticated message");

// HMAC-SHA256
const hmac256 = hmacSha256AsU8a(key, data);
console.log(hmac256.length); // 32 bytes

// HMAC-SHA512
const hmac512 = hmacSha512AsU8a(key, data);
console.log(hmac512.length); // 64 bytes

Hash Algorithm Comparison

AlgorithmSecuritySpeedUse Case
Blake2CryptographicVery FastPolkadot hashing
KeccakCryptographicFastEthereum compatibility
SHA-2CryptographicModerateGeneral purpose
XXHashNon-cryptographicExtremely FastChecksums, deduplication

Common Use Cases

Data Integrity

import { blake2AsU8a } from "@polkadot/util-crypto";

// Create checksum for data integrity
const data = new Uint8Array([1, 2, 3, 4, 5]);
const checksum = blake2AsU8a(data, 128); // 16-byte checksum

Password Hashing

import { blake2AsU8a } from "@polkadot/util-crypto";

// Hash password with salt
const password = new TextEncoder().encode("user-password");
const salt = new Uint8Array(16); // Random salt
const hashedPassword = blake2AsU8a(password, 256, salt);

Message Authentication

import { hmacSha256AsU8a } from "@polkadot/util-crypto";

// Authenticate message with HMAC
const secretKey = new Uint8Array(32); // Shared secret
const message = new TextEncoder().encode("Important message");
const mac = hmacSha256AsU8a(secretKey, message);

Security Considerations

  • Blake2: Suitable for all cryptographic purposes, preferred in Polkadot
  • Keccak: Used in Ethereum, different from SHA-3 standard
  • SHA-2: Widely adopted, slower than Blake2 but highly secure
  • XXHash: Fast but not cryptographically secure, use only for checksums
  • HMAC: Always use with appropriate key management
  • Salt Usage: Always use random salts for password hashing

Install with Tessl CLI

npx tessl i tessl/npm-polkadot--util-crypto

docs

address.md

base-encoding.md

crypto-init.md

ethereum.md

hashing.md

index.md

json-encryption.md

key-derivation-pbkdf.md

key-derivation.md

keypairs.md

mnemonic.md

random.md

signatures.md

tile.json