Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

sync-browser.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. var md5 = require('create-hash/md5')
  2. var RIPEMD160 = require('ripemd160')
  3. var sha = require('sha.js')
  4. var checkParameters = require('./precondition')
  5. var defaultEncoding = require('./default-encoding')
  6. var Buffer = require('safe-buffer').Buffer
  7. var ZEROS = Buffer.alloc(128)
  8. var sizes = {
  9. md5: 16,
  10. sha1: 20,
  11. sha224: 28,
  12. sha256: 32,
  13. sha384: 48,
  14. sha512: 64,
  15. rmd160: 20,
  16. ripemd160: 20
  17. }
  18. function Hmac (alg, key, saltLen) {
  19. var hash = getDigest(alg)
  20. var blocksize = (alg === 'sha512' || alg === 'sha384') ? 128 : 64
  21. if (key.length > blocksize) {
  22. key = hash(key)
  23. } else if (key.length < blocksize) {
  24. key = Buffer.concat([key, ZEROS], blocksize)
  25. }
  26. var ipad = Buffer.allocUnsafe(blocksize + sizes[alg])
  27. var opad = Buffer.allocUnsafe(blocksize + sizes[alg])
  28. for (var i = 0; i < blocksize; i++) {
  29. ipad[i] = key[i] ^ 0x36
  30. opad[i] = key[i] ^ 0x5C
  31. }
  32. var ipad1 = Buffer.allocUnsafe(blocksize + saltLen + 4)
  33. ipad.copy(ipad1, 0, 0, blocksize)
  34. this.ipad1 = ipad1
  35. this.ipad2 = ipad
  36. this.opad = opad
  37. this.alg = alg
  38. this.blocksize = blocksize
  39. this.hash = hash
  40. this.size = sizes[alg]
  41. }
  42. Hmac.prototype.run = function (data, ipad) {
  43. data.copy(ipad, this.blocksize)
  44. var h = this.hash(ipad)
  45. h.copy(this.opad, this.blocksize)
  46. return this.hash(this.opad)
  47. }
  48. function getDigest (alg) {
  49. function shaFunc (data) {
  50. return sha(alg).update(data).digest()
  51. }
  52. function rmd160Func (data) {
  53. return new RIPEMD160().update(data).digest()
  54. }
  55. if (alg === 'rmd160' || alg === 'ripemd160') return rmd160Func
  56. if (alg === 'md5') return md5
  57. return shaFunc
  58. }
  59. function pbkdf2 (password, salt, iterations, keylen, digest) {
  60. checkParameters(password, salt, iterations, keylen)
  61. if (!Buffer.isBuffer(password)) password = Buffer.from(password, defaultEncoding)
  62. if (!Buffer.isBuffer(salt)) salt = Buffer.from(salt, defaultEncoding)
  63. digest = digest || 'sha1'
  64. var hmac = new Hmac(digest, password, salt.length)
  65. var DK = Buffer.allocUnsafe(keylen)
  66. var block1 = Buffer.allocUnsafe(salt.length + 4)
  67. salt.copy(block1, 0, 0, salt.length)
  68. var destPos = 0
  69. var hLen = sizes[digest]
  70. var l = Math.ceil(keylen / hLen)
  71. for (var i = 1; i <= l; i++) {
  72. block1.writeUInt32BE(i, salt.length)
  73. var T = hmac.run(block1, hmac.ipad1)
  74. var U = T
  75. for (var j = 1; j < iterations; j++) {
  76. U = hmac.run(U, hmac.ipad2)
  77. for (var k = 0; k < hLen; k++) T[k] ^= U[k]
  78. }
  79. T.copy(DK, destPos)
  80. destPos += hLen
  81. }
  82. return DK
  83. }
  84. module.exports = pbkdf2