Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More β†’
Socket
Sign inDemoInstall
Socket

github.com/pablosichert/random-hash

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/pablosichert/random-hash

  • v4.0.1+incompatible
  • Source
  • Go
  • Socket score

Version published
Created
Source

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 --|
|--- πŸ˜‡ ---|---- πŸ‘» ----|--- βœ‹ ---| = 'πŸ˜‡πŸ‘»βœ‹'

FAQs

Package last updated on 27 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc