string-hashids
v0.1.0
Published
Like Hashids, this is a small JavaScript library to generate YouTube-like ids from strings.
Downloads
85
Maintainers
Readme
Why?
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!
To Use
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
API
encode(your_string, [your_salt])
Note:
- The string and salt values are automatically stringified using toString()
- Salt is an optional value.