All files / modules/digest index.js

100% Statements 13/13
100% Branches 0/0
100% Functions 9/9
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111      10x               1x                 1x                 10x 1x                 10x 1x                     7x                     1x                     7x                     4x                     11x         10x                    
/**
 * @module digest
 */
const crypto = require("crypto");
 
/**
 * Generate a secure random salt of schema defined length
 * (256-bits) and return value in hex.
 * @return {hex} 32-byte salt in hex
 */
function generateSecureSaltInHex() {
    return generateSecureRandomBytesInHex(32);
}
 
/**
 * Generate a secure random salt of schema defined length
 * (256-bits) and return value in Base64.
 * @return {base64} 32-byte salt in base64
 */
function generateSecureSaltInBase64() {
    return generateSecureRandomBytesInBase64(32);
}
 
/**
 * Generate secure random bytes of given size
 * and return value in hex.
 * @param {number} size in bytes
 * @return {string} random bytes in hex
 */
const generateSecureRandomBytesInHex = (size) => {
    return crypto.randomBytes(size).toString("hex");
};
 
/**
 * Generate secure random bytes of given size
 * and return value in Base64.
 * @param {number} size in bytes
 * @return {string} random bytes in base64
 */
const generateSecureRandomBytesInBase64 = (size) => {
    return crypto.randomBytes(size).toString("base64");
};
 
/**
 * Hashes the given password and salt concatenated with the
 * SHA-256 crypto standard hash function and returns the value in hex.
 * @param {string} password
 * @param {hex} secureSalt
 * @return {hex} 32-byte hash in hex
 */
function hashPassWithSaltInHex(password, secureSalt) {
    return hashVarargInHex(password, secureSalt);
}
 
/**
 * Hashes the given password and salt concatenated with the
 * SHA-256 crypto standard hash function and returns the value in Base64.
 * @param {string} password
 * @param {base64} secureSalt
 * @return {base64} 32-byte hash in Base64
 */
function hashPassWithSaltInBase64(password, secureSalt) {
    return hashVarargInBase64(password, secureSalt);
}
 
/**
 * Hash a variable number of arguments concatenated
 * with the SHA-256 crypto standard hash function and
 * return result in hex.
 * @param  {...any} args
 * @return {digest} Sha-256 hash in hex
 */
function hashVarargInHex(...args) {
    return hashInput(args.join("")).digest("hex");
}
 
/**
 * Hash a variable number of arguments concatenated
 * with the SHA-256 crypto standard hash function and
 * return result in Base64.
 * @param  {...any} args
 * @return {digest} Sha-256 hash in Base64
 */
function hashVarargInBase64(...args) {
    return hashInput(args.join("")).digest("base64");
}
 
/**
 * Hash a variable number of arguments concatenated
 * with the SHA-256 crypto standard hash function and
 * return result.
 * @param  {any} input
 * @return {digest} Sha-256 hash
 */
function hashInput(input) {
    return crypto
        .createHash("sha256")
        .update(input);
}
 
module.exports = {
    generateSecureRandomBytesInHex,
    generateSecureRandomBytesInBase64,
    generateSecureSaltInHex,
    generateSecureSaltInBase64,
    hashPassWithSaltInHex,
    hashPassWithSaltInBase64,
    hashVarargInHex,
    hashVarargInBase64,
};