All files / src/lib hash.js

100% Statements 11/11
100% Branches 4/4
100% Functions 5/5
100% Lines 11/11
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                            287x         287x 287x     4x 226x 61x             21x 1x     20x 19x     1x        
/*!
 * SuperGenPass library
 * https://github.com/chriszarate/supergenpass-lib
 * https://chriszarate.github.com/supergenpass/
 * License: GPLv2
 */
 
import encBase64 from 'crypto-js/enc-base64';
import md5 from 'crypto-js/md5';
import sha512 from 'crypto-js/sha512';
 
// Replace non-alphanumeric and padding characters in the Base-64 alphabet to
// comply with most password policies.
function customBase64(str) {
  return str.replace(/\+/g, '9').replace(/\//g, '8').replace(/=/g, 'A');
}
 
// Compute hexadecimal hash and convert it to Base-64.
function customBase64Hash(str, hashFunction) {
  const result = hashFunction(str).toString(encBase64);
  return customBase64(result);
}
 
const hashFunctions = {
  md5: str => customBase64Hash(str, md5),
  sha512: str => customBase64Hash(str, sha512),
};
 
// Return a hash function for SGP to use.
function hash(method) {
  // Is user supplies a function, use it and assume they will take of any
  // encoding (Base-64 or otherwise).
  if (typeof method === 'function') {
    return method;
  }
 
  if (hashFunctions.hasOwnProperty(method)) {
    return hashFunctions[method];
  }
 
  throw new Error(`Could not resolve hash function, received ${typeof method}.`);
}
 
export default hash;