Seeded-Hashids
Generate seeded Hashids that is unique per seed.
Seeded-Hashids is an easy to use library to generate seeded Hashids which is unique to a seed that can be based on a user or group. Hide the raw ids, hex strings, objectids or uuids from end users and reduces the number of database calls that check for valid or existing ids.
Works well with databases that has numeric keys or hex strings. Your database will contain only the original ids as there is no need to store the encoded versions. UUIDs and MongoDB's ObjectIDs are hex strings.
An example is to generate Hashids that are unique to a particular application. Even if multiple applications shared their userids with each other, the users could not be correlated or identified by their userids.
Sample Scenario
Encoding the userids and using their actual userid as a seed.
The userids are unique and never revealed to end users.
User A (ID 123)
User B (ID 456)
User C (ID 789)
Application A sees User B as 'zxcqwe' and sees User C as 'bnmrty'
Application B sees User A as 'qweasd' and sees User C as 'rtyjkl'
Application C sees User A as 'fghzxc' and sees User B as 'asdiop'
'asdiop' is supposedly only visible to Application C.
If Application A decodes 'asdiop', it decodes to an empty string.
Getting started
Install Seeded-Hashids via:
Sample code:
const seededHashids = ;const ObjectId = TypesObjectId;const scopes = scope: 'user' salt: 'some-salt'; seededHashids; let encoded decoded; // Encoding hex stringsencoded = seededHashids;decoded = seededHashids;console; // 'bNVA3Q9g'console; // 'abcd1234' // Encoding hex strings with seedencoded = seededHashids;decoded = seededHashids;console; // 'S4RTRZ2L'console; // 'abcd1234' // If a wrong seed is used for decodeHex, will decode to a different outputdecoded = seededHashids;console; // '' (Empty string) // Decoding ObjectIds, same as hex but needs to be 24 characters hex stringencoded = seededHashids;decoded = seededHashids;console; // '4Wg453PYPrdhAEdyeMYWpm'console; // ObjectId('abcd1234abcd1234abcd1234') // Decoding ObjectIds with seed, same as hex but needs to be 24 characters hex stringencoded = seededHashids;decoded = seededHashids;console; // 'QX3Bu2pNSTnPEZFg6sW5EY'console; // ObjectId('abcd1234abcd1234abcd1234') // Encoding positive integersencoded = seededHashids;decoded = seededHashids;console; // 'nY9AyaDn'console; // 12345678 // Encoding positive integers with seedencoded = seededHashids;decoded = seededHashids;console; // 'aNq4PsAx'console; // 12345678 // If a wrong seed is used for decode, will decode to a different outputdecoded = seededHashids;console; // NaN (Different output) // Encoding array of positive integersencoded = seededHashids;decoded = seededHashids;console; // '6ZsyFeUKc5fahqS5'console; // [1,2,3,4,5,6,7,8] // Encoding array of positive integers with seedencoded = seededHashids;decoded = seededHashids;console; // '9bzhGs3kHZVJs7wm'console; // [1,2,3,4,5,6,7,8]
API
undefined
initialize (options) : noResult To set up the required scopes and other parameters.
seededHashids;
Object
options Field | Required | Type | Defaults |
---|---|---|---|
scopes | yes | Array |
- |
charset | no | String |
a-z, A-Z, 2-9 without i, I, o, O, 1, 0 to increase readibility |
minOutputLength | no | Number |
8 |
shuffleOutput | no | Boolean |
true |
objectId | no | ObjectId |
- |
shuffleFunction | no | Function |
Built-in shuffle function |
unshuffleFunction | no | Function |
Built-in unshuffle function |
Array
scopes - The array is a list of scope object that contains a scope string and a salt string.
- Each scope could be then name of a class or an object type.
- Scopes have to be unique.
- Salts have to be unique.
let scope = scope: 'user' salt: 'some-salt' scope: 'profile' salt: 'another-salt';
String
(optional)
charset - This value is passed directly to Hashids.
- A minimum of 16 unique characters are required.
let charset = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
Number
(optional)
minOutputLength - This value is passed directly to Hashids, which adds padding to reach the length required.
let minOutputLength = 8;
Boolean
(optional)
shuffleOutput - This value determines if the output seeded-hashid will be shuffled after encoding and before decoding by Hashids.
- The output is shuffled based on the seed and attempts to prevent decoding using a wrong seed.
- If no seed is provided, the output seeded-hashid will not be shuffled.
let shuffleOutput = true;
Function
(optional)
objectId - This object is required only if there is a need to cast the decoding output to an ObjectId using
.decodeObjectId
. - Can pass in
require('mongoose').Types.ObjectId
orrequire('mongodb').ObjectId
or functions.
let objectId = TypesObjectId;
Function
(optional)
shuffleFunction - Change the shuffle function used.
- The shuffle function needs to accept (inputString, seedString) and returns an outputString.
let { return ;};
Function
(optional)
unshuffleFunction - Change the unshuffle function used.
- The unshuffle function needs to accept (inputString, seedString) and returns an outputString.
let { return ;};
String
encode (scope, data, [seed]) : seededHashid To encode positive numbers.
let userId = seededHashids;
String
scope - This scope should be the same scope string that was used during initialization.
Number
or Array of Numbers
data - The positive number or the array of positive numbers to be encoded.
String
(optional)
seed - This seed is used to encode a seeded-hashid that is unique to the seed.
String
encodeHex (scope, hex, [seed]) : seededHashid To encode hex strings.
let userId = seededHashids;
String
scope - This scope should be the same scope string that was used during initialization.
String
hex - This hex string to be encoded.
String
(optional)
seed - This seed is used to encode a seeded-hashid that is unique to the seed.
Number
or Array of Numbers
decode (scope, seededHashid, [seed]) : decodedData To decode seeded-hashid into a positive number or an array of positive numbers. Returns NaN if unable to decode.
let userId = seededHashids;
String
scope - This scope should be the same scope string that was used during initialization.
String
seededHashid - This seeded-hashid to be decoded.
String
(optional)
seed - This seed is used to decode a seeded-hashid that is unique to the seed.
String
decodeHex (scope, seededHashid, [seed]) : decodedHex To decode seeded-hashid into a hex string. Returns an empty string if unable to decode.
let userId = seededHashids;
String
scope - This scope should be the same scope string that was used during initialization.
String
seededHashid - This seeded-hashid to be decoded.
String
(optional)
seed - This seed is used to decode a seeded-hashid that is unique to the seed.
ObjectId
decodeObjectId (scope, seededHashid, [seed]) : decodedObjectId To decode seeded-hashid into an objectId. Returns NaN if unable to decode.
let userId = seededHashids;
String
scope - This scope should be the same scope string that was used during initialization.
String
seededHashid - This seeded-hashid to be decoded.
String
(optional)
seed - This seed is used to decode a seeded-hashid that is unique to the seed.
undefined
reset () : noResult To reset seededHashids, needs to initialize() again before usage.
seededHashids;
Boolean
isInitialized () : isInitialized To check if seededHashids is initialized.
let isInitialized = seededHashids;
Array
getScopes () : scopes To get the string array of scopes.
let scopes = seededHashids;
String
getCharset () : charset To get the charset string.
let charset = seededHashids;
Number
getMinOutputLength () : minOutputLength To get the minimum output seeded-hashid length.
let minOutputLength = seededHashids;
Boolean
getShuffleOutput () : shuffleOutput To check if the output will be shuffled if a seed is provided.
let shuffleOutput = seededHashids;
Function
getObjectId () : objectId To get the objectId function to see if available.
let objectId = seededHashids;
Function
getShuffleFunction () : shuffleFunction To get the shuffle function used.
let shuffleFunction = seededHashids;
Function
getUnshuffleFunction () : unshuffleFunction To get the unshuffle function used.
let unshuffleFunction = seededHashids;
Recommendations
- Charset should not be too short.
- Salts should not be too short.
- Seeds should not be too short. Recommended to use long hex strings such as ObjectIds or UUIDs.
- Encode longer input hex strings such as ObjectIds or UUIDs.
- Always validate the output after decoding.
- The minOutputLength should not be too small.
- Leave the shuffleOutput as true, which is the default value.
- Encode and decode as required, recommended for database to contain only original ids or hex strings.
Pitfalls
- Encoding of an array of numbers is supported but the numbers within are not individually shuffled.
- Encoding of negative numbers are not supported.
- Required to pass in the correct type of parameters in order to prevent the encoding of invalid seeded-hashids by accident, such as encoding
"[object Object]"
. - It could still be possible for a different seed to decode a seeded-hashid, but it is really rare if the recommendations are followed.
- Upgrade to a major version after testing as the output seeded-hashids may have changed.
- Do not use this library as a security tool and do not encode sensitive data. This is not an encryption library.
License
MIT License. See the LICENSE file.