argon2-json-hasher
Converts any JSON object into a hash, and compares their results. The library use argon2 to generate hashes.
Installation
Simply add this project as a local dependency:
$ npm i --save argon2-json-parser
Object to Hash
You can generate hashes from any json plain object. If you want to use a random salt generated with a fixed length, simply give to the JsonHasher
constructor a number with the desired length (in bytes) of the random salt, and the new instance will has been generated with a new random salt:
import { JsonHasher } from 'argon2-json-hasher';
async function program(obj: any): Promise<void> {
const hasher = new JsonHasher(32);
const hashed = await hasher.hash(obj);
console.log('hash ->', hashed.hash.toString('base64'));
console.log('salt ->', hashed.salt.toString('base64'));
}
In cases when you need to use an existing salt (as a Buffer
), you can give that as a constructor parameter:
import { JsonHasher } from 'argon2-json-hasher';
async function program(obj: any, salt: Buffer): Promise<void> {
const hasher = new JsonHasher(salt);
const hashed = await hasher.hash(obj);
console.log('hash ->', hashed.hash.toString('base64'));
console.log('salt ->', hashed.salt.toString('base64'));
}
Compare both objects
If you need to check if 2 complex objects are the same, you can use this library this way:
import { JsonHasher } from 'argon2-json-hasher';
async function program(objA: any, objB: any): Promise<void> {
const hasher = new JsonHasher(32);
const areEqual = await hasher.areEqual(objA, objB);
if (areEqual) {
console.log('Are the same');
} else {
console.log('Are different');
}
}
Save your configuration
If you want to save the configuration of your current instance, now you have the HasherFile
class for that purpose:
import { JsonHasher, HasherFile } from 'argon2-json-hasher';
const hasherFile = new HasherFile('./secret.argon2');
async function program(): Promise<void> {
const originalHasher = new JsonHasher(32, {
hashLength: 256,
memoryCost: 3660
});
await hasherFile.save(originalHasher);
const theSameHasher = await hasherFile.load();
}