Giter VIP home page Giter VIP logo

random-hash's Introduction

random-hash

NPM version Build status Coverage status Dependency status Dev dependency status

Installation

npm install random-hash

Usage

Generating hashes - optional configuration in function argument

import generateHash from 'random-hash';

generateHash(); // '0yyv6Z'

generateHash({ length: 4 }); // 'KLgF'

generateHash({
    charset: ['๐Ÿ˜', '๐Ÿ˜Ž', '๐Ÿ˜', '๐Ÿ˜‡', '๐Ÿค“', '๐Ÿค”', '๐Ÿ˜ด', '๐Ÿ˜']
}); // '๐Ÿ˜๐Ÿค“๐Ÿ˜Ž๐Ÿ˜‡๐Ÿ˜๐Ÿค”'

generateHash({
    rng: randomBytes => new Array(randomBytes).fill(0)
}); // 'aaaaaa'

Creating a stateful function object that stores its configuration and can be manipulated from the outside

import { RandomHash } from 'random-hash';
import { randomBytes } from 'crypto';

// With options (default values)
const generateHash = new RandomHash({
    length: 6,
    charset: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_',
    rng: randomBytes
});

// Without options
const generateHash = new RandomHash;

generateHash(); // 'VE5xn-'

generateHash({ length: 4 }); // 'E2s4'

generateHash(); // 'O1oJkK'

// Permanently setting hash length
generateHash.length = 4;

generateHash(); // 'AyHK'

// Permanently setting charset
generateHash.charset = ['๐Ÿ˜', '๐Ÿ˜Ž', '๐Ÿ˜', '๐Ÿ˜‡', '๐Ÿค“', '๐Ÿค”', '๐Ÿ˜ด'];

generateHash(); // '๐Ÿ˜ด๐Ÿ˜๐Ÿ˜๐Ÿ˜'

generateHash.rng = randomBytes => new Array(randomBytes).fill(0);

generateHash(); // '๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ˜'

Algorithm for generateHash

|----------- 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 --|
|--- ๐Ÿ˜‡ ---|---- ๐Ÿ‘ป ----|--- โœ‹ ---| = '๐Ÿ˜‡๐Ÿ‘ปโœ‹'

random-hash's People

Contributors

pablosichert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.