assertions/hashes.js

  1. /**
  2. * @fileOverview Hashes checks.
  3. * @module Hashes
  4. */
  5. const Interface = require('../interface');
  6. let Hashes = {};
  7. /**
  8. * Check if is a valid MD5 hash string
  9. *
  10. * **Interfaces**: `all`, `any`, `not`, `err`
  11. *
  12. * @function
  13. * @name md5
  14. * @param value {string} hash string
  15. * @returns {boolean}
  16. * @example
  17. * be.md5('00236a2ae558018ed13b5222ef1bd977') // true
  18. * be.not.md5('00236a2ae558018ed13b5222ef1bd977') // false
  19. */
  20. Hashes.md5 = (value) => {
  21. return /^[a-f0-9]{32}$/i.test(value);
  22. };
  23. /**
  24. * Check if is a valid SHA1 hash string
  25. *
  26. * **Interfaces**: `all`, `any`, `not`, `err`
  27. *
  28. * @function
  29. * @name sha1
  30. * @param value {string} hash string
  31. * @returns {boolean}
  32. * @example
  33. * be.sha1('aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d') // true
  34. * be.not.sha1('aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d') // false
  35. */
  36. Hashes.sha1 = (value) => {
  37. return /^[a-f0-9]{40}$/i.test(value);
  38. };
  39. /**
  40. * Check if is a valid SHA256 hash string
  41. *
  42. * **Interfaces**: `all`, `any`, `not`, `err`
  43. *
  44. * @function
  45. * @name sha256
  46. * @param value {string} hash string
  47. * @returns {boolean}
  48. * @since 1.9.0
  49. * @example
  50. * be.256('7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069') // true
  51. * be.not.256('7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069') // false
  52. */
  53. Hashes.sha256 = (value) => {
  54. return /^[a-f0-9]{64}$/i.test(value);
  55. };
  56. /**
  57. * Check if is a valid SHA512 hash string
  58. *
  59. * **Interfaces**: `all`, `any`, `not`, `err`
  60. *
  61. * @function
  62. * @name sha512
  63. * @param value {string} hash string
  64. * @returns {boolean}
  65. * @since 1.9.0
  66. * @example
  67. * be.sha512('aeae379a6e857728e44164267fdb7a0e27b205d757cc19899586c89dbb221930f1813d02ff93a661859bc17065eac4d6edf3c38a034e6283a84754d52917e5b0') // true
  68. * be.not.sha512('aeae379a6e857728e44164267fdb7a0e27b205d757cc19899586c89dbb221930f1813d02ff93a661859bc17065eac4d6edf3c38a034e6283a84754d52917e5b0') // false
  69. */
  70. Hashes.sha512 = (value) => {
  71. return /^[a-f0-9]{128}$/i.test(value);
  72. };
  73. Hashes = Interface.create(Hashes);
  74. module.exports = Hashes;