index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. var native = require('crypto')
  2. var checkParameters = require('./lib/precondition')
  3. var defaultEncoding = require('./lib/default-encoding')
  4. var toBuffer = require('./lib/to-buffer')
  5. function nativePBKDF2 (password, salt, iterations, keylen, digest, callback) {
  6. checkParameters(iterations, keylen)
  7. password = toBuffer(password, defaultEncoding, 'Password')
  8. salt = toBuffer(salt, defaultEncoding, 'Salt')
  9. if (typeof digest === 'function') {
  10. callback = digest
  11. digest = 'sha1'
  12. }
  13. if (typeof callback !== 'function') throw new Error('No callback provided to pbkdf2')
  14. return native.pbkdf2(password, salt, iterations, keylen, digest, callback)
  15. }
  16. function nativePBKDF2Sync (password, salt, iterations, keylen, digest) {
  17. checkParameters(iterations, keylen)
  18. password = toBuffer(password, defaultEncoding, 'Password')
  19. salt = toBuffer(salt, defaultEncoding, 'Salt')
  20. digest = digest || 'sha1'
  21. return native.pbkdf2Sync(password, salt, iterations, keylen, digest)
  22. }
  23. /* istanbul ignore next */
  24. if (!native.pbkdf2Sync || native.pbkdf2Sync.toString().indexOf('keylen, digest') === -1) {
  25. exports.pbkdf2Sync = require('./lib/sync')
  26. exports.pbkdf2 = require('./lib/async')
  27. // native
  28. } else {
  29. exports.pbkdf2Sync = nativePBKDF2Sync
  30. exports.pbkdf2 = nativePBKDF2
  31. }