import { base62encode } from './base62encode.js';
/**
* Hashes the input with the sha256 algorithm and truncates the hexadecimal hash length
* to the desired size. The result will be encoded to alphanumeric characters
* using base62.
*
* Truncating a sha2 hash is considered safe, but reducing the amount of bits
* increases the risk of collision. Default truncation value is 12-hex which results
* in a 48bit integer. The resulting base62 ID will moslty have a length of mostly 8 characters.
*
* @param {string} input
* @param {number} truncate default = 12
* @returns encoded id
*/
export async function generateShortId(input, truncate = 12) {
const hashjs = await import('hash.js');
const hash = hashjs.default.sha256().update(input).digest('hex');
const shortened = (truncate) ? hash.substring(0, truncate) : hash;
return base62encode("0x" + shortened);
}