bcrypt.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // callback error asynchronously
  35. return process.nextTick(function() {
  36. cb(new Error('rounds must be a number'));
  37. });
  38. }
  39. if (!cb) {
  40. return;
  41. }
  42. crypto.randomBytes(16, function(error, randomBytes) {
  43. if (error) {
  44. cb(error);
  45. return;
  46. }
  47. bindings.gen_salt(rounds, randomBytes, cb);
  48. });
  49. };
  50. /// hash data using a salt
  51. /// @param {String} data the data to encrypt
  52. /// @param {String} salt the salt to use when hashing
  53. /// @return {String} hash
  54. module.exports.hashSync = function(data, salt) {
  55. if (data == null || salt == null) {
  56. throw new Error('data and salt arguments required');
  57. }
  58. if (typeof data !== 'string' || (typeof salt !== 'string' && typeof salt !== 'number')) {
  59. throw new Error('data must be a string and salt must either be a salt string or a number of rounds');
  60. }
  61. if (typeof salt === 'number') {
  62. salt = module.exports.genSaltSync(salt);
  63. }
  64. return bindings.encrypt_sync(data, salt);
  65. };
  66. /// hash data using a salt
  67. /// @param {String} data the data to encrypt
  68. /// @param {String} salt the salt to use when hashing
  69. /// @param {Function} cb callback(err, hash)
  70. module.exports.hash = function(data, salt, cb) {
  71. if (typeof data === 'function') {
  72. return process.nextTick(function() {
  73. data(new Error('data must be a string and salt must either be a salt string or a number of rounds'));
  74. });
  75. }
  76. if (typeof salt === 'function') {
  77. return process.nextTick(function() {
  78. salt(new Error('data must be a string and salt must either be a salt string or a number of rounds'));
  79. });
  80. }
  81. if (data == null || salt == null) {
  82. return process.nextTick(function() {
  83. cb(new Error('data and salt arguments required'));
  84. });
  85. }
  86. if (typeof data !== 'string' || (typeof salt !== 'string' && typeof salt !== 'number')) {
  87. return process.nextTick(function() {
  88. cb(new Error('data must be a string and salt must either be a salt string or a number of rounds'));
  89. });
  90. }
  91. if (!cb || typeof cb !== 'function') {
  92. return;
  93. }
  94. if (typeof salt === 'number') {
  95. return module.exports.genSalt(salt, function(err, salt) {
  96. return bindings.encrypt(data, salt, cb);
  97. });
  98. }
  99. return bindings.encrypt(data, salt, cb);
  100. };
  101. /// compare raw data to hash
  102. /// @param {String} data the data to hash and compare
  103. /// @param {String} hash expected hash
  104. /// @return {bool} true if hashed data matches hash
  105. module.exports.compareSync = function(data, hash) {
  106. if (data == null || hash == null) {
  107. throw new Error('data and hash arguments required');
  108. }
  109. if (typeof data !== 'string' || typeof hash !== 'string') {
  110. throw new Error('data and hash must be strings');
  111. }
  112. return bindings.compare_sync(data, hash);
  113. };
  114. /// compare raw data to hash
  115. /// @param {String} data the data to hash and compare
  116. /// @param {String} hash expected hash
  117. /// @param {Function} cb callback(err, matched) - matched is true if hashed data matches hash
  118. module.exports.compare = function(data, hash, cb) {
  119. if (data == null || hash == null) {
  120. return process.nextTick(function() {
  121. cb(new Error('data and hash arguments required'));
  122. });
  123. }
  124. if (typeof data !== 'string' || typeof hash !== 'string') {
  125. return process.nextTick(function() {
  126. cb(new Error('data and hash must be strings'));
  127. });
  128. }
  129. if (!cb || typeof cb !== 'function') {
  130. return;
  131. }
  132. return bindings.compare(data, hash, cb);
  133. };
  134. /// @param {String} hash extract rounds from this hash
  135. /// @return {Number} the number of rounds used to encrypt a given hash
  136. module.exports.getRounds = function(hash) {
  137. if (hash == null) {
  138. throw new Error('hash argument required');
  139. }
  140. if (typeof hash !== 'string') {
  141. throw new Error('hash must be a string');
  142. }
  143. return bindings.get_rounds(hash);
  144. };