decrypter.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. (function() {
  2. var nodeEnv = typeof require !== 'undefined' && typeof process !== 'undefined';
  3. var __module = nodeEnv ? module : {exports:{}};
  4. var __filename = 'preview-scripts/__node_modules/browserify-aes/decrypter.js';
  5. var __require = nodeEnv ? function (request) {
  6. return cc.require(request);
  7. } : function (request) {
  8. return __quick_compile_project__.require(request, __filename);
  9. };
  10. function __define (exports, require, module) {
  11. if (!nodeEnv) {__quick_compile_project__.registerModule(__filename, module);}var AuthCipher = require('./authCipher')
  12. var Buffer = require('safe-buffer').Buffer
  13. var MODES = require('./modes')
  14. var StreamCipher = require('./streamCipher')
  15. var Transform = require('cipher-base')
  16. var aes = require('./aes')
  17. var ebtk = require('evp_bytestokey')
  18. var inherits = require('inherits')
  19. function Decipher (mode, key, iv) {
  20. Transform.call(this)
  21. this._cache = new Splitter()
  22. this._last = void 0
  23. this._cipher = new aes.AES(key)
  24. this._prev = Buffer.from(iv)
  25. this._mode = mode
  26. this._autopadding = true
  27. }
  28. inherits(Decipher, Transform)
  29. Decipher.prototype._update = function (data) {
  30. this._cache.add(data)
  31. var chunk
  32. var thing
  33. var out = []
  34. while ((chunk = this._cache.get(this._autopadding))) {
  35. thing = this._mode.decrypt(this, chunk)
  36. out.push(thing)
  37. }
  38. return Buffer.concat(out)
  39. }
  40. Decipher.prototype._final = function () {
  41. var chunk = this._cache.flush()
  42. if (this._autopadding) {
  43. return unpad(this._mode.decrypt(this, chunk))
  44. } else if (chunk) {
  45. throw new Error('data not multiple of block length')
  46. }
  47. }
  48. Decipher.prototype.setAutoPadding = function (setTo) {
  49. this._autopadding = !!setTo
  50. return this
  51. }
  52. function Splitter () {
  53. this.cache = Buffer.allocUnsafe(0)
  54. }
  55. Splitter.prototype.add = function (data) {
  56. this.cache = Buffer.concat([this.cache, data])
  57. }
  58. Splitter.prototype.get = function (autoPadding) {
  59. var out
  60. if (autoPadding) {
  61. if (this.cache.length > 16) {
  62. out = this.cache.slice(0, 16)
  63. this.cache = this.cache.slice(16)
  64. return out
  65. }
  66. } else {
  67. if (this.cache.length >= 16) {
  68. out = this.cache.slice(0, 16)
  69. this.cache = this.cache.slice(16)
  70. return out
  71. }
  72. }
  73. return null
  74. }
  75. Splitter.prototype.flush = function () {
  76. if (this.cache.length) return this.cache
  77. }
  78. function unpad (last) {
  79. var padded = last[15]
  80. if (padded < 1 || padded > 16) {
  81. throw new Error('unable to decrypt data')
  82. }
  83. var i = -1
  84. while (++i < padded) {
  85. if (last[(i + (16 - padded))] !== padded) {
  86. throw new Error('unable to decrypt data')
  87. }
  88. }
  89. if (padded === 16) return
  90. return last.slice(0, 16 - padded)
  91. }
  92. function createDecipheriv (suite, password, iv) {
  93. var config = MODES[suite.toLowerCase()]
  94. if (!config) throw new TypeError('invalid suite type')
  95. if (typeof iv === 'string') iv = Buffer.from(iv)
  96. if (config.mode !== 'GCM' && iv.length !== config.iv) throw new TypeError('invalid iv length ' + iv.length)
  97. if (typeof password === 'string') password = Buffer.from(password)
  98. if (password.length !== config.key / 8) throw new TypeError('invalid key length ' + password.length)
  99. if (config.type === 'stream') {
  100. return new StreamCipher(config.module, password, iv, true)
  101. } else if (config.type === 'auth') {
  102. return new AuthCipher(config.module, password, iv, true)
  103. }
  104. return new Decipher(config.module, password, iv)
  105. }
  106. function createDecipher (suite, password) {
  107. var config = MODES[suite.toLowerCase()]
  108. if (!config) throw new TypeError('invalid suite type')
  109. var keys = ebtk(password, false, config.key, config.iv)
  110. return createDecipheriv(suite, keys.key, keys.iv)
  111. }
  112. exports.createDecipher = createDecipher
  113. exports.createDecipheriv = createDecipheriv
  114. }
  115. if (nodeEnv) {
  116. __define(__module.exports, __require, __module);
  117. }
  118. else {
  119. __quick_compile_project__.registerModuleFunc(__filename, function () {
  120. __define(__module.exports, __require, __module);
  121. });
  122. }
  123. })();