hash.spec.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* Import modules. */
  2. const Nito = require('../..')
  3. describe('Crypto:hash', () => {
  4. test('it should perform an MD5 hash on a string', async () => {
  5. /* Calculate hash. */
  6. const hash = Nito.Crypto.hash('Hello, world', 'md5')
  7. /* Set expected hex value. */
  8. const expected = Buffer.from('bc6e6f16b8a077ef5fbc8d59d0b931b9', 'hex')
  9. /* Evaluate test. */
  10. expect(hash).toEqual(expected)
  11. })
  12. test('it should perform a SHA256 hash on a string', async () => {
  13. /* Calculate hash. */
  14. const hash = Nito.Crypto.hash('Hello, world', 'sha256')
  15. /* Set expected hex value. */
  16. const expected = Buffer.from('4ae7c3b6ac0beff671efa8cf57386151c06e58ca53a78d83f36107316cec125f', 'hex')
  17. /* Evaluate test. */
  18. expect(hash).toEqual(expected)
  19. })
  20. test('it should perform a SHA512 (default) hash on a string', async () => {
  21. /* Calculate hash. */
  22. const hash = Nito.Crypto.hash('Hello, world')
  23. /* Set expected hex value. */
  24. const expected = Buffer.from('f986313ffca1a20c61fa2cff5cb597f1af10a650aecca497a746e8d11d1b6bf33e9e6a25eb7ba26af2fcfaa70472d8250b908419a188a16e17191fc26f423f52', 'hex')
  25. /* Evaluate test. */
  26. expect(hash).toEqual(expected)
  27. })
  28. test('it should perform a "double" SHA256 hash on a string', async () => {
  29. /* Calculate hash. */
  30. const hash = Nito.Crypto.hash('Hello, world', 'sha256sha256')
  31. /* Set expected hex value. */
  32. const expected = Buffer.from('d9ebb60edf18fd5bbcc8f01ddb130b4122f74ccac9a1374c85b310a9a0ddddca', 'hex')
  33. /* Evaluate test. */
  34. expect(hash).toEqual(expected)
  35. })
  36. test('it should perform a SHA256 and RIPEMD160 hash on a string', async () => {
  37. /* Calculate hash. */
  38. const hash = Nito.Crypto.hash('Hello, world', 'sha256ripemd160')
  39. /* Set expected hex value. */
  40. const expected = Buffer.from('02a48dfcdae3f658e5118409200b2e072029b566', 'hex')
  41. /* Evaluate test. */
  42. expect(hash).toEqual(expected)
  43. })
  44. })