WebAssembly interface layer providing high-performance cryptographic functions for blockchain applications in the Polkadot ecosystem.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Various cryptographic hash functions including SHA variants, BLAKE2b, Keccak, HMAC, and the Two-X hash function optimized for Polkadot applications.
BLAKE2b cryptographic hash function with support for keyed hashing and configurable output size.
/**
* Computes BLAKE2b hash
* @param data - Data to hash as Uint8Array
* @param key - Optional key for keyed hashing as Uint8Array (use empty array for keyless)
* @param size - Output size in bytes (1-64)
* @returns Hash as Uint8Array of specified size
*/
function blake2b(data: Uint8Array, key: Uint8Array, size: number): Uint8Array;Usage Example:
import { waitReady, blake2b } from "@polkadot/wasm-crypto";
await waitReady();
const data = new TextEncoder().encode("Hello, BLAKE2b!");
// Keyless hashing with different output sizes
const hash32 = blake2b(data, new Uint8Array(0), 32);
const hash64 = blake2b(data, new Uint8Array(0), 64);
// Keyed hashing
const key = new Uint8Array(32).fill(1);
const keyedHash = blake2b(data, key, 32);
console.log("32-byte hash length:", hash32.length); // 32
console.log("64-byte hash length:", hash64.length); // 64
console.log("Keyed hash length:", keyedHash.length); // 32Standard SHA-256 cryptographic hash function.
/**
* Computes SHA-256 hash
* @param data - Data to hash as Uint8Array
* @returns 32-byte hash as Uint8Array
*/
function sha256(data: Uint8Array): Uint8Array;Usage Example:
import { waitReady, sha256 } from "@polkadot/wasm-crypto";
await waitReady();
const data = new TextEncoder().encode("Hello, SHA-256!");
const hash = sha256(data);
console.log("SHA-256 hash length:", hash.length); // 32Standard SHA-512 cryptographic hash function.
/**
* Computes SHA-512 hash
* @param data - Data to hash as Uint8Array
* @returns 64-byte hash as Uint8Array
*/
function sha512(data: Uint8Array): Uint8Array;Usage Example:
import { waitReady, sha512 } from "@polkadot/wasm-crypto";
await waitReady();
const data = new TextEncoder().encode("Hello, SHA-512!");
const hash = sha512(data);
console.log("SHA-512 hash length:", hash.length); // 64Keccak-256 hash function (different from SHA3-256).
/**
* Computes Keccak-256 hash
* @param data - Data to hash as Uint8Array
* @returns 32-byte hash as Uint8Array
*/
function keccak256(data: Uint8Array): Uint8Array;Usage Example:
import { waitReady, keccak256 } from "@polkadot/wasm-crypto";
await waitReady();
const data = new TextEncoder().encode("Hello, Keccak-256!");
const hash = keccak256(data);
console.log("Keccak-256 hash length:", hash.length); // 32Keccak-512 hash function.
/**
* Computes Keccak-512 hash
* @param data - Data to hash as Uint8Array
* @returns 64-byte hash as Uint8Array
*/
function keccak512(data: Uint8Array): Uint8Array;Usage Example:
import { waitReady, keccak512 } from "@polkadot/wasm-crypto";
await waitReady();
const data = new TextEncoder().encode("Hello, Keccak-512!");
const hash = keccak512(data);
console.log("Keccak-512 hash length:", hash.length); // 64Hash-based Message Authentication Code using SHA-256.
/**
* Computes HMAC-SHA256
* @param key - Secret key as Uint8Array
* @param data - Data to authenticate as Uint8Array
* @returns 32-byte HMAC as Uint8Array
*/
function hmacSha256(key: Uint8Array, data: Uint8Array): Uint8Array;Usage Example:
import { waitReady, hmacSha256 } from "@polkadot/wasm-crypto";
await waitReady();
const key = new TextEncoder().encode("secret-key");
const data = new TextEncoder().encode("Hello, HMAC-SHA256!");
const hmac = hmacSha256(key, data);
console.log("HMAC-SHA256 length:", hmac.length); // 32Hash-based Message Authentication Code using SHA-512.
/**
* Computes HMAC-SHA512
* @param key - Secret key as Uint8Array
* @param data - Data to authenticate as Uint8Array
* @returns 64-byte HMAC as Uint8Array
*/
function hmacSha512(key: Uint8Array, data: Uint8Array): Uint8Array;Usage Example:
import { waitReady, hmacSha512 } from "@polkadot/wasm-crypto";
await waitReady();
const key = new TextEncoder().encode("secret-key");
const data = new TextEncoder().encode("Hello, HMAC-SHA512!");
const hmac = hmacSha512(key, data);
console.log("HMAC-SHA512 length:", hmac.length); // 64Two-X hash function optimized for Polkadot applications with configurable rounds.
/**
* Computes Two-X hash
* @param data - Data to hash as Uint8Array
* @param rounds - Number of rounds (typically 2, 4, or 8)
* @returns Hash as Uint8Array (length depends on rounds: rounds * 8 bytes)
*/
function twox(data: Uint8Array, rounds: number): Uint8Array;Usage Example:
import { waitReady, twox } from "@polkadot/wasm-crypto";
await waitReady();
const data = new TextEncoder().encode("Hello, Two-X!");
// Different round configurations
const twox64 = twox(data, 1); // 8 bytes
const twox128 = twox(data, 2); // 16 bytes
const twox256 = twox(data, 4); // 32 bytes
console.log("Two-X 64 length:", twox64.length); // 8
console.log("Two-X 128 length:", twox128.length); // 16
console.log("Two-X 256 length:", twox256.length); // 32import {
waitReady,
sha256,
sha512,
blake2b,
keccak256,
keccak512,
twox
} from "@polkadot/wasm-crypto";
async function compareHashFunctions() {
await waitReady();
const data = new TextEncoder().encode("Compare hash functions");
// Compute various hashes
const sha256Hash = sha256(data);
const sha512Hash = sha512(data);
const blake2bHash = blake2b(data, new Uint8Array(0), 32);
const keccak256Hash = keccak256(data);
const keccak512Hash = keccak512(data);
const twoxHash = twox(data, 4); // 32 bytes
console.log("Hash comparison for same input:");
console.log("SHA-256: ", Array.from(sha256Hash.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join(''));
console.log("SHA-512: ", Array.from(sha512Hash.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join(''));
console.log("BLAKE2b: ", Array.from(blake2bHash.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join(''));
console.log("Keccak-256:", Array.from(keccak256Hash.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join(''));
console.log("Keccak-512:", Array.from(keccak512Hash.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join(''));
console.log("Two-X: ", Array.from(twoxHash.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join(''));
return {
sha256Hash,
sha512Hash,
blake2bHash,
keccak256Hash,
keccak512Hash,
twoxHash
};
}
compareHashFunctions();Install with Tessl CLI
npx tessl i tessl/npm-polkadot--wasm-crypto