test_crypto_pwhash.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * Created by bmf on 10/31/13.
  3. */
  4. "use strict";
  5. var assert = require('assert');
  6. var sodium = require('../build/Release/sodium');
  7. var toBuffer = require('../lib/toBuffer');
  8. describe('PWHash scryptsalsa208sha256', function() {
  9. it('should verify the generated hash with same password', function(done) {
  10. var password = Buffer.from('this is a test password','utf8');
  11. var out = sodium.crypto_pwhash_scryptsalsa208sha256_str(
  12. password,
  13. sodium.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE,
  14. sodium.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE);
  15. assert(sodium.crypto_pwhash_scryptsalsa208sha256_str_verify(out, password));
  16. done();
  17. });
  18. it('should not verify the generated hash with different passwords', function(done) {
  19. var password = Buffer.from('this is a test password','utf8');
  20. var badPassword = Buffer.from('this is a bad password','utf8');
  21. var out = sodium.crypto_pwhash_scryptsalsa208sha256_str(
  22. password,
  23. sodium.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE,
  24. sodium.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE);
  25. assert(sodium.crypto_pwhash_scryptsalsa208sha256_str_verify(out, badPassword)==false);
  26. done();
  27. });
  28. it('should generate pwhash using low level scrypt API', function(done) {
  29. var password = Buffer.from("pleaseletmein",'utf8');
  30. var salt = Buffer.from('SodiumChloride','utf8');
  31. var result = toBuffer("7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2d5432955613f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887");
  32. var N = 16384;
  33. var r = 8;
  34. var p = 1;
  35. var output = Buffer.allocUnsafe(64);
  36. sodium.crypto_pwhash_scryptsalsa208sha256_ll(password, salt, N, r, p, output);
  37. assert(sodium.compare(result,output)==0);
  38. done();
  39. });
  40. });
  41. describe('PWHash', function() {
  42. it('should verify the generated hash with same password', function(done) {
  43. var password = Buffer.from('this is a test password','utf8');
  44. var out = sodium.crypto_pwhash_str(
  45. password,
  46. sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE,
  47. sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE);
  48. var valid = sodium.crypto_pwhash_str_verify(out, password);
  49. assert(valid);
  50. done();
  51. });
  52. it('should not verify the generated hash with different passwords', function(done) {
  53. var password = Buffer.from('this is a test password','utf8');
  54. var badPassword = Buffer.from('this is a bad password','utf8');
  55. var out = sodium.crypto_pwhash_str(
  56. password,
  57. sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE,
  58. sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE);
  59. assert(sodium.crypto_pwhash_str_verify(out, badPassword)==false);
  60. done();
  61. });
  62. });
  63. describe('PWHash argon2i', function() {
  64. it('should verify the generated hash with same password', function(done) {
  65. var password = Buffer.from('this is a test password','utf8');
  66. var hash = sodium.crypto_pwhash_argon2i_str(
  67. password,
  68. sodium.crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE,
  69. sodium.crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVE);
  70. var result = sodium.crypto_pwhash_argon2i_str_verify(hash, password);
  71. assert(result);
  72. done();
  73. });
  74. it('should not verify the generated hash with different passwords', function(done) {
  75. var password = Buffer.from('this is a test password','utf8');
  76. var badPassword = Buffer.from('this is a bad password','utf8');
  77. var out = sodium.crypto_pwhash_argon2i_str(
  78. password,
  79. sodium.crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE,
  80. sodium.crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVE);
  81. assert(sodium.crypto_pwhash_argon2i_str_verify(out, badPassword)==false);
  82. done();
  83. });
  84. });