You may have used hashids and I bet you love how the library encodes numeric values into awesome and short ids.
This library was born from my efforts to achieve the same very short ids using strings/text, not supported by hashids.
After a lot of tinkering, I figured that it was possible to use the CRC-32 algorithm as an intermediary. This generates a short numeric hash from the string which is then converted to a hashid! In between we perform some "padding" for negative CRC-32 hashes as hashids only supports positive integers.
Simple and effective!
Of course ids these are not intended to be cryptographically safe!
Install: yarn add string-hashids
Then:
let sHash = require("string-hashids");
let str = 'After a lot of tinkering, I figured that it was possible to use the CRC-32 algorithm as an intermediary. This generates a short numeric hash from the string which is then converted to a hashid! In between we perform some "padding" for negative CRC-32 hashes as hashids only supports positive integers.';
let hash = sHash.encode(str, "my salt");
console.log(hash); //ERfnG8y6O
encode(your_string, [your_salt])
Note:
- The string and salt values are automatically stringified using toString()
- Salt is an optional value.