Crypto Utilities API reference for cryptographic utility functions lock Guide

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

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()

Generates a unique identifier.

Returns

A string representing a unique identifier.

Example

import { generateID } from '@semantic-ui/utils';
console.log(generateID()); // "1A2B3C4D"
console.log(generateID()); // "5E6F7G8H"

prettifyID

function prettifyID(num)

Converts a number into a more readable string representation.

Parameters

NameTypeDescription
numnumberThe number to convert

Returns

A string representation of the number using alphanumeric characters.

Example

import { prettifyID } from '@semantic-ui/utils';
console.log(prettifyID(123456)); // "1E240"
console.log(prettifyID(0)); // "0"