argon2-hasher
This is a wrapper for Argon2 module. With this package you can:
- Hash
string
, Buffer
, or any JSON object. - Validate hashes with an specific configuration.
- Compare the "deep" equality of 2 objects.
- Load and save your current cofiguration in a file.
- Now with CommonJS and ESM Modules support.
Installation
Simply add this project as a local dependency to your project:
$ npm i --save argon2-hasher
Hash anything
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 { Hasher } from 'argon2-hasher';
async function program(obj: any): Promise<void> {
const hasher = new Hasher(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 { Hasher } from 'argon2-hasher';
async function program(obj: any, salt: Buffer): Promise<void> {
const hasher = new Hasher(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 { Hasher } from 'argon2-hasher';
async function program(objA: any, objB: any): Promise<void> {
const hasher = new Hasher(32);
const areEqual = await hasher.compare(objA, objB);
if (areEqual) {
console.log('Are the same');
} else {
console.log('Are different');
}
}
Save and load your configuration
If you want to save the configuration of your current instance, now you have the HasherFile
class for that purpose:
Save configuration
With an instance of HasherFile
you can save the Hasher current configuration simply using a single method:
import { Hasher, HasherFile } from 'argon2-hasher';
async function program(): Promise<void> {
const originalHasher = new Hasher(32, {
hashLength: 256,
memoryCost: 3660
});
const hasherFile = new HasherFile('./secret.argon2');
await hasherFile.save(originalHasher);
}
Load configuration
With HasherFile
you have 2 ways to load the configuration. One way is using an instance of HasherFile
:
import { Hasher, HasherFile } from 'argon2-hasher';
async function program(): Promise<void> {
const hasherFile = new HasherFile('./secret.argon2');
const hasher = await hasherFile.load();
const hashed = hasher.hash('jajAJjajaj');
}
...or using the static method of the same class:
import { Hasher, HasherFile } from 'argon2-hasher';
async function program(): Promise<void> {
const hasher = await HasherFile.load('./secret.argon2');
const hashed = hasher.hash('jajAJjajaj');
}