const crypto = require('crypto');
/**
* Hash helper
*
* @static
* @requires crypto Native NodeJS module
*/
class HashHelper {
/**
* Hashes a provided value using the SHA-256 hash function.
*
* @static
* @method
* @public
* @param {string} value the value to hash
* @returns {string} the hashed value
* @example
it('should hash "test" correctly', async function () {
const value = 'test'
const expectedHash = '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'
chai.expect(hashHelper.sha256Hash(value)).to.equal(expectedHash)
})
*/
static sha256Hash = (value) => {
if (typeof value !== 'string' || value === '') throw (new Error('hashHelper.sha256Hash requires a non-empty string value to hash.'));
return crypto
.createHash('sha256')
.update(value)
.digest('hex');
};
/**
* Hashes a provided value using the SHA-512 hash function.
*
* @static
* @method
* @public
* @param {string} value the value to hash
* @returns {string} the hashed value
* @example
it('should hash "Test" correctly', async function () {
const value = 'Test'
const expectedHash = 'c6ee9e33cf5c6715a1d148fd73f7318884b41adcb916021e2bc0e800a5c5dd97f5142178f6ae88c8fdd98e1afb0ce4c8d2c54b5f37b30b7da1997bb33b0b8a31'
chai.expect(hashHelper.sha512Hash(value)).to.equal(expectedHash)
})
*/
static sha512Hash = (value) => {
if (typeof value !== 'string' || value === '') throw (new Error('hashHelper.sha512Hash requires a non-empty string value to hash.'));
return crypto
.createHash('sha512')
.update(value)
.digest('hex');
};
/**
* Hashes a provided value using the MD5 hash function.
*
* @static
* @method
* @public
* @param {string} value the value to hash
* @returns {string} the hashed value
* @example
it('should hash "test" correctly', async function () {
const value = 'test'
const expectedHash = '098f6bcd4621d373cade4e832627b4f6'
chai.expect(hashHelper.md5Hash(value)).to.equal(expectedHash)
})
*/
static md5Hash = (value) => {
if (typeof value !== 'string' || value === '') throw (new Error('hashHelper.md5Hash requires a non-empty string value to hash.'));
return crypto
.createHash('md5')
.update(value)
.digest('hex');
};
/**
* Hashes a provided value using the SHA-1 hash function.
*
* @static
* @method
* @public
* @param {string} value the value to hash
* @returns {string} the hashed value
* @example
it('should hash "test" correctly', async function () {
const value = 'test'
const expectedHash = 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'
chai.expect(hashHelper.sha1Hash(value)).to.equal(expectedHash)
})
*/
static sha1Hash = (value) => {
if (typeof value !== 'string' || value === '') throw (new Error('hashHelper.sha1Hash requires a non-empty string value to hash.'));
return crypto
.createHash('sha1')
.update(value)
.digest('hex');
};
}
module.exports = HashHelper;