npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

random-hash

v4.0.1

Published

Generate random hashes with a fully customizable charset and configurable length

Downloads

3,928

Readme

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 --|
|--- 😇 ---|---- 👻 ----|--- ✋ ---| = '😇👻✋'