
Enter a search term above to see results...
Enter a search term above to see results...
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.
function tokenize(str = '')
Converts a string to a URL-friendly token.
Name | Type | Description |
---|---|---|
str | string | The input string |
The tokenized string.
import { tokenize } from '@semantic-ui/utils';
console.log(tokenize('Hello World')); // "hello-world"console.log(tokenize('A simple-test_string')); // "a-simple-test-string"
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.
Name | Type | Description |
---|---|---|
input | any | The value to hash |
options | object | Optional configuration |
Name | Type | Default | Description |
---|---|---|---|
prettify | boolean | false | If true, returns a prettified string representation |
seed | number | 0x12345678 | Seed value for the hash function |
A 32-bit integer hash code, or a prettified string if prettify
is true.
import { hashCode } from '@semantic-ui/utils';
console.log(hashCode("Hello, world!")); // 2686193689console.log(hashCode("Hello, world!", { prettify: true })); // "9ZX3P"console.log(hashCode({ a: 1, b: [2, 3] })); // 3608339170
function generateID(seed = getRandomSeed())
Generates a prettified ID using a random or custom seed.
Name | Type | Description |
---|---|---|
seed | number | Optional seed value. If not provided, uses getRandomSeed() |
A prettified alphanumeric ID with default 6-character minimum length.
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)
function prettifyHash(numericHash, { minLength = 6, padChar = '0' } = {})
Converts a numeric hash value to a prettified alphanumeric string using base-36 encoding.
Name | Type | Description |
---|---|---|
numericHash | number | The numeric hash value to convert |
options | object | Optional configuration |
Name | Type | Default | Description |
---|---|---|---|
minLength | number | 6 | Minimum length of the output string. Will pad with padChar if necessary |
padChar | string | ’0’ | Character to use for padding |
The prettified hash string. Returns padded “0” if input parses to 0 or NaN.
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"
function getRandomSeed()
Generates a cryptographically secure random seed value.
A random 32-bit unsigned integer. Uses crypto.getRandomValues when available, falls back to Math.random.
import { getRandomSeed } from '@semantic-ui/utils';
console.log(getRandomSeed()); // 2949673445console.log(getRandomSeed()); // 1234567890