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 build using it.
JavaScript (CommonJS)
const { hash } = require('@konfirm/checksum');
const objectA = { greet: 'Hello World', count: Infinity, value: ['a', 'b'] };
const objectB = { value: ['a', 'b'], greet: 'Hello World', count: Infinity };
const hashA = hash(objectA);
const hashB = hash(objectB);
console.log({
hashA,
hashB,
hashes_equal: hashA === hashB,
json_equal: JSON.stringify(objectA) === JSON.stringify(objectB),
});
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');
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';
const objectA = { greet: 'Hello World', count: Infinity, value: ['a', 'b'] };
const objectB = { value: ['a', 'b'], greet: 'Hello World', count: Infinity };
const hashA = hash(objectA);
const hashB = hash(objectB);
console.log({
hashA,
hashB,
hashes_equal: hashA === hashB,
json_equal: JSON.stringify(objectA) === JSON.stringify(objectB),
});
Mimicing the "default" import
Whilst not advocating this style, 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';
const hashA = Checksum.hash(objectA);
TypeScript
import { hash } from '@konfirm/checksum';
const objectA: object = { greet: 'Hello World', count: Infinity, value: ['a', 'b'] };
const objectB: object = { value: ['a', 'b'], greet: 'Hello World', count: Infinity };
const hashA: string = hash(objectA);
const hashB: string = hash(objectB);
console.log({
hashA,
hashB,
hashes_equal: hashA === hashB,
json_equal: JSON.stringify(objectA) === JSON.stringify(objectB),
});
Mimicing the "default" import
Whilst not advocating this style, 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';
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 algorith (default 'sha256'
) and return 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);
JavaScript (ES Modules)
import { hash } from '@konfirm/checksum';
const output = hash('the quick brown fox jumps over the lazy dog');
console.log(output);
JavaScript (CommonJS)
const { hash } = require('@konfirm/checksum');
const output = hash('the quick brown fox jumps over the lazy dog');
console.log(output);
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 algorith (default 'sha256'
) and return the created hash as a string using the optional digest method (default 'hex'
). Albeit 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);
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);
JavaScript (CommonJS)
const { hmac } = require('@konfirm/checksum');
const output = hmac('my secret', 'the quick brown fox jumps over the lazy dog');
console.log(output);
Migrating from version 1.0 to 2.0
Removed features
Several previously public available members have be removed from the package
verifyAlgorithmAndDigest
method, this has been removed as its internal mechanics have been improved to safe-guard the values givenALGORITHMS
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 entiry library (const Checksum = require('@konfirm/checksum');
), that syntax still woks, though we (now) recommend to pick 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.