bcrypt.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. 'use strict';
  2. var bindings = require('bindings')('bcrypt_lib');
  3. var crypto = require('crypto');
  4. /// generate a salt (sync)
  5. /// @param {Number} [rounds] number of rounds (default 10)
  6. /// @return {String} salt
  7. module.exports.genSaltSync = function(rounds) {
  8. // default 10 rounds
  9. if (!rounds) {
  10. rounds = 10;
  11. } else if (typeof rounds !== 'number') {
  12. throw new Error('rounds must be a number');
  13. }
  14. return bindings.gen_salt_sync(rounds, crypto.randomBytes(16));
  15. };
  16. /// generate a salt
  17. /// @param {Number} [rounds] number of rounds (default 10)
  18. /// @param {Function} cb callback(err, salt)
  19. module.exports.genSalt = function(rounds, ignore, cb) {
  20. // if callback is first argument, then use defaults for others
  21. if (typeof arguments[0] === 'function') {
  22. // have to set callback first otherwise arguments are overriden
  23. cb = arguments[0];
  24. rounds = 10;
  25. // callback is second argument
  26. } else if (typeof arguments[1] === 'function') {
  27. // have to set callback first otherwise arguments are overriden
  28. cb = arguments[1];
  29. }
  30. // default 10 rounds
  31. if (!rounds) {
  32. rounds = 10;
  33. } else if (typeof rounds !== 'number') {
  34. return cb(new Error('rounds must be a number'));
  35. }
  36. if (!cb) {
  37. return;
  38. }
  39. return bindings.gen_salt(rounds, crypto.randomBytes(16), cb);
  40. };
  41. /// hash data using a salt
  42. /// @param {String} data the data to encrypt
  43. /// @param {String} salt the salt to use when hashing
  44. /// @return {String} hash
  45. module.exports.hashSync = function(data, salt) {
  46. if (data == null || salt == null) {
  47. throw new Error('data and salt arguments required');
  48. }
  49. if (typeof data !== 'string' || (typeof salt !== 'string' && typeof salt !== 'number')) {
  50. throw new Error('data must be a string and salt must either be a salt string or a number of rounds');
  51. }
  52. if (typeof salt === 'number') {
  53. salt = module.exports.genSaltSync(salt);
  54. }
  55. return bindings.encrypt_sync(data, salt);
  56. };
  57. /// hash data using a salt
  58. /// @param {String} data the data to encrypt
  59. /// @param {String} salt the salt to use when hashing
  60. /// @param {Function} cb callback(err, hash)
  61. module.exports.hash = function(data, salt, cb) {
  62. if (typeof data === 'function') {
  63. return data(new Error('data must be a string and salt must either be a salt string or a number of rounds'));
  64. }
  65. if (typeof salt === 'function') {
  66. return salt(new Error('data must be a string and salt must either be a salt string or a number of rounds'));
  67. }
  68. if (data == null || salt == null) {
  69. return cb(new Error('data and salt arguments required'));
  70. }
  71. if (typeof data !== 'string' || (typeof salt !== 'string' && typeof salt !== 'number')) {
  72. return cb(new Error('data must be a string and salt must either be a salt string or a number of rounds'));
  73. }
  74. if (!cb || typeof cb !== 'function') {
  75. return;
  76. }
  77. if (typeof salt === 'number') {
  78. return module.exports.genSalt(salt, function(err, salt) {
  79. return bindings.encrypt(data, salt, cb);
  80. });
  81. }
  82. return bindings.encrypt(data, salt, cb);
  83. };
  84. /// compare raw data to hash
  85. /// @param {String} data the data to hash and compare
  86. /// @param {String} hash expected hash
  87. /// @return {bool} true if hashed data matches hash
  88. module.exports.compareSync = function(data, hash) {
  89. if (data == null || hash == null) {
  90. throw new Error('data and hash arguments required');
  91. }
  92. if (typeof data !== 'string' || typeof hash !== 'string') {
  93. throw new Error('data and hash must be strings');
  94. }
  95. return bindings.compare_sync(data, hash);
  96. };
  97. /// compare raw data to hash
  98. /// @param {String} data the data to hash and compare
  99. /// @param {String} hash expected hash
  100. /// @param {Function} cb callback(err, matched) - matched is true if hashed data matches hash
  101. module.exports.compare = function(data, hash, cb) {
  102. if (data == null || hash == null) {
  103. return cb(new Error('data and hash arguments required'));
  104. }
  105. if (typeof data !== 'string' || typeof hash !== 'string') {
  106. return cb(new Error('data and hash must be strings'));
  107. }
  108. if (!cb || typeof cb !== 'function') {
  109. return;
  110. }
  111. return bindings.compare(data, hash, cb);
  112. };
  113. /// @param {String} hash extract rounds from this hash
  114. /// @return {Number} the number of rounds used to encrypt a given hash
  115. module.exports.getRounds = function(hash) {
  116. if (hash == null) {
  117. throw new Error('hash argument required');
  118. }
  119. if (typeof hash !== 'string') {
  120. throw new Error('hash must be a string');
  121. }
  122. return bindings.get_rounds(hash);
  123. };