a zip code crypto-currency system good for red ONLY

sync-browser.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. var md5 = require('create-hash/md5')
  2. var rmd160 = 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. if (alg === 'rmd160' || alg === 'ripemd160') return rmd160
  53. if (alg === 'md5') return md5
  54. return shaFunc
  55. }
  56. function pbkdf2 (password, salt, iterations, keylen, digest) {
  57. checkParameters(password, salt, iterations, keylen)
  58. if (!Buffer.isBuffer(password)) password = Buffer.from(password, defaultEncoding)
  59. if (!Buffer.isBuffer(salt)) salt = Buffer.from(salt, defaultEncoding)
  60. digest = digest || 'sha1'
  61. var hmac = new Hmac(digest, password, salt.length)
  62. var DK = Buffer.allocUnsafe(keylen)
  63. var block1 = Buffer.allocUnsafe(salt.length + 4)
  64. salt.copy(block1, 0, 0, salt.length)
  65. var destPos = 0
  66. var hLen = sizes[digest]
  67. var l = Math.ceil(keylen / hLen)
  68. for (var i = 1; i <= l; i++) {
  69. block1.writeUInt32BE(i, salt.length)
  70. var T = hmac.run(block1, hmac.ipad1)
  71. var U = T
  72. for (var j = 1; j < iterations; j++) {
  73. U = hmac.run(U, hmac.ipad2)
  74. for (var k = 0; k < hLen; k++) T[k] ^= U[k]
  75. }
  76. T.copy(DK, destPos)
  77. destPos += hLen
  78. }
  79. return DK
  80. }
  81. module.exports = pbkdf2