Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

async.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. var checkParameters = require('./precondition')
  2. var defaultEncoding = require('./default-encoding')
  3. var sync = require('./sync')
  4. var Buffer = require('safe-buffer').Buffer
  5. var ZERO_BUF
  6. var subtle = global.crypto && global.crypto.subtle
  7. var toBrowser = {
  8. 'sha': 'SHA-1',
  9. 'sha-1': 'SHA-1',
  10. 'sha1': 'SHA-1',
  11. 'sha256': 'SHA-256',
  12. 'sha-256': 'SHA-256',
  13. 'sha384': 'SHA-384',
  14. 'sha-384': 'SHA-384',
  15. 'sha-512': 'SHA-512',
  16. 'sha512': 'SHA-512'
  17. }
  18. var checks = []
  19. function checkNative (algo) {
  20. if (global.process && !global.process.browser) {
  21. return Promise.resolve(false)
  22. }
  23. if (!subtle || !subtle.importKey || !subtle.deriveBits) {
  24. return Promise.resolve(false)
  25. }
  26. if (checks[algo] !== undefined) {
  27. return checks[algo]
  28. }
  29. ZERO_BUF = ZERO_BUF || Buffer.alloc(8)
  30. var prom = browserPbkdf2(ZERO_BUF, ZERO_BUF, 10, 128, algo)
  31. .then(function () {
  32. return true
  33. }).catch(function () {
  34. return false
  35. })
  36. checks[algo] = prom
  37. return prom
  38. }
  39. function browserPbkdf2 (password, salt, iterations, length, algo) {
  40. return subtle.importKey(
  41. 'raw', password, {name: 'PBKDF2'}, false, ['deriveBits']
  42. ).then(function (key) {
  43. return subtle.deriveBits({
  44. name: 'PBKDF2',
  45. salt: salt,
  46. iterations: iterations,
  47. hash: {
  48. name: algo
  49. }
  50. }, key, length << 3)
  51. }).then(function (res) {
  52. return Buffer.from(res)
  53. })
  54. }
  55. function resolvePromise (promise, callback) {
  56. promise.then(function (out) {
  57. process.nextTick(function () {
  58. callback(null, out)
  59. })
  60. }, function (e) {
  61. process.nextTick(function () {
  62. callback(e)
  63. })
  64. })
  65. }
  66. module.exports = function (password, salt, iterations, keylen, digest, callback) {
  67. if (typeof digest === 'function') {
  68. callback = digest
  69. digest = undefined
  70. }
  71. digest = digest || 'sha1'
  72. var algo = toBrowser[digest.toLowerCase()]
  73. if (!algo || typeof global.Promise !== 'function') {
  74. return process.nextTick(function () {
  75. var out
  76. try {
  77. out = sync(password, salt, iterations, keylen, digest)
  78. } catch (e) {
  79. return callback(e)
  80. }
  81. callback(null, out)
  82. })
  83. }
  84. checkParameters(password, salt, iterations, keylen)
  85. if (typeof callback !== 'function') throw new Error('No callback provided to pbkdf2')
  86. if (!Buffer.isBuffer(password)) password = Buffer.from(password, defaultEncoding)
  87. if (!Buffer.isBuffer(salt)) salt = Buffer.from(salt, defaultEncoding)
  88. resolvePromise(checkNative(algo).then(function (resp) {
  89. if (resp) return browserPbkdf2(password, salt, iterations, keylen, algo)
  90. return sync(password, salt, iterations, keylen, digest)
  91. }), callback)
  92. }