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

@konfirm/checksum

v2.0.2

Published

Calculate checksums on any kind of value, sorting keys for objects for consistency

Downloads

204

Readme

tests release

Checksum

Calculate checksums on any variable type, including objects.

Installation

Checksum is a scoped package, which means both the installation and require (or import) need the scope along with the package name:

$ npm install --save @konfirm/checksum

Exports

| name | description | | ------ | ------------------------------------------------- | | hash | Calculate the Hash (checksum) for the given value | | hmac | Calculate the HMAC (checksum) for the given value |

Usage

As of version 2.0 the Checksum package has support for TypeScript, JavaScript ES Modules (import) and JavaScript CommonJS (require). Prior versions only support CommonJS, as the package was built using it.

JavaScript (CommonJS)

const { hash } = require('@konfirm/checksum');

//  create an object
const objectA = { greet: 'Hello World', count: Infinity, value: ['a', 'b'] };
//  create a second object, using a different order of the same key/value pairs
const objectB = { value: ['a', 'b'], greet: 'Hello World', count: Infinity };

//  create a hash (using the default algorithm and digest) of both objects
const hashA = hash(objectA);
const hashB = hash(objectB);

console.log({
	hashA, // 68607defdaf491bab7dbf54710c5cdfaeef11885a23c484fabc0bc7dda8f600f
	hashB, // 68607defdaf491bab7dbf54710c5cdfaeef11885a23c484fabc0bc7dda8f600f
	hashes_equal: hashA === hashB, // true
	json_equal: JSON.stringify(objectA) === JSON.stringify(objectB), // false
});

Using the "default" import

The demonstrated syntax in versions prior to 2.0 used the so-called default import style, e.g.

const Checksum = require('@konfirm/checksum');

//  as in the code above, the `hash` function is now not available directly, but only via `Checksum`
const hashA = Checksum.hash(objectA);

This still works, though do consider using the direct import/extraction style, as that is the standard for ES Modules and Typescript.

JavaScript (ES Modules)

import { hash } from '@konfirm/checksum';

//  create an object
const objectA = { greet: 'Hello World', count: Infinity, value: ['a', 'b'] };
//  create a second object, using a different order of the same key/value pairs
const objectB = { value: ['a', 'b'], greet: 'Hello World', count: Infinity };

//  create a hash (using the default algorithm and digest) of both objects
const hashA = hash(objectA);
const hashB = hash(objectB);

console.log({
	hashA, // 68607defdaf491bab7dbf54710c5cdfaeef11885a23c484fabc0bc7dda8f600f
	hashB, // 68607defdaf491bab7dbf54710c5cdfaeef11885a23c484fabc0bc7dda8f600f
	hashes_equal: hashA === hashB, // true
	json_equal: JSON.stringify(objectA) === JSON.stringify(objectB), // false
});

Mimicking the "default" import

We are not advocating this style, though it may be beneficial to at least be aware of it. If, for some reason, the "default" is needed, JavaScript allows for this syntax.

import * as Checksum from '@konfirm/checksum';

//  as in the code above, the `hash` function is now not available directly, but only via `Checksum`
const hashA = Checksum.hash(objectA);

TypeScript

import { hash } from '@konfirm/checksum';

//  create an object
const objectA: object = { greet: 'Hello World', count: Infinity, value: ['a', 'b'] };
//  create a second object, using a different order of the same key/value pairs
const objectB: object = { value: ['a', 'b'], greet: 'Hello World', count: Infinity };

//  create a hash (using the default algorithm and digest) of both objects
const hashA: string = hash(objectA);
const hashB: string = hash(objectB);

console.log({
	hashA, // 68607defdaf491bab7dbf54710c5cdfaeef11885a23c484fabc0bc7dda8f600f
	hashB, // 68607defdaf491bab7dbf54710c5cdfaeef11885a23c484fabc0bc7dda8f600f
	hashes_equal: hashA === hashB, // true
	json_equal: JSON.stringify(objectA) === JSON.stringify(objectB), // false
});

Mimicking the "default" import

We are not advocating this style, though it may be beneficial to at least be aware of it. If, for some reason, the "default" is needed, TypeScript allows for this syntax.

import * as Checksum from '@konfirm/checksum';

//  as in the code above, the `hash` function is now not available directly, but only via `Checksum`
const hashA: string = Checksum.hash(objectA);

API

hash(value: any, algorithm: string = 'sha256', digest: string = 'hex'): string

Calculates the checksum using a Hash object with the optional algorithm (default 'sha256') and returns the created hash as a string using the optional digest method (default 'hex')

TypeScript

import { hash } from '@konfirm/checksum';
const output: string = hash('the quick brown fox jumps over the lazy dog');

console.log(output); // 05c6e08f1d9fdafa03147fcb8f82f124c76d2f70e3d989dc8aadb5e7d7450bec

JavaScript (ES Modules)

import { hash } from '@konfirm/checksum';
const output = hash('the quick brown fox jumps over the lazy dog');

console.log(output); // 05c6e08f1d9fdafa03147fcb8f82f124c76d2f70e3d989dc8aadb5e7d7450bec

JavaScript (CommonJS)

const { hash } = require('@konfirm/checksum');
const output = hash('the quick brown fox jumps over the lazy dog');

console.log(output); // 05c6e08f1d9fdafa03147fcb8f82f124c76d2f70e3d989dc8aadb5e7d7450bec

hmac(secret: string, value: any, algorithm: string = 'sha256', digest: string = 'hex'): string

Calculates the checksum using an HMAC object using the provided secret with the optional algorithm (default 'sha256') and returns the created hash as a string using the optional digest method (default 'hex'). Although the use is different, it can be used as a salted checksum.

TypeScript

import { hmac } from '@konfirm/checksum';
const output: string = hmac('my secret', 'the quick brown fox jumps over the lazy dog');

console.log(output); // 216262dbc93d393b146b181b966df3525d979499d05f99a00a185edfe425df6e

JavaScript (ES Modules)

import { hmac } from '@konfirm/checksum';
const output = hmac('my secret', 'the quick brown fox jumps over the lazy dog');

console.log(output); // 216262dbc93d393b146b181b966df3525d979499d05f99a00a185edfe425df6e

JavaScript (CommonJS)

const { hmac } = require('@konfirm/checksum');
const output = hmac('my secret', 'the quick brown fox jumps over the lazy dog');

console.log(output); // 216262dbc93d393b146b181b966df3525d979499d05f99a00a185edfe425df6e

Migrating from version 1.0 to 2.0

Removed features

Several previously public available members have been removed from the package

  • verifyAlgorithmAndDigest method, this has been removed as its internal mechanics have been improved to safeguard the values given
  • ALGORITHMS property, this has always been the crypto.getHashes() output; if the list is needed, please update to use the getHashes method instead.
  • DIGEST_METHODS, this used to provide an array with the values 'hex'', 'base64' and 'latin1'

The digest method 'latin1' has been removed from the supported options, leaving only 'hex' and 'base64'

As the Checksum package did not have TypeScript or ES Module support before, users of those may be able to remove any workaround needed (not aware of any), and TypeScript users now have better type hinting (even though the package is mainly strings on the outside).

For the CommonJS users importing the entire library (const Checksum = require('@konfirm/checksum');), that syntax still works, though we (now) recommend picking only what is needed.

const { hmac } = require('@konfirm/checksum');

License

MIT License Copyright (c) 2017-2021 Rogier Spieker (Konfirm)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.