Crypto Utilities API reference for cryptographic utility functions lock API Reference
Categories

Crypto Utilities

The Crypto utilities provide functions for generating unique identifiers and hash codes in JavaScript. These functions are useful for creating unique keys, generating consistent hashes for objects, and other cryptographic purposes.

Functions

tokenize

function tokenize(str = '')

Converts a string to a URL-friendly token.

Parameters

NameTypeDescription
strstringThe input string

Returns

The tokenized string.

Example

import { tokenize } from '@semantic-ui/utils';
console.log(tokenize('Hello World')); // "hello-world"
console.log(tokenize('A simple-test_string')); // "a-simple-test-string"

hashCode

function hashCode(input, { prettify = false, seed = 0x12345678 } = {})

Generates a hash code for the given input using an adapted UMASH algorithm.

Collision Resistance This is based on the UMASH algorithm which provides good performance and collission resistance.

Parameters

NameTypeDescription
inputanyThe value to hash
optionsobjectOptional configuration
Options
NameTypeDefaultDescription
prettifybooleanfalseIf true, returns a prettified string representation
seednumber0x12345678Seed value for the hash function

Returns

A 32-bit integer hash code, or a prettified string if prettify is true.

Example

import { hashCode } from '@semantic-ui/utils';
console.log(hashCode("Hello, world!")); // 2686193689
console.log(hashCode("Hello, world!", { prettify: true })); // "9ZX3P"
console.log(hashCode({ a: 1, b: [2, 3] })); // 3608339170

generateID

function generateID(seed = getRandomSeed())

Generates a prettified ID using a random or custom seed.

Parameters

NameTypeDescription
seednumberOptional seed value. If not provided, uses getRandomSeed()

Returns

A prettified alphanumeric ID with default 6-character minimum length.

Example

import { generateID } from '@semantic-ui/utils';
console.log(generateID()); // "A7B3X9"
console.log(generateID()); // "K2M8P4"
console.log(generateID(12345)); // "00009IX"
console.log(generateID(12345)); // "00009IX" (same seed produces same ID)

prettifyHash

function prettifyHash(numericHash, { minLength = 6, padChar = '0' } = {})

Converts a numeric hash value to a prettified alphanumeric string using base-36 encoding.

Parameters

NameTypeDescription
numericHashnumberThe numeric hash value to convert
optionsobjectOptional configuration
Options
NameTypeDefaultDescription
minLengthnumber6Minimum length of the output string. Will pad with padChar if necessary
padCharstring’0’Character to use for padding

Returns

The prettified hash string. Returns padded “0” if input parses to 0 or NaN.

Example

import { prettifyHash } from '@semantic-ui/utils';
console.log(prettifyHash(123)); // "00003F"
console.log(prettifyHash(123, { minLength: 8 })); // "0000003F"
console.log(prettifyHash(123, { minLength: 4, padChar: 'X' })); // "XX3F"
console.log(prettifyHash(0)); // "000000"

getRandomSeed

function getRandomSeed()

Generates a cryptographically secure random seed value.

Returns

A random 32-bit unsigned integer. Uses crypto.getRandomValues when available, falls back to Math.random.

Example

import { getRandomSeed } from '@semantic-ui/utils';
console.log(getRandomSeed()); // 2949673445
console.log(getRandomSeed()); // 1234567890
Previous
Colors
Next
Dates