Source: digest/index.js

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