Enter a search term above to see results...
On This Page
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
| Name | Type | Description |
|---|---|---|
| str | string | The 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
| Name | Type | Description |
|---|---|---|
| input | any | The value to hash |
| options | object | Optional configuration |
Options
| Name | Type | Default | Description |
|---|---|---|---|
| prettify | boolean | false | If true, returns a prettified string representation |
| seed | number | 0x12345678 | Seed 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!")); // 2686193689console.log(hashCode("Hello, world!", { prettify: true })); // "9ZX3P"console.log(hashCode({ a: 1, b: [2, 3] })); // 3608339170generateID
function generateID(seed = getRandomSeed())Generates a prettified ID using a random or custom seed.
Parameters
| Name | Type | Description |
|---|---|---|
| seed | number | Optional 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
| Name | Type | Description |
|---|---|---|
| numericHash | number | The numeric hash value to convert |
| options | object | Optional configuration |
Options
| 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 |
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()); // 2949673445console.log(getRandomSeed()); // 1234567890