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

@bemoje/hashing

v1.0.0

Published

Hashing made very easy. Simple port of some great tools. Most used options off them are the only options exposed here. All the node crypto library's hashing algorithms are all available.

Downloads

5

Readme

@bemoje/hashing

Hashing made very easy. Simple port of some great tools. Most used options off them are the only options exposed here. All the node crypto library's hashing algorithms are all available.

Various hashing methods in one bundle. A port of the very popular 'node-object-hash'-module is included, and then for getting really short strings or integers from a hasher, a djb2 xor hasher is included, which can return both integers or strings (selectable option, but default radix 36 simple conversion.). Set that to 16 to get hex, for example.

install

npm install --save @bemoje/hashing

Docs

import NodeObjectHash from 'node-object-hash'
import { toJson } from '@bemoje/serializer'

/**
 * A djb2_xor string hasher. Returns an integer as woulde be expected by default from djb2. But there is also a
 * string-verison, that simply takes the output and calls .toString(radix=36) on it. The radix is an option.
 * If a non-string is received on the input, the '@bemoje/serializer'-module is used to serialize first, and then hash that. For more advanced
 * object hashing, use @see oHash method.
 *
 * @class strHash
 */
export class strHash {
	/**
	 * djb2_xor string hasher. Returns an integer as woulde be expected by default from djb2.
	 *
	 * If a non-string is received, @bemoje/serializer is used to serialize first, and then hash that. For more advanced
	 * object hashing, use @see oHash method.
	 * @static
	 * @param {string} str
	 * @returns {integer}
	 */
	static toInteger(str: String): Number

	/**
	 * djb2_xor string hasher. Returns a radix 36 string, but the radix can be defined. If undefined, returns the integer as
	 * woulde be expected by default from djb2.
	 * To get hex strings, set radix to 16.
	 *
	 * If a non-string is received, @bemoje/serializer is used to serialize first, and then hash that. For more advanced
	 * object hashing, use @see oHash method.
	 *
	 * @static
	 * @param {string} str
	 * @param {string} [toStringRadix=36]
	 * @returns {string}
	 */
	static toString(str: String, toStringRadix?: Number = 36): String
}

/**
 * A port of the very popular 'node-object-hash'-module with just some of the options available.
 * @method oHash
 * @property {object} options - The options are not passed to the funcitons. Just set them directly as so: "oHash.options.alg = 'sha512'"
 *
 * @param {*} object - The object to hash.
 * @returns {string}
 */
export function oHash(object: Object): String

/**
 * The sorter from the 'node-object-hash' module can be very handy also. It has its own options that can be set on here as well.
 * @see oHash
 *
 * @method oHashSorter
 * @property {object} options - The options are not passed to the funcitons. Just set them directly as so: "oHash.options.alg = 'sha512'"
 *
 * @param {*} object - The object to hash.
 * @returns {string}
 */
export function oHashSorter(object: Object): String

/**
 * Just a simplified/defaulted port of the 'node-object-hash' module for simplicity.
 * The exposed options that can be set are 'sort', 'coerce', 'trim' and 'alg' (algorithm).
 */
const options = {
	sort: { map: true, array: false, object: true, set: true },
	coerce: false,
	trim: false,
	alg: 'sha1',
}

export default { strHash, oHash, oHashSorter }

usage

import { strHash, oHash, oHashSorter } from '@bemoje/hashing'

const oTest = { a: 2, b: 4 }
const strTest = '{ a: 2, b: 4 }'

oHash.options.alg = 'sha1' // default

console.log({
	fn: 'oHash',
	arg: oTest,
	return: oHash(oTest),
	note: 'object hashing - sha1 algorithm (default).',
})

//

oHash.options.alg = 'sha512'

console.log({
	fn: 'oHash',
	arg: oTest,
	return: oHash(oTest),
	note: 'object hashing - sha512 algorithm.',
})

//

oHash.options.alg = 'md5'

console.log({
	fn: 'oHash',
	arg: oTest,
	return: oHash(oTest),
	note: 'object hashing - md5 algorithm.',
})

//

console.log({
	fn: 'oHashSorter',
	arg: oTest,
	return: oHashSorter(oTest),
	note: 'oHashSorter - defaults',
})

//

console.log({
	fn: 'strHash.toInteger',
	arg: strTest,
	return: strHash.toInteger(strTest),
	note: 'djb2 string hashing - return integer.',
})

//

console.log({
	fn: 'strHash.toString',
	arg: strTest,
	return: strHash.toString(strTest),
	note: 'djb2 string hashing - return string',
})

//

console.log({
	fn: 'strHash.toString',
	arg: oTest,
	return: strHash.toString(oTest),
	note:
		'djb2 string hashing - object input - automatically serialized, then that is hashed',
})

//

console.log({
	fn: 'strHash.toString',
	arg: oTest,
	return: strHash.toString(oTest, 16),
	note: 'djb2 string hashing - radix 36 (default) now 16 -> return hex string',
})

/*{
   fn: 'oHash',
   arg: { a: 2, b: 4 },
   return: '14d291add23967c267e2cd13ce4246f15f115329',
   note: 'object hashing - sha1 algorithm (default).'
 }
 {
   fn: 'oHash',
   arg: { a: 2, b: 4 },
   return: '2a8fcb4868846348f7fb6e74e3d9c131eff66ad0e03bdb38f68eacbb494f9231171ec90f0bf1a7f2596f92c05c2a0aefcdd02dfcb624d6bd6fb2dacf8687bebd',
   note: 'object hashing - sha512 algorithm.'
 }
 {
   fn: 'oHash',
   arg: { a: 2, b: 4 },
   return: 'f33f3234f5a9f63d3ed07db0fcb42794',
   note: 'object hashing - md5 algorithm.'
 }
 {
   fn: 'oHashSorter',
   arg: { a: 2, b: 4 },
   return: '{a:2,b:4}',
   note: 'oHashSorter - defaults'
 }
 {
   fn: 'strHash.toInteger',
   arg: '{ a: 2, b: 4 }',
   return: 3015698442,
   note: 'djb2 string hashing - return integer.'
 }
 {
   fn: 'strHash.toString',
   arg: '{ a: 2, b: 4 }',
   return: '1dvgvt6',
   note: 'djb2 string hashing - return string'
 }
 {
   fn: 'strHash.toString',
   arg: { a: 2, b: 4 },
   return: '10yrphm',
   note: 'djb2 string hashing - object input - automatically serialized, then that is hashed'
 }
 {
   fn: 'strHash.toString',
   arg: { a: 2, b: 4 },
   return: '853a2bca',
   note: 'djb2 string hashing - radix 36 (default) now 16 -> return hex string'
 }
 */