@bemoje/hashing

1.0.0 • Public • Published

@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.

  • version 1.0.0
  • author Benjamin Møller Jensen bemoje@gmail.com
  • date July, 2020

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'
 }
 */

Package Sidebar

Install

npm i @bemoje/hashing

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

14.4 kB

Total Files

3

Last publish

Collaborators

  • bemoje