hash-code-string is a small utility package to convert long strings into shorter, hash-like strings. It provides functions to create hashes, encode them in a URL-safe base64 format, and decode them back into bytes, or a 32 bit number if desired. Limited to 32 bits of uniqueness. No dependencies, the hashing code is 5 lines. Assumes environment has Uint8Array
, Math.imul
, and btoa
.
npm install hash-code-string
pnpm install hash-code-string
import { hashString, hashToNum32 } from 'hash-code-string';
// Example usage
const longString = "This is a long string that needs to be hashed";
const hash = hashString(longString);
console.log(`Hash: ${hash}`); // Hash: g3g2qw
const decodedHash = hashToNum32(hash);
console.log(`Decoded Hash: ${decodedHash}`); // Decoded Hash: -1422493565
Creates a hash code number from a string similar to Java's String.hashCode method.
Parameters:
- str: The input string to hash.
Returns:
A 32-bit integer hash code.
Splits a 32-bit number into an array of 4 bytes.
Parameters:
- num: The 32-bit integer to split.
Returns:
A Uint8Array containing 4 bytes.
Converts an array of byte values to a string.
Parameters:
- bytes: An array of bytes.
Returns:
A string representing the byte values.
Encodes a string into a URL-safe base64 format.
Parameters:
- str: The input string to encode.
Returns:
A URL-safe base64 encoded string.
Creates a hash of the input string and encodes it in URL-safe base64 format.
Parameters:
- str: The input string to hash and encode.
Returns:
A URL-safe base64 encoded hash string.
Decodes a URL-safe base64 string.
Parameters:
- str: The URL-safe base64 string to decode.
Returns:
The decoded string.
Converts a string to an array of character codes.
Parameters:
- str: The input string.
Returns:
An array of character codes.
Converts the first 4 elements of a byte array back to a 32-bit number.
Parameters:
- bytes: An array of bytes.
Returns:
A 32-bit integer.
const str = "example";
const hash = hashString(str);
console.log(hash); // Output: URL-safe base64 encoded hash
const decoded = decode64(hash);
console.log(decoded); // Output: decoded hash string
const charCodes = stringToCodes(decoded);
console.log(charCodes); // Output: Array of character codes
const num = bytesToNum32(charCodes);
console.log(num); // Output: 32-bit number
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
This project is licensed under the ISC License. See the LICENSE file for details.