PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/bcrypt/bcrypt.js

https://bitbucket.org/yeouchien/billion-users-project
JavaScript | 189 lines | 120 code | 35 blank | 34 comment | 79 complexity | 59ef38a10f77b6ce86a90c6a4caa4221 MD5 | raw file
Possible License(s): MIT, GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. var bindings = require('bindings')('bcrypt_lib');
  2. /// generate a salt (sync)
  3. /// @param {Number} [rounds] number of rounds (default 10)
  4. /// @param {Number} [seed_length] number of random bytes (default 20)
  5. /// @return {String} salt
  6. module.exports.gen_salt_sync = function(rounds, seed_length) {
  7. console.log("DEPRECATION WARNING: `gen_salt_sync` has been deprecated. Please use `genSaltSync` instead.");
  8. return module.exports.genSaltSync(rounds, seed_length);
  9. }
  10. module.exports.genSaltSync = function(rounds, seed_length) {
  11. // default 10 rounds
  12. if (!rounds) {
  13. rounds = 10;
  14. } else if (typeof rounds !== 'number') {
  15. throw new Error('rounds must be a number');
  16. }
  17. // default length 20
  18. if (!seed_length) {
  19. seed_length = 20;
  20. } else if (typeof seed_length !== 'number') {
  21. throw new Error('seed_length must be a number');
  22. }
  23. return bindings.gen_salt_sync(rounds, seed_length);
  24. };
  25. /// generate a salt
  26. /// @param {Number} [rounds] number of rounds (default 10)
  27. /// @param {Number} [seed_length] number of random bytes (default 20)
  28. /// @param {Function} cb callback(err, salt)
  29. module.exports.gen_salt = function(rounds, seed_length, cb) {
  30. console.log("DEPRECATION WARNING: `gen_salt` has been deprecated. Please use `genSalt` instead.");
  31. return module.exports.genSalt(rounds, seed_length, cb);
  32. }
  33. module.exports.genSalt = function(rounds, seed_length, cb) {
  34. // if callback is first argument, then use defaults for others
  35. if (typeof arguments[0] === 'function') {
  36. // have to set callback first otherwise arguments are overriden
  37. cb = arguments[0];
  38. rounds = 10;
  39. seed_length = 20;
  40. // callback is second argument
  41. } else if (typeof arguments[1] === 'function') {
  42. // have to set callback first otherwise arguments are overriden
  43. cb = arguments[1];
  44. seed_length = 20;
  45. }
  46. // default 10 rounds
  47. if (!rounds) {
  48. rounds = 10;
  49. } else if (typeof rounds !== 'number') {
  50. throw new Error('rounds must be a number');
  51. }
  52. // default length 20
  53. if (!seed_length) {
  54. seed_length = 20;
  55. } else if (typeof seed_length !== 'number') {
  56. throw new Error('seed_length must be a number');
  57. }
  58. if (!cb) {
  59. throw new Error('callback required for gen_salt');
  60. }
  61. return bindings.gen_salt(rounds, seed_length, cb);
  62. };
  63. /// hash data using a salt
  64. /// @param {String} data the data to encrypt
  65. /// @param {String} salt the salt to use when hashing
  66. /// @return {String} hash
  67. module.exports.encrypt_sync = function(data, salt) {
  68. console.log("DEPRECATION WARNING: `encrypt_sync` has been deprecated. Please use `hashSync` instead.");
  69. return module.exports.hashSync(data, salt);
  70. }
  71. module.exports.hashSync = function(data, salt) {
  72. if (data == null || data == undefined || salt == null || salt == undefined) {
  73. throw new Error('data and salt arguments required');
  74. } else if (typeof data !== 'string' && (typeof salt !== 'string' || typeof salt !== 'number')) {
  75. throw new Error('data must be a string and salt must either be a salt string or a number of rounds');
  76. }
  77. if (typeof salt === 'number') {
  78. salt = module.exports.genSaltSync(salt);
  79. }
  80. return bindings.encrypt_sync(data, salt);
  81. };
  82. /// hash data using a salt
  83. /// @param {String} data the data to encrypt
  84. /// @param {String} salt the salt to use when hashing
  85. /// @param {Function} cb callback(err, hash)
  86. module.exports.encrypt = function(data, salt, cb) {
  87. console.log("DEPRECATION WARNING: `encrypt` has been deprecated. Please use `hash` instead.");
  88. return module.exports.hash(data, salt, cb);
  89. }
  90. module.exports.hash = function(data, salt, cb) {
  91. if (data == null || data == undefined || salt == null || salt == undefined) {
  92. throw new Error('data and salt arguments required');
  93. } else if (typeof data !== 'string' && (typeof salt !== 'string' || typeof salt !== 'number')) {
  94. throw new Error('data must be a string and salt must either be a salt string or a number of rounds');
  95. }
  96. if (!cb) {
  97. throw new Error('callback required for async compare');
  98. } else if (typeof cb !== 'function') {
  99. throw new Error('callback must be a function');
  100. }
  101. if (typeof salt === 'number') {
  102. return module.exports.genSalt(salt, function(err, salt) {
  103. return bindings.encrypt(data, salt, cb);
  104. });
  105. }
  106. return bindings.encrypt(data, salt, cb);
  107. };
  108. /// compare raw data to hash
  109. /// @param {String} data the data to hash and compare
  110. /// @param {String} hash expected hash
  111. /// @return {bool} true if hashed data matches hash
  112. module.exports.compare_sync = function(data, hash) {
  113. console.log("DEPRECATION WARNING: `compare_sync` has been deprecated. Please use `compareSync` instead.");
  114. return module.exports.compareSync(data, hash);
  115. }
  116. module.exports.compareSync = function(data, hash) {
  117. if (data == null || data == undefined || hash == null || hash == undefined) {
  118. throw new Error('data and hash arguments required');
  119. } else if (typeof data !== 'string' || typeof hash !== 'string') {
  120. throw new Error('data and hash must be strings');
  121. }
  122. return bindings.compare_sync(data, hash);
  123. };
  124. /// compare raw data to hash
  125. /// @param {String} data the data to hash and compare
  126. /// @param {String} hash expected hash
  127. /// @param {Function} cb callback(err, matched) - matched is true if hashed data matches hash
  128. module.exports.compare = function(data, hash, cb) {
  129. if (data == null || data == undefined || hash == null || hash == undefined) {
  130. throw new Error('data and hash arguments required');
  131. } else if (typeof data !== 'string' || typeof hash !== 'string') {
  132. throw new Error('data and hash must be strings');
  133. }
  134. if (!cb) {
  135. throw new Error('callback required for async compare');
  136. } else if (typeof cb !== 'function') {
  137. throw new Error('callback must be a function');
  138. }
  139. return bindings.compare(data, hash, cb);
  140. };
  141. /// @param {String} hash extract rounds from this hash
  142. /// @return {Number} the number of rounds used to encrypt a given hash
  143. module.exports.get_rounds = function(hash) {
  144. console.log("DEPRECATION WARNING: `get_rounds` has been deprecated. Please use `getRounds` instead.");
  145. return module.exports.getRounds(hash);
  146. }
  147. module.exports.getRounds = function(hash) {
  148. if (hash == null || hash == undefined) {
  149. throw new Error('hash argument required');
  150. } else if (typeof hash !== 'string') {
  151. throw new Error('hash must be a string');
  152. }
  153. return bindings.get_rounds(hash);
  154. };