random-hash
Installation
npm install random-hash
Usage
Generating hashes - optional configuration in function argument
; ; // '0yyv6Z' ; // 'KLgF' ; // '😝🤓😎😇😝🤔' ; // 'aaaaaa'
Creating a stateful function object that stores its configuration and can be manipulated from the outside
;; // With options (default values)const generateHash = length: 6 charset: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_' rng: randomBytes; // Without optionsconst generateHash = ; ; // 'VE5xn-' ; // 'E2s4' ; // 'O1oJkK' // Permanently setting hash lengthgenerateHashlength = 4; ; // 'AyHK' // Permanently setting charsetgenerateHashcharset = '😁' '😎' '😍' '😇' '🤓' '🤔' '😴'; ; // '😴😁😁😍' generateHash randomBytes; ; // '😁😁😁😁'
generateHash
Algorithm for |----------- a1 ----------| ... |------------------------- a(n) ------------------------|
| b1 b2 b3 b4 b5 b6 b7 b8 | ... | b(n-7) b(n-6) b(n-5) b(n-4) b(n-3) b(n-2) b(n-1) b(n) |
|--- c1 ---| ... |-- c(n) --|
a(n): binary with 8 digits
b(n): binary from RNG
c(n): binary with log2(charset.length) digits
a1 ... a(ceil(hashLength * log2(charset.length) / 8))
b1 ... b(ceil(hashLength * log2(charset.length) / 8) * 8)
c1 ... c(hashLength)
charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_'
length = 4
|------- a1 ------|------- b2 ------|------- c3 ------|
| 0 1 1 0 0 0 1 1 | 0 1 0 1 0 1 0 0 | 0 0 0 0 0 0 0 1 | random bytes
|---- c1 ----|----- c2 ----|----- c3 ----|---- c4 ----|
|----- y ----|----- 1 -----|----- q -----|----- b ----| = 'y1qb'
Example
charset = ['😁', '😎', '😍', '😇', '🤓', '🤔', '😴', '😝', '🤑', '🤒', '😭', '😈', '👻', '👽', '🤖', '💩', '🎅', '💪', '👈', '👉', '👆', '👇', '✌', '✋', '👌', '👍', '👎', '👐', '👂', '👃', '👣', '👁']
length = 3
|------- a1 ------|------- b2 ------|
| 0 0 0 1 1 0 1 1 | 0 0 1 0 1 1 1 0 | random bytes
|--- c1 ---|---- c2 ---|--- c3 --|
|--- 😇 ---|---- 👻 ----|--- ✋ ---| = '😇👻✋'