diff --git a/build/otr-web.js b/build/otr-web.js index 85e5e03..2a205c7 100644 --- a/build/otr-web.js +++ b/build/otr-web.js @@ -1,4 +1,4 @@ -!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.OTR=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 0 ? subject >>> 0 : 0 - else if (type === 'string') { + if (type === 'number') { + length = +subject + } else if (type === 'string') { length = Buffer.byteLength(subject, encoding) } else if (type === 'object' && subject !== null) { // assume object is array-like if (subject.type === 'Buffer' && isArray(subject.data)) subject = subject.data - length = +subject.length > 0 ? Math.floor(+subject.length) : 0 - } else + length = +subject.length + } else { throw new TypeError('must start with number, buffer, array or string') + } if (length > kMaxLength) throw new RangeError('Attempt to allocate Buffer larger than maximum ' + 'size: 0x' + kMaxLength.toString(16) + ' bytes') - var buf + if (length < 0) + length = 0 + else + length >>>= 0 // Coerce to uint32. + + var self = this if (Buffer.TYPED_ARRAY_SUPPORT) { // Preferred: Return an augmented `Uint8Array` instance for best performance - buf = Buffer._augment(new Uint8Array(length)) + /*eslint-disable consistent-this */ + self = Buffer._augment(new Uint8Array(length)) + /*eslint-enable consistent-this */ } else { // Fallback: Return THIS instance of Buffer (created by `new`) - buf = this - buf.length = length - buf._isBuffer = true + self.length = length + self._isBuffer = true } var i if (Buffer.TYPED_ARRAY_SUPPORT && typeof subject.byteLength === 'number') { // Speed optimization -- use set if we're copying from a typed array - buf._set(subject) + self._set(subject) } else if (isArrayish(subject)) { // Treat array-ish objects as a byte array if (Buffer.isBuffer(subject)) { for (i = 0; i < length; i++) - buf[i] = subject.readUInt8(i) + self[i] = subject.readUInt8(i) } else { for (i = 0; i < length; i++) - buf[i] = ((subject[i] % 256) + 256) % 256 + self[i] = ((subject[i] % 256) + 256) % 256 } } else if (type === 'string') { - buf.write(subject, 0, encoding) + self.write(subject, 0, encoding) } else if (type === 'number' && !Buffer.TYPED_ARRAY_SUPPORT && !noZero) { for (i = 0; i < length; i++) { - buf[i] = 0 + self[i] = 0 } } if (length > 0 && length <= Buffer.poolSize) - buf.parent = rootParent + self.parent = rootParent - return buf + return self } -function SlowBuffer(subject, encoding, noZero) { +function SlowBuffer (subject, encoding, noZero) { if (!(this instanceof SlowBuffer)) return new SlowBuffer(subject, encoding, noZero) @@ -6094,6 +6098,8 @@ Buffer.compare = function (a, b) { if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) throw new TypeError('Arguments must be Buffers') + if (a === b) return 0 + var x = a.length var y = b.length for (var i = 0, len = Math.min(x, y); i < len && a[i] === b[i]; i++) {} @@ -6234,6 +6240,7 @@ Buffer.prototype.toString = function (encoding, start, end) { Buffer.prototype.equals = function (b) { if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') + if (this === b) return true return Buffer.compare(this, b) === 0 } @@ -6250,6 +6257,7 @@ Buffer.prototype.inspect = function () { Buffer.prototype.compare = function (b) { if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') + if (this === b) return 0 return Buffer.compare(this, b) } @@ -6312,7 +6320,7 @@ function base64Write (buf, string, offset, length) { } function utf16leWrite (buf, string, offset, length) { - var charsWritten = blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length, 2) + var charsWritten = blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length) return charsWritten } @@ -6334,7 +6342,7 @@ Buffer.prototype.write = function (string, offset, length, encoding) { offset = Number(offset) || 0 if (length < 0 || offset < 0 || offset > this.length) - throw new RangeError('attempt to write outside buffer bounds'); + throw new RangeError('attempt to write outside buffer bounds') var remaining = this.length - offset if (!length) { @@ -6457,7 +6465,7 @@ Buffer.prototype.slice = function (start, end) { end = end === undefined ? len : ~~end if (start < 0) { - start += len; + start += len if (start < 0) start = 0 } else if (start > len) { @@ -6526,7 +6534,7 @@ Buffer.prototype.readUIntBE = function (offset, byteLength, noAssert) { var val = this[offset + --byteLength] var mul = 1 while (byteLength > 0 && (mul *= 0x100)) - val += this[offset + --byteLength] * mul; + val += this[offset + --byteLength] * mul return val } @@ -6934,7 +6942,7 @@ Buffer.prototype.writeDoubleBE = function (value, offset, noAssert) { // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) Buffer.prototype.copy = function (target, target_start, start, end) { - var source = this + var self = this // source if (!start) start = 0 if (!end && end !== 0) end = this.length @@ -6944,12 +6952,12 @@ Buffer.prototype.copy = function (target, target_start, start, end) { // Copy 0 bytes; we're done if (end === start) return 0 - if (target.length === 0 || source.length === 0) return 0 + if (target.length === 0 || self.length === 0) return 0 // Fatal error conditions if (target_start < 0) throw new RangeError('targetStart out of bounds') - if (start < 0 || start >= source.length) throw new RangeError('sourceStart out of bounds') + if (start < 0 || start >= self.length) throw new RangeError('sourceStart out of bounds') if (end < 0) throw new RangeError('sourceEnd out of bounds') // Are we oob? @@ -7123,61 +7131,50 @@ function toHex (n) { return n.toString(16) } -function utf8ToBytes(string, units) { - var codePoint, length = string.length - var leadSurrogate = null +function utf8ToBytes (string, units) { units = units || Infinity + var codePoint + var length = string.length + var leadSurrogate = null var bytes = [] var i = 0 - for (; i 0xD7FF && codePoint < 0xE000) { - // last char was a lead if (leadSurrogate) { - // 2 leads in a row if (codePoint < 0xDC00) { if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) leadSurrogate = codePoint continue - } - - // valid surrogate pair - else { + } else { + // valid surrogate pair codePoint = leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00 | 0x10000 leadSurrogate = null } - } - - // no lead yet - else { + } else { + // no lead yet - // unexpected trail if (codePoint > 0xDBFF) { + // unexpected trail if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) continue - } - - // unpaired lead - else if (i + 1 === length) { + } else if (i + 1 === length) { + // unpaired lead if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) continue - } - - // valid lead - else { + } else { + // valid lead leadSurrogate = codePoint continue } } - } - - // valid bmp char, but last char was a lead - else if (leadSurrogate) { + } else if (leadSurrogate) { + // valid bmp char, but last char was a lead if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) leadSurrogate = null } @@ -7186,32 +7183,28 @@ function utf8ToBytes(string, units) { if (codePoint < 0x80) { if ((units -= 1) < 0) break bytes.push(codePoint) - } - else if (codePoint < 0x800) { + } else if (codePoint < 0x800) { if ((units -= 2) < 0) break bytes.push( codePoint >> 0x6 | 0xC0, codePoint & 0x3F | 0x80 - ); - } - else if (codePoint < 0x10000) { + ) + } else if (codePoint < 0x10000) { if ((units -= 3) < 0) break bytes.push( codePoint >> 0xC | 0xE0, codePoint >> 0x6 & 0x3F | 0x80, codePoint & 0x3F | 0x80 - ); - } - else if (codePoint < 0x200000) { + ) + } else if (codePoint < 0x200000) { if ((units -= 4) < 0) break bytes.push( codePoint >> 0x12 | 0xF0, codePoint >> 0xC & 0x3F | 0x80, codePoint >> 0x6 & 0x3F | 0x80, codePoint & 0x3F | 0x80 - ); - } - else { + ) + } else { throw new Error('Invalid code point') } } @@ -7232,7 +7225,6 @@ function utf16leToBytes (str, units) { var c, hi, lo var byteArray = [] for (var i = 0; i < str.length; i++) { - if ((units -= 2) < 0) break c = str.charCodeAt(i) @@ -7249,8 +7241,7 @@ function base64ToBytes (str) { return base64.toByteArray(base64clean(str)) } -function blitBuffer (src, dst, offset, length, unitSize) { - if (unitSize) length -= length % unitSize; +function blitBuffer (src, dst, offset, length) { for (var i = 0; i < length; i++) { if ((i + offset >= dst.length) || (i >= src.length)) break @@ -7267,7 +7258,7 @@ function decodeUtf8Char (str) { } } -},{"base64-js":23,"ieee754":24,"is-array":25}],23:[function(require,module,exports){ +},{"base64-js":24,"ieee754":25,"is-array":26}],24:[function(require,module,exports){ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; ;(function (exports) { @@ -7393,7 +7384,7 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; exports.fromByteArray = uint8ToBase64 }(typeof exports === 'undefined' ? (this.base64js = {}) : exports)) -},{}],24:[function(require,module,exports){ +},{}],25:[function(require,module,exports){ exports.read = function(buffer, offset, isLE, mLen, nBytes) { var e, m, eLen = nBytes * 8 - mLen - 1, @@ -7479,7 +7470,7 @@ exports.write = function(buffer, value, offset, isLE, mLen, nBytes) { buffer[offset + i - d] |= s * 128; }; -},{}],25:[function(require,module,exports){ +},{}],26:[function(require,module,exports){ /** * isArray @@ -7514,34 +7505,21 @@ module.exports = isArray || function (val) { return !! val && '[object Array]' == str.call(val); }; -},{}],26:[function(require,module,exports){ +},{}],27:[function(require,module,exports){ 'use strict'; exports.randomBytes = exports.rng = exports.pseudoRandomBytes = exports.prng = require('randombytes') -function error () { - var m = [].slice.call(arguments).join(' ') - throw new Error([ - m, - 'we accept pull requests', - 'http://github.com/dominictarr/crypto-browserify' - ].join('\n')) -} - exports.createHash = exports.Hash = require('create-hash') exports.createHmac = exports.Hmac = require('create-hmac') -function each(a, f) { - for(var i in a) - f(a[i], i) -} var hashes = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160'].concat(Object.keys(require('browserify-sign/algos'))) exports.getHashes = function () { return hashes; } -var p = require('./pbkdf2')(exports) +var p = require('pbkdf2-compat') exports.pbkdf2 = p.pbkdf2 exports.pbkdf2Sync = p.pbkdf2Sync @@ -7561,21 +7539,46 @@ var aes = require('browserify-aes'); exports[key] = aes[key]; }) +var dh = require('diffie-hellman'); +[ + 'DiffieHellmanGroup', + 'createDiffieHellmanGroup', + 'getDiffieHellman', + 'createDiffieHellman', + 'DiffieHellman' +].forEach(function (key) { + exports[key] = dh[key]; +}) + require('browserify-sign/inject')(module.exports, exports); -require('diffie-hellman/inject')(exports, module.exports); -require('create-ecdh/inject')(module.exports, exports); -require('public-encrypt/inject')(module.exports, exports); + +exports.createECDH = require('create-ecdh') + +var publicEncrypt = require('public-encrypt'); + +[ + 'publicEncrypt', + 'privateEncrypt', + 'publicDecrypt', + 'privateDecrypt' +].forEach(function (key) { + exports[key] = publicEncrypt[key]; +}) // the least I can do is make error messages for the rest of the node.js/crypto api. -each([ +;[ 'createCredentials' -], function (name) { +].forEach(function (name) { exports[name] = function () { - error('sorry,', name, 'is not implemented yet') + throw new Error([ + 'sorry, ' + name + ' is not implemented yet', + 'we accept pull requests', + 'https://github.com/crypto-browserify/crypto-browserify' + ].join('\n')); } }) -},{"./pbkdf2":162,"browserify-aes":30,"browserify-sign/algos":45,"browserify-sign/inject":46,"create-ecdh/inject":92,"create-hash":114,"create-hmac":125,"diffie-hellman/inject":128,"public-encrypt/inject":134,"randombytes":161}],27:[function(require,module,exports){ +},{"browserify-aes":31,"browserify-sign/algos":46,"browserify-sign/inject":47,"create-ecdh":93,"create-hash":115,"create-hmac":126,"diffie-hellman":127,"pbkdf2-compat":134,"public-encrypt":135,"randombytes":161}],28:[function(require,module,exports){ (function (Buffer){ var md5 = require('create-hash/md5'); module.exports = EVP_BytesToKey; @@ -7640,7 +7643,7 @@ function EVP_BytesToKey(password, keyLen, ivLen) { }; } }).call(this,require("buffer").Buffer) -},{"buffer":22,"create-hash/md5":116}],28:[function(require,module,exports){ +},{"buffer":23,"create-hash/md5":117}],29:[function(require,module,exports){ (function (Buffer){ // based on the aes implimentation in triple sec // https://github.com/keybase/triplesec @@ -7841,7 +7844,7 @@ AES.prototype._doCryptBlock = function(M, keySchedule, SUB_MIX, SBOX) { exports.AES = AES; }).call(this,require("buffer").Buffer) -},{"buffer":22}],29:[function(require,module,exports){ +},{"buffer":23}],30:[function(require,module,exports){ (function (Buffer){ var aes = require('./aes'); var Transform = require('./cipherBase'); @@ -7944,7 +7947,7 @@ function xorTest(a, b) { }).call(this,require("buffer").Buffer) -},{"./aes":28,"./cipherBase":31,"./ghash":34,"./xor":44,"buffer":22,"inherits":164}],30:[function(require,module,exports){ +},{"./aes":29,"./cipherBase":32,"./ghash":35,"./xor":45,"buffer":23,"inherits":163}],31:[function(require,module,exports){ var ciphers = require('./encrypter'); exports.createCipher = exports.Cipher = ciphers.createCipher; exports.createCipheriv = exports.Cipheriv = ciphers.createCipheriv; @@ -7957,7 +7960,7 @@ function getCiphers () { } exports.listCiphers = exports.getCiphers = getCiphers; -},{"./decrypter":32,"./encrypter":33,"./modes":35}],31:[function(require,module,exports){ +},{"./decrypter":33,"./encrypter":34,"./modes":36}],32:[function(require,module,exports){ (function (Buffer){ var Transform = require('stream').Transform; var inherits = require('inherits'); @@ -7997,7 +8000,7 @@ CipherBase.prototype.final = function (outputEnc) { return outData; }; }).call(this,require("buffer").Buffer) -},{"buffer":22,"inherits":164,"stream":179}],32:[function(require,module,exports){ +},{"buffer":23,"inherits":163,"stream":178}],33:[function(require,module,exports){ (function (Buffer){ var aes = require('./aes'); var Transform = require('./cipherBase'); @@ -8137,7 +8140,7 @@ function createDecipher (suite, password) { exports.createDecipher = createDecipher; exports.createDecipheriv = createDecipheriv; }).call(this,require("buffer").Buffer) -},{"./EVP_BytesToKey":27,"./aes":28,"./authCipher":29,"./cipherBase":31,"./modes":35,"./modes/cbc":36,"./modes/cfb":37,"./modes/cfb1":38,"./modes/cfb8":39,"./modes/ctr":40,"./modes/ecb":41,"./modes/ofb":42,"./streamCipher":43,"buffer":22,"inherits":164}],33:[function(require,module,exports){ +},{"./EVP_BytesToKey":28,"./aes":29,"./authCipher":30,"./cipherBase":32,"./modes":36,"./modes/cbc":37,"./modes/cfb":38,"./modes/cfb1":39,"./modes/cfb8":40,"./modes/ctr":41,"./modes/ecb":42,"./modes/ofb":43,"./streamCipher":44,"buffer":23,"inherits":163}],34:[function(require,module,exports){ (function (Buffer){ var aes = require('./aes'); var Transform = require('./cipherBase'); @@ -8261,7 +8264,7 @@ function createCipher (suite, password) { exports.createCipheriv = createCipheriv; exports.createCipher = createCipher; }).call(this,require("buffer").Buffer) -},{"./EVP_BytesToKey":27,"./aes":28,"./authCipher":29,"./cipherBase":31,"./modes":35,"./modes/cbc":36,"./modes/cfb":37,"./modes/cfb1":38,"./modes/cfb8":39,"./modes/ctr":40,"./modes/ecb":41,"./modes/ofb":42,"./streamCipher":43,"buffer":22,"inherits":164}],34:[function(require,module,exports){ +},{"./EVP_BytesToKey":28,"./aes":29,"./authCipher":30,"./cipherBase":32,"./modes":36,"./modes/cbc":37,"./modes/cfb":38,"./modes/cfb1":39,"./modes/cfb8":40,"./modes/ctr":41,"./modes/ecb":42,"./modes/ofb":43,"./streamCipher":44,"buffer":23,"inherits":163}],35:[function(require,module,exports){ (function (Buffer){ var zeros = new Buffer(16); zeros.fill(0); @@ -8362,7 +8365,7 @@ function xor(a, b) { ]; } }).call(this,require("buffer").Buffer) -},{"buffer":22}],35:[function(require,module,exports){ +},{"buffer":23}],36:[function(require,module,exports){ exports['aes-128-ecb'] = { cipher: 'AES', key: 128, @@ -8534,7 +8537,7 @@ exports['aes-256-gcm'] = { mode: 'GCM', type: 'auth' }; -},{}],36:[function(require,module,exports){ +},{}],37:[function(require,module,exports){ var xor = require('../xor'); exports.encrypt = function (self, block) { var data = xor(block, self._prev); @@ -8547,7 +8550,7 @@ exports.decrypt = function (self, block) { var out = self._cipher.decryptBlock(block); return xor(out, pad); }; -},{"../xor":44}],37:[function(require,module,exports){ +},{"../xor":45}],38:[function(require,module,exports){ (function (Buffer){ var xor = require('../xor'); exports.encrypt = function (self, data, decrypt) { @@ -8577,7 +8580,7 @@ function encryptStart(self, data, decrypt) { return out; } }).call(this,require("buffer").Buffer) -},{"../xor":44,"buffer":22}],38:[function(require,module,exports){ +},{"../xor":45,"buffer":23}],39:[function(require,module,exports){ (function (Buffer){ function encryptByte(self, byte, decrypt) { @@ -8615,7 +8618,7 @@ function shiftIn(buffer, value) { return out; } }).call(this,require("buffer").Buffer) -},{"buffer":22}],39:[function(require,module,exports){ +},{"buffer":23}],40:[function(require,module,exports){ (function (Buffer){ function encryptByte(self, byte, decrypt) { var pad = self._cipher.encryptBlock(self._prev); @@ -8633,7 +8636,7 @@ exports.encrypt = function (self, chunk, decrypt) { return out; }; }).call(this,require("buffer").Buffer) -},{"buffer":22}],40:[function(require,module,exports){ +},{"buffer":23}],41:[function(require,module,exports){ (function (Buffer){ var xor = require('../xor'); function getBlock(self) { @@ -8664,14 +8667,14 @@ function incr32(iv) { } } }).call(this,require("buffer").Buffer) -},{"../xor":44,"buffer":22}],41:[function(require,module,exports){ +},{"../xor":45,"buffer":23}],42:[function(require,module,exports){ exports.encrypt = function (self, block) { return self._cipher.encryptBlock(block); }; exports.decrypt = function (self, block) { return self._cipher.decryptBlock(block); }; -},{}],42:[function(require,module,exports){ +},{}],43:[function(require,module,exports){ (function (Buffer){ var xor = require('../xor'); function getBlock(self) { @@ -8687,7 +8690,7 @@ exports.encrypt = function (self, chunk) { return xor(chunk, pad); }; }).call(this,require("buffer").Buffer) -},{"../xor":44,"buffer":22}],43:[function(require,module,exports){ +},{"../xor":45,"buffer":23}],44:[function(require,module,exports){ (function (Buffer){ var aes = require('./aes'); var Transform = require('./cipherBase'); @@ -8715,7 +8718,7 @@ StreamCipher.prototype._final = function () { this._cipher.scrub(); }; }).call(this,require("buffer").Buffer) -},{"./aes":28,"./cipherBase":31,"buffer":22,"inherits":164}],44:[function(require,module,exports){ +},{"./aes":29,"./cipherBase":32,"buffer":23,"inherits":163}],45:[function(require,module,exports){ (function (Buffer){ module.exports = xor; function xor(a, b) { @@ -8728,7 +8731,7 @@ function xor(a, b) { return out; } }).call(this,require("buffer").Buffer) -},{"buffer":22}],45:[function(require,module,exports){ +},{"buffer":23}],46:[function(require,module,exports){ (function (Buffer){ exports['RSA-SHA224'] = exports.sha224WithRSAEncryption = { sign: 'rsa', @@ -8801,7 +8804,7 @@ exports['RSA-MD5'] = exports.md5WithRSAEncryption = { id: new Buffer('3020300c06082a864886f70d020505000410', 'hex') }; }).call(this,require("buffer").Buffer) -},{"buffer":22}],46:[function(require,module,exports){ +},{"buffer":23}],47:[function(require,module,exports){ (function (Buffer){ var sign = require('./sign'); var verify = require('./verify'); @@ -8883,7 +8886,11 @@ Verify.prototype.verify = function verifyMethod(key, sig, enc) { return verify(sig, Buffer.concat([this._tag, hash]), key); }; }).call(this,require("buffer").Buffer) -},{"./algos":45,"./sign":89,"./verify":90,"buffer":22,"inherits":164,"stream":179}],47:[function(require,module,exports){ +},{"./algos":46,"./sign":90,"./verify":91,"buffer":23,"inherits":163,"stream":178}],48:[function(require,module,exports){ +(function(module, exports) { + +'use strict'; + // Utils function assert(val, msg) { @@ -8891,19 +8898,14 @@ function assert(val, msg) { throw new Error(msg || 'Assertion failed'); } -function assertEqual(l, r, msg) { - if (l != r) - throw new Error(msg || ('Assertion failed: ' + l + ' != ' + r)); -} - // Could use `inherits` module, but don't want to move from single file // architecture yet. function inherits(ctor, superCtor) { - ctor.super_ = superCtor - var TempCtor = function () {} - TempCtor.prototype = superCtor.prototype - ctor.prototype = new TempCtor() - ctor.prototype.constructor = ctor + ctor.super_ = superCtor; + var TempCtor = function () {}; + TempCtor.prototype = superCtor.prototype; + ctor.prototype = new TempCtor(); + ctor.prototype.constructor = ctor; } // BN @@ -8933,6 +8935,8 @@ function BN(number, base, endian) { } if (typeof module === 'object') module.exports = BN; +else + exports.BN = BN; BN.BN = BN; BN.wordSize = 26; @@ -9105,7 +9109,10 @@ BN.prototype._parseBase = function _parseBase(number, base, start) { word = parseBase(number, i, i + limbLen, base); this.imuln(limbPow); - this.words[0] += word; + if (this.words[0] + word < 0x4000000) + this.words[0] += word; + else + this._iaddn(word); } if (mod !== 0) { @@ -9115,7 +9122,10 @@ BN.prototype._parseBase = function _parseBase(number, base, start) { for (var i = 0; i < mod; i++) pow *= base; this.imuln(pow); - this.words[0] += word; + if (this.words[0] + word < 0x4000000) + this.words[0] += word; + else + this._iaddn(word); } }; @@ -9172,7 +9182,6 @@ var base = 2 - 1; while (++base < 36 + 1) { var groupSize = 0; var groupBase = 1; - // TODO: <= while (groupBase < (1 << BN.wordSize) / base) { groupBase *= base; groupSize += 1; @@ -9361,8 +9370,6 @@ BN.prototype.bitLength = function bitLength() { }; BN.prototype.byteLength = function byteLength() { - var hi = 0; - var w = this.words[this.length - 1]; return Math.ceil(this.bitLength() / 8); }; @@ -9376,6 +9383,115 @@ BN.prototype.neg = function neg() { return r; }; + +// Or `num` with `this` in-place +BN.prototype.ior = function ior(num) { + this.sign = this.sign || num.sign; + + while (this.length < num.length) + this.words[this.length++] = 0; + + for (var i = 0; i < num.length; i++) + this.words[i] = this.words[i] | num.words[i]; + + return this.strip(); +}; + + +// Or `num` with `this` +BN.prototype.or = function or(num) { + if (this.length > num.length) + return this.clone().ior(num); + else + return num.clone().ior(this); +}; + + +// And `num` with `this` in-place +BN.prototype.iand = function iand(num) { + this.sign = this.sign && num.sign; + + // b = min-length(num, this) + var b; + if (this.length > num.length) + b = num; + else + b = this; + + for (var i = 0; i < b.length; i++) + this.words[i] = this.words[i] & num.words[i]; + + this.length = b.length; + + return this.strip(); +}; + + +// And `num` with `this` +BN.prototype.and = function and(num) { + if (this.length > num.length) + return this.clone().iand(num); + else + return num.clone().iand(this); +}; + + +// Xor `num` with `this` in-place +BN.prototype.ixor = function ixor(num) { + this.sign = this.sign || num.sign; + + // a.length > b.length + var a; + var b; + if (this.length > num.length) { + a = this; + b = num; + } else { + a = num; + b = this; + } + + for (var i = 0; i < b.length; i++) + this.words[i] = a.words[i] ^ b.words[i]; + + if (this !== a) + for (; i < a.length; i++) + this.words[i] = a.words[i]; + + this.length = a.length; + + return this.strip(); +}; + + +// Xor `num` with `this` +BN.prototype.xor = function xor(num) { + if (this.length > num.length) + return this.clone().ixor(num); + else + return num.clone().ixor(this); +}; + + +// Set `bit` of `this` +BN.prototype.setn = function setn(bit, val) { + assert(typeof bit === 'number' && bit >= 0); + + var off = (bit / 26) | 0; + var wbit = bit % 26; + + while (this.length <= off) + this.words[this.length++] = 0; + + if (val) + this.words[off] = this.words[off] | (1 << wbit); + else + this.words[off] = this.words[off] & ~(1 << wbit); + + return this.strip(); +}; + + // Add `num` to `this` in-place BN.prototype.iadd = function iadd(num) { // negative + positive @@ -9478,34 +9594,26 @@ BN.prototype.isub = function isub(num) { } // a > b + var a; + var b; if (cmp > 0) { - var a = this; - var b = num; + a = this; + b = num; } else { - var a = num; - var b = this; + a = num; + b = this; } var carry = 0; for (var i = 0; i < b.length; i++) { - var r = a.words[i] - b.words[i] - carry; - if (r < 0) { - r += 0x4000000; - carry = 1; - } else { - carry = 0; - } - this.words[i] = r; + var r = a.words[i] - b.words[i] + carry; + carry = r >> 26; + this.words[i] = r & 0x3ffffff; } for (; carry !== 0 && i < a.length; i++) { - var r = a.words[i] - carry; - if (r < 0) { - r += 0x4000000; - carry = 1; - } else { - carry = 0; - } - this.words[i] = r; + var r = a.words[i] + carry; + carry = r >> 26; + this.words[i] = r & 0x3ffffff; } // Copy rest of the words @@ -9670,7 +9778,6 @@ BN.prototype.imul = function imul(num) { this.length = this.length + num.length; this.words[this.length - 1] = 0; - var lastCarry = 0; for (var k = this.length - 2; k >= 0; k--) { // Sum all words with the same `i + j = k` and accumulate `carry`, // note that carry could be >= 0x3ffffff @@ -9712,9 +9819,12 @@ BN.prototype.imuln = function imuln(num) { var carry = 0; for (var i = 0; i < this.length; i++) { var w = this.words[i] * num; - var lo = (w & 0x3ffffff) + carry; - carry = (w / 0x4000000) | 0; - this.words[i] = lo; + var lo = (w & 0x3ffffff) + (carry & 0x3ffffff); + carry >>= 26; + carry += (w / 0x4000000) | 0; + // NOTE: lo is 27bit maximum + carry += lo >>> 26; + this.words[i] = lo & 0x3ffffff; } if (carry !== 0) { @@ -9742,7 +9852,6 @@ BN.prototype.ishln = function ishln(bits) { var s = (bits - r) / 26; var carryMask = (0x3ffffff >>> (26 - r)) << (26 - r); - var o = this.clone(); if (r !== 0) { var carry = 0; for (var i = 0; i < this.length; i++) { @@ -9899,6 +10008,12 @@ BN.prototype.iaddn = function iaddn(num) { this.sign = true; return this; } + + // Add without checks + return this._iaddn(num); +}; + +BN.prototype._iaddn = function _iaddn(num) { this.words[0] += num; // Carry @@ -9949,77 +10064,120 @@ BN.prototype.subn = function subn(num) { BN.prototype.iabs = function iabs() { this.sign = false; - return this + return this; }; BN.prototype.abs = function abs() { return this.clone().iabs(); }; +BN.prototype._ishlnsubmul = function _ishlnsubmul(num, mul, shift) { + // Bigger storage is needed + var len = num.length + shift; + var i; + if (this.words.length < len) { + var t = new Array(len); + for (var i = 0; i < this.length; i++) + t[i] = this.words[i]; + this.words = t; + } else { + i = this.length; + } + + // Zeroify rest + this.length = Math.max(this.length, len); + for (; i < this.length; i++) + this.words[i] = 0; + + var carry = 0; + for (var i = 0; i < num.length; i++) { + var w = this.words[i + shift] + carry; + var right = num.words[i] * mul; + w -= right & 0x3ffffff; + carry = (w >> 26) - ((right / 0x4000000) | 0); + this.words[i + shift] = w & 0x3ffffff; + } + for (; i < this.length - shift; i++) { + var w = this.words[i + shift] + carry; + carry = w >> 26; + this.words[i + shift] = w & 0x3ffffff; + } + + if (carry === 0) + return this.strip(); + + // Subtraction overflow + assert(carry === -1); + carry = 0; + for (var i = 0; i < this.length; i++) { + var w = -this.words[i] + carry; + carry = w >> 26; + this.words[i] = w & 0x3ffffff; + } + this.sign = true; + + return this.strip(); +}; + BN.prototype._wordDiv = function _wordDiv(num, mode) { var shift = this.length - num.length; var a = this.clone(); var b = num; - var q = mode !== 'mod' && new BN(0); - var sign = false; - - // Approximate quotient at each step - while (a.length > b.length) { - // NOTE: a.length is always >= 2, because of the condition .div() - var hi = a.words[a.length - 1] * 0x4000000 + a.words[a.length - 2]; - var sq = (hi / b.words[b.length - 1]); - var sqhi = (sq / 0x4000000) | 0; - var sqlo = sq & 0x3ffffff; - sq = new BN(null); - sq.words = [ sqlo, sqhi ]; - sq.length = 2; - - // Collect quotient - var shift = (a.length - b.length - 1) * 26; - if (q) { - var t = sq.shln(shift); - if (a.sign) - q.isub(t); - else - q.iadd(t); - } + // Normalize + var bhi = b.words[b.length - 1]; + for (var shift = 0; bhi < 0x2000000; shift++) + bhi <<= 1; + if (shift !== 0) { + b = b.shln(shift); + a.ishln(shift); + bhi = b.words[b.length - 1]; + } - sq = sq.mul(b).ishln(shift); - if (a.sign) - a.iadd(sq) - else - a.isub(sq); - } - // At this point a.length <= b.length - while (a.ucmp(b) >= 0) { - // NOTE: a.length is always >= 2, because of the condition above - var hi = a.words[a.length - 1]; - var sq = new BN((hi / b.words[b.length - 1]) | 0); - var shift = (a.length - b.length) * 26; - - if (q) { - var t = sq.shln(shift); - if (a.sign) - q.isub(t); - else - q.iadd(t); - } + // Initialize quotient + var m = a.length - b.length; + var q; - sq = sq.mul(b).ishln(shift); + if (mode !== 'mod') { + q = new BN(null); + q.length = m + 1; + q.words = new Array(q.length); + for (var i = 0; i < q.length; i++) + q.words[i] = 0; + } - if (a.sign) - a.iadd(sq); - else - a.isub(sq); + var diff = a.clone()._ishlnsubmul(b, 1, m); + if (!diff.sign) { + a = diff; + if (q) + q.words[m] = 1; } - if (a.sign) { + for (var j = m - 1; j >= 0; j--) { + var qj = a.words[b.length + j] * 0x4000000 + a.words[b.length + j - 1]; + + // NOTE: (qj / bhi) is (0x3ffffff * 0x4000000 + 0x3ffffff) / 0x2000000 max + // (0x7ffffff) + qj = Math.min((qj / bhi) | 0, 0x3ffffff); + + a._ishlnsubmul(b, qj, j); + while (a.sign) { + qj--; + a.sign = false; + a._ishlnsubmul(b, 1, j); + a.sign = !a.sign; + } if (q) - q.isubn(1); - a.iadd(b); + q.words[j] = qj; } + if (q) + q.strip(); + a.strip(); + + // Denormalize + if (mode !== 'div' && shift !== 0) + a.ishrn(shift); return { div: q ? q : null, mod: a }; }; @@ -10218,11 +10376,11 @@ BN.prototype.invm = function invm(num) { return this._egcd(new BN(1), num).mod(num); }; -BN.prototype.isEven = function isEven(num) { +BN.prototype.isEven = function isEven() { return (this.words[0] & 1) === 0; }; -BN.prototype.isOdd = function isOdd(num) { +BN.prototype.isOdd = function isOdd() { return (this.words[0] & 1) === 1; }; @@ -10502,30 +10660,27 @@ K256.prototype.imulK = function imulK(num) { num.words[num.length + 1] = 0; num.length += 2; - var uhi = 0; - var hi = 0; + // bounded at: 0x40 * 0x3ffffff + 0x3d0 = 0x100000390 + var hi; var lo = 0; for (var i = 0; i < num.length; i++) { var w = num.words[i]; - hi += w * 0x40; + hi = w * 0x40; lo += w * 0x3d1; hi += (lo / 0x4000000) | 0; - uhi += (hi / 0x4000000) | 0; - hi &= 0x3ffffff; lo &= 0x3ffffff; num.words[i] = lo; lo = hi; - hi = uhi; - uhi = 0; } // Fast length reduction - if (num.words[num.length - 1] === 0) - num.length--; - if (num.words[num.length - 1] === 0) + if (num.words[num.length - 1] === 0) { num.length--; + if (num.words[num.length - 1] === 0) + num.length--; + } return num; }; @@ -10590,7 +10745,7 @@ BN._prime = function prime(name) { primes[name] = prime; return prime; -} +}; // // Base reduction engine @@ -10807,12 +10962,9 @@ function Mont(m) { this.r2 = this.imod(this.r.sqr()); this.rinv = this.r.invm(this.m); - // TODO(indutny): simplify it - this.minv = this.rinv.mul(this.r) - .sub(new BN(1)) - .div(this.m) - .neg() - .mod(this.r); + this.minv = this.rinv.mul(this.r).isubn(1).div(this.m); + this.minv.sign = true; + this.minv = this.minv.mod(this.r); } inherits(Mont, Red); @@ -10867,7 +11019,9 @@ Mont.prototype.invm = function invm(a) { return res._forceRed(this); }; -},{}],48:[function(require,module,exports){ +})(typeof module === 'undefined' || module, this); + +},{}],49:[function(require,module,exports){ (function (Buffer){ var bn = require('bn.js'); module.exports = crt; @@ -10915,7 +11069,7 @@ function getr(priv, crypto) { return r; } }).call(this,require("buffer").Buffer) -},{"bn.js":47,"buffer":22}],49:[function(require,module,exports){ +},{"bn.js":48,"buffer":23}],50:[function(require,module,exports){ var elliptic = exports; elliptic.version = require('../package.json').version; @@ -10928,7 +11082,7 @@ elliptic.curves = require('./elliptic/curves'); // Protocols elliptic.ec = require('./elliptic/ec'); -},{"../package.json":68,"./elliptic/curve":52,"./elliptic/curves":55,"./elliptic/ec":56,"./elliptic/hmac-drbg":59,"./elliptic/utils":60,"brorand":61}],50:[function(require,module,exports){ +},{"../package.json":69,"./elliptic/curve":53,"./elliptic/curves":56,"./elliptic/ec":57,"./elliptic/hmac-drbg":60,"./elliptic/utils":61,"brorand":62}],51:[function(require,module,exports){ var bn = require('bn.js'); var elliptic = require('../../elliptic'); @@ -11232,7 +11386,7 @@ BasePoint.prototype.dblp = function dblp(k) { return r; }; -},{"../../elliptic":49,"bn.js":47}],51:[function(require,module,exports){ +},{"../../elliptic":50,"bn.js":48}],52:[function(require,module,exports){ var curve = require('../curve'); var elliptic = require('../../elliptic'); var bn = require('bn.js'); @@ -11595,7 +11749,7 @@ Point.prototype.getY = function getY() { Point.prototype.toP = Point.prototype.normalize; Point.prototype.mixedAdd = Point.prototype.add; -},{"../../elliptic":49,"../curve":52,"bn.js":47,"inherits":164}],52:[function(require,module,exports){ +},{"../../elliptic":50,"../curve":53,"bn.js":48,"inherits":163}],53:[function(require,module,exports){ var curve = exports; curve.base = require('./base'); @@ -11603,7 +11757,7 @@ curve.short = require('./short'); curve.mont = require('./mont'); curve.edwards = require('./edwards'); -},{"./base":50,"./edwards":51,"./mont":53,"./short":54}],53:[function(require,module,exports){ +},{"./base":51,"./edwards":52,"./mont":54,"./short":55}],54:[function(require,module,exports){ var curve = require('../curve'); var elliptic = require('../../elliptic'); var bn = require('bn.js'); @@ -11768,7 +11922,7 @@ Point.prototype.getX = function getX() { return this.x.fromRed(); }; -},{"../../elliptic":49,"../curve":52,"bn.js":47,"inherits":164}],54:[function(require,module,exports){ +},{"../../elliptic":50,"../curve":53,"bn.js":48,"inherits":163}],55:[function(require,module,exports){ var curve = require('../curve'); var elliptic = require('../../elliptic'); var bn = require('bn.js'); @@ -12666,7 +12820,7 @@ JPoint.prototype.isInfinity = function isInfinity() { return this.z.cmpn(0) === 0; }; -},{"../../elliptic":49,"../curve":52,"bn.js":47,"inherits":164}],55:[function(require,module,exports){ +},{"../../elliptic":50,"../curve":53,"bn.js":48,"inherits":163}],56:[function(require,module,exports){ var curves = exports; var hash = require('hash.js'); @@ -13596,7 +13750,7 @@ defineCurve('secp256k1', { ] }); -},{"../elliptic":49,"bn.js":47,"hash.js":62}],56:[function(require,module,exports){ +},{"../elliptic":50,"bn.js":48,"hash.js":63}],57:[function(require,module,exports){ var bn = require('bn.js'); var elliptic = require('../../elliptic'); var utils = elliptic.utils; @@ -13749,7 +13903,7 @@ EC.prototype.verify = function verify(msg, signature, key) { return p.getX().mod(this.n).cmp(r) === 0; }; -},{"../../elliptic":49,"./key":57,"./signature":58,"bn.js":47}],57:[function(require,module,exports){ +},{"../../elliptic":50,"./key":58,"./signature":59,"bn.js":48}],58:[function(require,module,exports){ var bn = require('bn.js'); var elliptic = require('../../elliptic'); @@ -13895,7 +14049,7 @@ KeyPair.prototype.inspect = function inspect() { ' pub: ' + (this.pub && this.pub.inspect()) + ' >'; }; -},{"../../elliptic":49,"bn.js":47}],58:[function(require,module,exports){ +},{"../../elliptic":50,"bn.js":48}],59:[function(require,module,exports){ var bn = require('bn.js'); var elliptic = require('../../elliptic'); @@ -13960,7 +14114,7 @@ Signature.prototype.toDER = function toDER(enc) { return utils.encode(res, enc); }; -},{"../../elliptic":49,"bn.js":47}],59:[function(require,module,exports){ +},{"../../elliptic":50,"bn.js":48}],60:[function(require,module,exports){ var hash = require('hash.js'); var elliptic = require('../elliptic'); var utils = elliptic.utils; @@ -14074,7 +14228,7 @@ HmacDRBG.prototype.generate = function generate(len, enc, add, addEnc) { return utils.encode(res, enc); }; -},{"../elliptic":49,"hash.js":62}],60:[function(require,module,exports){ +},{"../elliptic":50,"hash.js":63}],61:[function(require,module,exports){ var bn = require('bn.js'); var utils = exports; @@ -14226,7 +14380,7 @@ function getJSF(k1, k2) { } utils.getJSF = getJSF; -},{"bn.js":47}],61:[function(require,module,exports){ +},{"bn.js":48}],62:[function(require,module,exports){ var r; module.exports = function rand(len) { @@ -14285,7 +14439,7 @@ if (typeof window === 'object') { } } -},{}],62:[function(require,module,exports){ +},{}],63:[function(require,module,exports){ var hash = exports; hash.utils = require('./hash/utils'); @@ -14302,7 +14456,7 @@ hash.sha384 = hash.sha.sha384; hash.sha512 = hash.sha.sha512; hash.ripemd160 = hash.ripemd.ripemd160; -},{"./hash/common":63,"./hash/hmac":64,"./hash/ripemd":65,"./hash/sha":66,"./hash/utils":67}],63:[function(require,module,exports){ +},{"./hash/common":64,"./hash/hmac":65,"./hash/ripemd":66,"./hash/sha":67,"./hash/utils":68}],64:[function(require,module,exports){ var hash = require('../hash'); var utils = hash.utils; var assert = utils.assert; @@ -14395,7 +14549,7 @@ BlockHash.prototype._pad = function pad() { return res; }; -},{"../hash":62}],64:[function(require,module,exports){ +},{"../hash":63}],65:[function(require,module,exports){ var hmac = exports; var hash = require('../hash'); @@ -14445,7 +14599,7 @@ Hmac.prototype.digest = function digest(enc) { return this.outer.digest(enc); }; -},{"../hash":62}],65:[function(require,module,exports){ +},{"../hash":63}],66:[function(require,module,exports){ var hash = require('../hash'); var utils = hash.utils; @@ -14591,7 +14745,7 @@ var sh = [ 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11 ]; -},{"../hash":62}],66:[function(require,module,exports){ +},{"../hash":63}],67:[function(require,module,exports){ var hash = require('../hash'); var utils = hash.utils; var assert = utils.assert; @@ -15157,7 +15311,7 @@ function g1_512_lo(xh, xl) { return r; } -},{"../hash":62}],67:[function(require,module,exports){ +},{"../hash":63}],68:[function(require,module,exports){ var utils = exports; var inherits = require('inherits'); @@ -15416,7 +15570,7 @@ function shr64_lo(ah, al, num) { }; exports.shr64_lo = shr64_lo; -},{"inherits":164}],68:[function(require,module,exports){ +},{"inherits":163}],69:[function(require,module,exports){ module.exports={ "name": "elliptic", "version": "1.0.1", @@ -15475,10 +15629,11 @@ module.exports={ "tarball": "http://registry.npmjs.org/elliptic/-/elliptic-1.0.1.tgz" }, "directories": {}, - "_resolved": "https://registry.npmjs.org/elliptic/-/elliptic-1.0.1.tgz" + "_resolved": "https://registry.npmjs.org/elliptic/-/elliptic-1.0.1.tgz", + "readme": "ERROR: No README data found!" } -},{}],69:[function(require,module,exports){ +},{}],70:[function(require,module,exports){ (function (Buffer){ module.exports = function evp(crypto, password, salt, keyLen) { @@ -15520,7 +15675,7 @@ module.exports = function evp(crypto, password, salt, keyLen) { return key; }; }).call(this,require("buffer").Buffer) -},{"buffer":22}],70:[function(require,module,exports){ +},{"buffer":23}],71:[function(require,module,exports){ module.exports={"2.16.840.1.101.3.4.1.1": "aes-128-ecb", "2.16.840.1.101.3.4.1.2": "aes-128-cbc", "2.16.840.1.101.3.4.1.3": "aes-128-ofb", @@ -15534,7 +15689,7 @@ module.exports={"2.16.840.1.101.3.4.1.1": "aes-128-ecb", "2.16.840.1.101.3.4.1.43": "aes-256-ofb", "2.16.840.1.101.3.4.1.44": "aes-256-cfb" } -},{}],71:[function(require,module,exports){ +},{}],72:[function(require,module,exports){ // from https://github.com/indutny/self-signed/blob/gh-pages/lib/asn1.js // Fedor, you are amazing. @@ -15697,7 +15852,7 @@ exports.signature = asn1.define('signature', function() { this.key('s').int() ); }); -},{"asn1.js":75,"asn1.js-rfc3280":74}],72:[function(require,module,exports){ +},{"asn1.js":76,"asn1.js-rfc3280":75}],73:[function(require,module,exports){ (function (Buffer){ var findProc = /Proc-Type: 4,ENCRYPTED\n\r?DEK-Info: AES-((?:128)|(?:192)|(?:256))-CBC,([0-9A-H]+)\n\r?\n\r?([0-9A-z\n\r\+\/\=]+)\n\r?/m; var startRegex = /^-----BEGIN (.*)-----\n/; @@ -15736,7 +15891,7 @@ function wrap(str) { return chunks.join("\n"); } }).call(this,require("buffer").Buffer) -},{"./EVP_BytesToKey":69,"buffer":22}],73:[function(require,module,exports){ +},{"./EVP_BytesToKey":70,"buffer":23}],74:[function(require,module,exports){ (function (Buffer){ var pemstrip = require('pemstrip'); var asn1 = require('./asn1'); @@ -15842,7 +15997,7 @@ function decrypt(crypto, data, password) { return Buffer.concat(out); } }).call(this,require("buffer").Buffer) -},{"./aesid.json":70,"./asn1":71,"./fixProc":72,"buffer":22,"pemstrip":88}],74:[function(require,module,exports){ +},{"./aesid.json":71,"./asn1":72,"./fixProc":73,"buffer":23,"pemstrip":89}],75:[function(require,module,exports){ try { var asn1 = require('asn1.js'); } catch (e) { @@ -15996,7 +16151,7 @@ var AttributeValue = asn1.define('AttributeValue', function() { }); exports.AttributeValue = AttributeValue; -},{"asn1.js":75}],75:[function(require,module,exports){ +},{"asn1.js":76}],76:[function(require,module,exports){ var asn1 = exports; asn1.bignum = require('bn.js'); @@ -16007,7 +16162,7 @@ asn1.constants = require('./asn1/constants'); asn1.decoders = require('./asn1/decoders'); asn1.encoders = require('./asn1/encoders'); -},{"./asn1/api":76,"./asn1/base":78,"./asn1/constants":82,"./asn1/decoders":84,"./asn1/encoders":86,"bn.js":47}],76:[function(require,module,exports){ +},{"./asn1/api":77,"./asn1/base":79,"./asn1/constants":83,"./asn1/decoders":85,"./asn1/encoders":87,"bn.js":48}],77:[function(require,module,exports){ var asn1 = require('../asn1'); var inherits = require('inherits'); var vm = require('vm'); @@ -16060,7 +16215,7 @@ Entity.prototype.encode = function encode(data, enc, /* internal */ reporter) { return this._getEncoder(enc).encode(data, reporter); }; -},{"../asn1":75,"inherits":164,"vm":183}],77:[function(require,module,exports){ +},{"../asn1":76,"inherits":163,"vm":182}],78:[function(require,module,exports){ var inherits = require('inherits'); var Reporter = require('../base').Reporter; var Buffer = require('buffer').Buffer; @@ -16177,7 +16332,7 @@ EncoderBuffer.prototype.join = function join(out, offset) { return out; }; -},{"../base":78,"buffer":22,"inherits":164}],78:[function(require,module,exports){ +},{"../base":79,"buffer":23,"inherits":163}],79:[function(require,module,exports){ var base = exports; base.Reporter = require('./reporter').Reporter; @@ -16185,7 +16340,7 @@ base.DecoderBuffer = require('./buffer').DecoderBuffer; base.EncoderBuffer = require('./buffer').EncoderBuffer; base.Node = require('./node'); -},{"./buffer":77,"./node":79,"./reporter":80}],79:[function(require,module,exports){ +},{"./buffer":78,"./node":80,"./reporter":81}],80:[function(require,module,exports){ var Reporter = require('../base').Reporter; var EncoderBuffer = require('../base').EncoderBuffer; var assert = require('minimalistic-assert'); @@ -16762,7 +16917,7 @@ Node.prototype._encodePrimitive = function encodePrimitive(tag, data) { throw new Error('Unsupported tag: ' + tag); }; -},{"../base":78,"minimalistic-assert":87}],80:[function(require,module,exports){ +},{"../base":79,"minimalistic-assert":88}],81:[function(require,module,exports){ var inherits = require('inherits'); function Reporter(options) { @@ -16853,7 +17008,7 @@ ReporterError.prototype.rethrow = function rethrow(msg) { return this; }; -},{"inherits":164}],81:[function(require,module,exports){ +},{"inherits":163}],82:[function(require,module,exports){ var constants = require('../constants'); exports.tagClass = { @@ -16897,7 +17052,7 @@ exports.tag = { }; exports.tagByName = constants._reverse(exports.tag); -},{"../constants":82}],82:[function(require,module,exports){ +},{"../constants":83}],83:[function(require,module,exports){ var constants = exports; // Helper @@ -16918,7 +17073,7 @@ constants._reverse = function reverse(map) { constants.der = require('./der'); -},{"./der":81}],83:[function(require,module,exports){ +},{"./der":82}],84:[function(require,module,exports){ var inherits = require('inherits'); var asn1 = require('../../asn1'); @@ -17220,12 +17375,12 @@ function derDecodeLen(buf, primitive, fail) { return len; } -},{"../../asn1":75,"inherits":164}],84:[function(require,module,exports){ +},{"../../asn1":76,"inherits":163}],85:[function(require,module,exports){ var decoders = exports; decoders.der = require('./der'); -},{"./der":83}],85:[function(require,module,exports){ +},{"./der":84}],86:[function(require,module,exports){ var inherits = require('inherits'); var Buffer = require('buffer').Buffer; @@ -17497,12 +17652,12 @@ function encodeTag(tag, primitive, cls, reporter) { return res; } -},{"../../asn1":75,"buffer":22,"inherits":164}],86:[function(require,module,exports){ +},{"../../asn1":76,"buffer":23,"inherits":163}],87:[function(require,module,exports){ var encoders = exports; encoders.der = require('./der'); -},{"./der":85}],87:[function(require,module,exports){ +},{"./der":86}],88:[function(require,module,exports){ module.exports = assert; function assert(val, msg) { @@ -17515,7 +17670,7 @@ assert.equal = function assertEqual(l, r, msg) { throw new Error(msg || ('Assertion failed: ' + l + ' != ' + r)); }; -},{}],88:[function(require,module,exports){ +},{}],89:[function(require,module,exports){ exports.strip = function strip(artifact) { artifact = artifact.toString() var startRegex = /^-----BEGIN (.*)-----\n/; @@ -17549,7 +17704,7 @@ exports.assemble = function assemble(info) { var endLine = "-----END " + tag + "-----"; return startLine + "\n" + wrap(base64, 64) + "\n" + endLine + "\n"; } -},{}],89:[function(require,module,exports){ +},{}],90:[function(require,module,exports){ (function (Buffer){ // much of this based on https://github.com/indutny/self-signed/blob/gh-pages/lib/rsa.js var parseKeys = require('parse-asn1'); @@ -17712,7 +17867,7 @@ function makeR(g, k, p, q) { return g.toRed(bn.mont(p)).redPow(k).fromRed().mod(q); } }).call(this,require("buffer").Buffer) -},{"bn.js":47,"browserify-rsa":48,"buffer":22,"elliptic":49,"parse-asn1":73}],90:[function(require,module,exports){ +},{"bn.js":48,"browserify-rsa":49,"buffer":23,"elliptic":50,"parse-asn1":74}],91:[function(require,module,exports){ (function (Buffer){ // much of this based on https://github.com/indutny/self-signed/blob/gh-pages/lib/rsa.js var parseKeys = require('parse-asn1'); @@ -17792,17 +17947,53 @@ function checkValue(b, q) { } } }).call(this,require("buffer").Buffer) -},{"bn.js":47,"buffer":22,"elliptic":49,"parse-asn1":73}],91:[function(require,module,exports){ +},{"bn.js":48,"buffer":23,"elliptic":50,"parse-asn1":74}],92:[function(require,module,exports){ (function (Buffer){ var elliptic = require('elliptic'); var BN = require('bn.js'); -module.exports = ECDH; -function ECDH(curve, crypto) { - elliptic.rand = crypto.randomBytes; - this.curve = new elliptic.ec(curve); +module.exports = function createECDH(curve) { + return new ECDH(curve); +}; + +var aliases = { + secp256k1: { + name: 'secp256k1', + byteLength: 32 + }, + secp224r1: { + name: 'p224', + byteLength: 28 + }, + prime256v1: { + name: 'p256', + byteLength: 32 + }, + prime192v1: { + name: 'p192', + byteLength: 24 + }, + ed25519: { + name: 'ed25519', + byteLength: 32 + } +}; + +aliases.p224 = aliases.secp224r1; +aliases.p256 = aliases.secp256r1 = aliases.prime256v1; +aliases.p192 = aliases.secp192r1 = aliases.prime192v1; + +function ECDH(curve) { + this.curveType = aliases[curve]; + if (!this.curveType ) { + this.curveType = { + name: curve + }; + } + this.curve = new elliptic.ec(this.curveType.name); this.keys = void 0; } + ECDH.prototype.generateKeys = function (enc, format) { this.keys = this.curve.genKeyPair(); return this.getPublicKey(enc, format); @@ -17817,8 +18008,9 @@ ECDH.prototype.computeSecret = function (other, inenc, enc) { other = other.toString(16); var otherPub = this.curve.keyPair(other, 'hex').getPublic(); var out = otherPub.mul(this.keys.getPrivate()).getX(); - return returnValue(out, enc); + return formatReturnValue(out, enc, this.curveType.byteLength); }; + ECDH.prototype.getPublicKey = function (enc, format) { var key = this.keys.getPublic(format === 'compressed', true); if (format === 'hybrid') { @@ -17828,10 +18020,11 @@ ECDH.prototype.getPublicKey = function (enc, format) { key [0] = 6; } } - return returnValue(key, enc); + return formatReturnValue(key, enc); }; + ECDH.prototype.getPrivateKey = function (enc) { - return returnValue(this.keys.getPrivate(), enc); + return formatReturnValue(this.keys.getPrivate(), enc); }; ECDH.prototype.setPublicKey = function (pub, enc) { @@ -17842,7 +18035,9 @@ ECDH.prototype.setPublicKey = function (pub, enc) { var pkey = new BN(pub); pkey = pkey.toArray(); this.keys._importPublicHex(pkey); + return this; }; + ECDH.prototype.setPrivateKey = function (priv, enc) { enc = enc || 'utf8'; if (!Buffer.isBuffer(priv)) { @@ -17851,12 +18046,19 @@ ECDH.prototype.setPrivateKey = function (priv, enc) { var _priv = new BN(priv); _priv = _priv.toString(16); this.keys._importPrivate(_priv); + return this; }; -function returnValue(bn, enc) { + +function formatReturnValue(bn, enc, len) { if (!Array.isArray(bn)) { bn = bn.toArray(); } var buf = new Buffer(bn); + if (len && buf.length < len) { + var zeros = new Buffer(len - buf.length); + zeros.fill(0); + buf = Buffer.concat([zeros, buf]); + } if (!enc) { return buf; } else { @@ -17864,56 +18066,53 @@ function returnValue(bn, enc) { } } }).call(this,require("buffer").Buffer) -},{"bn.js":93,"buffer":22,"elliptic":94}],92:[function(require,module,exports){ -var ECDH = require('./ecdh'); -module.exports = function (crypto, exports) { - exports.createECDH = function (curve) { - return new ECDH(curve, crypto); - }; -}; -},{"./ecdh":91}],93:[function(require,module,exports){ -arguments[4][47][0].apply(exports,arguments) -},{"dup":47}],94:[function(require,module,exports){ -arguments[4][49][0].apply(exports,arguments) -},{"../package.json":113,"./elliptic/curve":97,"./elliptic/curves":100,"./elliptic/ec":101,"./elliptic/hmac-drbg":104,"./elliptic/utils":105,"brorand":106,"dup":49}],95:[function(require,module,exports){ +},{"bn.js":94,"buffer":23,"elliptic":95}],93:[function(require,module,exports){ +var createECDH = require('crypto').createECDH; + +module.exports = createECDH || require('./browser'); +},{"./browser":92,"crypto":27}],94:[function(require,module,exports){ +arguments[4][48][0].apply(exports,arguments) +},{"dup":48}],95:[function(require,module,exports){ arguments[4][50][0].apply(exports,arguments) -},{"../../elliptic":94,"bn.js":93,"dup":50}],96:[function(require,module,exports){ +},{"../package.json":114,"./elliptic/curve":98,"./elliptic/curves":101,"./elliptic/ec":102,"./elliptic/hmac-drbg":105,"./elliptic/utils":106,"brorand":107,"dup":50}],96:[function(require,module,exports){ arguments[4][51][0].apply(exports,arguments) -},{"../../elliptic":94,"../curve":97,"bn.js":93,"dup":51,"inherits":164}],97:[function(require,module,exports){ +},{"../../elliptic":95,"bn.js":94,"dup":51}],97:[function(require,module,exports){ arguments[4][52][0].apply(exports,arguments) -},{"./base":95,"./edwards":96,"./mont":98,"./short":99,"dup":52}],98:[function(require,module,exports){ +},{"../../elliptic":95,"../curve":98,"bn.js":94,"dup":52,"inherits":163}],98:[function(require,module,exports){ arguments[4][53][0].apply(exports,arguments) -},{"../../elliptic":94,"../curve":97,"bn.js":93,"dup":53,"inherits":164}],99:[function(require,module,exports){ +},{"./base":96,"./edwards":97,"./mont":99,"./short":100,"dup":53}],99:[function(require,module,exports){ arguments[4][54][0].apply(exports,arguments) -},{"../../elliptic":94,"../curve":97,"bn.js":93,"dup":54,"inherits":164}],100:[function(require,module,exports){ +},{"../../elliptic":95,"../curve":98,"bn.js":94,"dup":54,"inherits":163}],100:[function(require,module,exports){ arguments[4][55][0].apply(exports,arguments) -},{"../elliptic":94,"bn.js":93,"dup":55,"hash.js":107}],101:[function(require,module,exports){ +},{"../../elliptic":95,"../curve":98,"bn.js":94,"dup":55,"inherits":163}],101:[function(require,module,exports){ arguments[4][56][0].apply(exports,arguments) -},{"../../elliptic":94,"./key":102,"./signature":103,"bn.js":93,"dup":56}],102:[function(require,module,exports){ +},{"../elliptic":95,"bn.js":94,"dup":56,"hash.js":108}],102:[function(require,module,exports){ arguments[4][57][0].apply(exports,arguments) -},{"../../elliptic":94,"bn.js":93,"dup":57}],103:[function(require,module,exports){ +},{"../../elliptic":95,"./key":103,"./signature":104,"bn.js":94,"dup":57}],103:[function(require,module,exports){ arguments[4][58][0].apply(exports,arguments) -},{"../../elliptic":94,"bn.js":93,"dup":58}],104:[function(require,module,exports){ +},{"../../elliptic":95,"bn.js":94,"dup":58}],104:[function(require,module,exports){ arguments[4][59][0].apply(exports,arguments) -},{"../elliptic":94,"dup":59,"hash.js":107}],105:[function(require,module,exports){ +},{"../../elliptic":95,"bn.js":94,"dup":59}],105:[function(require,module,exports){ arguments[4][60][0].apply(exports,arguments) -},{"bn.js":93,"dup":60}],106:[function(require,module,exports){ +},{"../elliptic":95,"dup":60,"hash.js":108}],106:[function(require,module,exports){ arguments[4][61][0].apply(exports,arguments) -},{"dup":61}],107:[function(require,module,exports){ +},{"bn.js":94,"dup":61}],107:[function(require,module,exports){ arguments[4][62][0].apply(exports,arguments) -},{"./hash/common":108,"./hash/hmac":109,"./hash/ripemd":110,"./hash/sha":111,"./hash/utils":112,"dup":62}],108:[function(require,module,exports){ +},{"dup":62}],108:[function(require,module,exports){ arguments[4][63][0].apply(exports,arguments) -},{"../hash":107,"dup":63}],109:[function(require,module,exports){ +},{"./hash/common":109,"./hash/hmac":110,"./hash/ripemd":111,"./hash/sha":112,"./hash/utils":113,"dup":63}],109:[function(require,module,exports){ arguments[4][64][0].apply(exports,arguments) -},{"../hash":107,"dup":64}],110:[function(require,module,exports){ +},{"../hash":108,"dup":64}],110:[function(require,module,exports){ arguments[4][65][0].apply(exports,arguments) -},{"../hash":107,"dup":65}],111:[function(require,module,exports){ +},{"../hash":108,"dup":65}],111:[function(require,module,exports){ arguments[4][66][0].apply(exports,arguments) -},{"../hash":107,"dup":66}],112:[function(require,module,exports){ +},{"../hash":108,"dup":66}],112:[function(require,module,exports){ arguments[4][67][0].apply(exports,arguments) -},{"dup":67,"inherits":164}],113:[function(require,module,exports){ +},{"../hash":108,"dup":67}],113:[function(require,module,exports){ arguments[4][68][0].apply(exports,arguments) -},{"dup":68}],114:[function(require,module,exports){ +},{"dup":68,"inherits":163}],114:[function(require,module,exports){ +arguments[4][69][0].apply(exports,arguments) +},{"dup":69}],115:[function(require,module,exports){ (function (Buffer){ 'use strict'; var createHash = require('sha.js') @@ -17999,7 +18198,7 @@ Hash.prototype.digest = function (enc) { return outData } }).call(this,require("buffer").Buffer) -},{"./md5":116,"buffer":22,"inherits":164,"ripemd160":117,"sha.js":119,"stream":179}],115:[function(require,module,exports){ +},{"./md5":117,"buffer":23,"inherits":163,"ripemd160":118,"sha.js":120,"stream":178}],116:[function(require,module,exports){ (function (Buffer){ 'use strict'; var intSize = 4; @@ -18036,7 +18235,7 @@ function hash(buf, fn, hashSize, bigEndian) { } exports.hash = hash; }).call(this,require("buffer").Buffer) -},{"buffer":22}],116:[function(require,module,exports){ +},{"buffer":23}],117:[function(require,module,exports){ 'use strict'; /* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message @@ -18193,7 +18392,7 @@ function bit_rol(num, cnt) module.exports = function md5(buf) { return helpers.hash(buf, core_md5, 16); }; -},{"./helpers":115}],117:[function(require,module,exports){ +},{"./helpers":116}],118:[function(require,module,exports){ (function (Buffer){ /* CryptoJS v3.1.2 @@ -18403,7 +18602,7 @@ function ripemd160(message) { module.exports = ripemd160 }).call(this,require("buffer").Buffer) -},{"buffer":22}],118:[function(require,module,exports){ +},{"buffer":23}],119:[function(require,module,exports){ (function (Buffer){ //prototype class for hash functions function Hash (blockSize, finalSize) { @@ -18476,7 +18675,7 @@ Hash.prototype._update = function () { module.exports = Hash }).call(this,require("buffer").Buffer) -},{"buffer":22}],119:[function(require,module,exports){ +},{"buffer":23}],120:[function(require,module,exports){ var exports = module.exports = function (alg) { var Alg = exports[alg.toLowerCase()] if(!Alg) throw new Error(alg + ' is not supported (we accept pull requests)') @@ -18490,7 +18689,7 @@ exports.sha256 = require('./sha256') exports.sha384 = require('./sha384') exports.sha512 = require('./sha512') -},{"./sha1":120,"./sha224":121,"./sha256":122,"./sha384":123,"./sha512":124}],120:[function(require,module,exports){ +},{"./sha1":121,"./sha224":122,"./sha256":123,"./sha384":124,"./sha512":125}],121:[function(require,module,exports){ (function (Buffer){ /* * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined @@ -18590,7 +18789,7 @@ module.exports = Sha1 }).call(this,require("buffer").Buffer) -},{"./hash":118,"buffer":22,"inherits":164}],121:[function(require,module,exports){ +},{"./hash":119,"buffer":23,"inherits":163}],122:[function(require,module,exports){ (function (Buffer){ /** * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined @@ -18646,7 +18845,7 @@ Sha224.prototype._hash = function () { module.exports = Sha224 }).call(this,require("buffer").Buffer) -},{"./hash":118,"./sha256":122,"buffer":22,"inherits":164}],122:[function(require,module,exports){ +},{"./hash":119,"./sha256":123,"buffer":23,"inherits":163}],123:[function(require,module,exports){ (function (Buffer){ /** * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined @@ -18799,7 +18998,7 @@ Sha256.prototype._hash = function () { module.exports = Sha256 }).call(this,require("buffer").Buffer) -},{"./hash":118,"buffer":22,"inherits":164}],123:[function(require,module,exports){ +},{"./hash":119,"buffer":23,"inherits":163}],124:[function(require,module,exports){ (function (Buffer){ var inherits = require('inherits') var SHA512 = require('./sha512'); @@ -18859,7 +19058,7 @@ Sha384.prototype._hash = function () { module.exports = Sha384 }).call(this,require("buffer").Buffer) -},{"./hash":118,"./sha512":124,"buffer":22,"inherits":164}],124:[function(require,module,exports){ +},{"./hash":119,"./sha512":125,"buffer":23,"inherits":163}],125:[function(require,module,exports){ (function (Buffer){ var inherits = require('inherits') var Hash = require('./hash') @@ -19108,7 +19307,7 @@ Sha512.prototype._hash = function () { module.exports = Sha512 }).call(this,require("buffer").Buffer) -},{"./hash":118,"buffer":22,"inherits":164}],125:[function(require,module,exports){ +},{"./hash":119,"buffer":23,"inherits":163}],126:[function(require,module,exports){ (function (Buffer){ 'use strict'; var createHash = require('create-hash/browser'); @@ -19152,15 +19351,12 @@ function Hmac(alg, key) { inherits(Hmac, Transform) Hmac.prototype.update = function (data, enc) { - if (typeof data === 'string') { - data = new Buffer(data, enc) - } + this._hash.update(data, enc) - this._hash.update(data) return this } -Hmac.prototype._transform = function (data, enc, next) { +Hmac.prototype._transform = function (data, _, next) { this._hash.update(data) next() @@ -19174,9 +19370,8 @@ Hmac.prototype._flush = function (next) { Hmac.prototype.digest = function (enc) { var h = this._hash.digest() - var outData = createHash(this._alg).update(this._opad).update(h).digest() - return enc ? outData.toString() : outData + return createHash(this._alg).update(this._opad).update(h).digest(enc) } module.exports = function createHmac(alg, key) { @@ -19184,7 +19379,51 @@ module.exports = function createHmac(alg, key) { } }).call(this,require("buffer").Buffer) -},{"buffer":22,"create-hash/browser":114,"inherits":164,"stream":179}],126:[function(require,module,exports){ +},{"buffer":23,"create-hash/browser":115,"inherits":163,"stream":178}],127:[function(require,module,exports){ +(function (Buffer){ +var generatePrime = require('./lib/generatePrime'); +var primes = require('./lib/primes'); + +var DH = require('./lib/dh'); + +function getDiffieHellman(mod) { + var prime = new Buffer(primes[mod].prime, 'hex'); + var gen = new Buffer(primes[mod].gen, 'hex'); + + return new DH(prime, gen); +} + +function createDiffieHellman(prime, enc, generator, genc) { + if (Buffer.isBuffer(enc) || (typeof enc === 'string' && ['hex', 'binary', 'base64'].indexOf(enc) === -1)) { + genc = generator; + generator = enc; + enc = undefined; + } + + enc = enc || 'binary'; + genc = genc || 'binary'; + generator = generator || new Buffer([2]); + + if (!Buffer.isBuffer(generator)) { + generator = new Buffer(generator, genc); + } + + if (typeof prime === 'number') { + return new DH(generatePrime(prime, generator), generator, true); + } + + if (!Buffer.isBuffer(prime)) { + prime = new Buffer(prime, enc); + } + + return new DH(prime, generator, true); +} + +exports.DiffieHellmanGroup = exports.createDiffieHellmanGroup = exports.getDiffieHellman = getDiffieHellman; +exports.createDiffieHellman = exports.DiffieHellman = createDiffieHellman; + +}).call(this,require("buffer").Buffer) +},{"./lib/dh":128,"./lib/generatePrime":129,"./lib/primes":130,"buffer":23}],128:[function(require,module,exports){ (function (Buffer){ var BN = require('bn.js'); var MillerRabin = require('miller-rabin'); @@ -19195,157 +19434,167 @@ var TEN = new BN(10); var THREE = new BN(3); var SEVEN = new BN(7); var primes = require('./generatePrime'); +var randomBytes = require('randombytes'); module.exports = DH; + function setPublicKey(pub, enc) { - enc = enc || 'utf8'; - if (!Buffer.isBuffer(pub)) { - pub = new Buffer(pub, enc); - } - this._pub = new BN(pub); + enc = enc || 'utf8'; + if (!Buffer.isBuffer(pub)) { + pub = new Buffer(pub, enc); + } + this._pub = new BN(pub); + return this; } + function setPrivateKey(priv, enc) { - enc = enc || 'utf8'; - if (!Buffer.isBuffer(priv)) { - priv = new Buffer(priv, enc); - } - this._priv = new BN(priv); + enc = enc || 'utf8'; + if (!Buffer.isBuffer(priv)) { + priv = new Buffer(priv, enc); + } + this._priv = new BN(priv); + return this; } + var primeCache = {}; function checkPrime(prime, generator) { - var gen = generator.toString('hex'); - var hex = [gen, prime.toString(16)].join('_'); - if (hex in primeCache) { - return primeCache[hex]; - } - var error = 0; - - if (prime.isEven() || - !primes.simpleSieve || - !primes.fermatTest(prime) || - !millerRabin.test(prime)) { - //not a prime so +1 - error += 1; - - if (gen === '02' || gen === '05') { - // we'd be able to check the generator - // it would fail so +8 - error += 8; - } else { - //we wouldn't be able to test the generator - // so +4 - error += 4; - } - primeCache[hex] = error; - return error; - } - if (!millerRabin.test(prime.shrn(1))) { - //not a safe prime - error += 2; - } - var gen = generator.toString('hex'); - var rem; - switch (gen) { - case '02': - if (prime.mod(TWENTYFOUR).cmp(ELEVEN)) { - // unsuidable generator - error += 8; - } - break; - case '05': - rem = prime.mod(TEN); - if (rem.cmp(THREE) && rem.cmp(SEVEN)) { - // prime mod 10 needs to equal 3 or 7 - error += 8; - } - break; - default: - error += 4; - } - primeCache[hex] = error; - return error; + var gen = generator.toString('hex'); + var hex = [gen, prime.toString(16)].join('_'); + if (hex in primeCache) { + return primeCache[hex]; + } + var error = 0; + + if (prime.isEven() || + !primes.simpleSieve || + !primes.fermatTest(prime) || + !millerRabin.test(prime)) { + //not a prime so +1 + error += 1; + + if (gen === '02' || gen === '05') { + // we'd be able to check the generator + // it would fail so +8 + error += 8; + } else { + //we wouldn't be able to test the generator + // so +4 + error += 4; + } + primeCache[hex] = error; + return error; + } + if (!millerRabin.test(prime.shrn(1))) { + //not a safe prime + error += 2; + } + var rem; + switch (gen) { + case '02': + if (prime.mod(TWENTYFOUR).cmp(ELEVEN)) { + // unsuidable generator + error += 8; + } + break; + case '05': + rem = prime.mod(TEN); + if (rem.cmp(THREE) && rem.cmp(SEVEN)) { + // prime mod 10 needs to equal 3 or 7 + error += 8; + } + break; + default: + error += 4; + } + primeCache[hex] = error; + return error; } + function defineError (self, error) { - try { - Object.defineProperty(self, 'verifyError', { - enumerable: true, - value: error, - writable: false - }); - } catch(e) { - self.verifyError = error; - } + try { + Object.defineProperty(self, 'verifyError', { + enumerable: true, + value: error, + writable: false + }); + } catch(e) { + self.verifyError = error; + } } -function DH(prime, generator,crypto, malleable) { - this.setGenerator(generator); - this.__prime = new BN(prime); - this._prime = BN.mont(this.__prime); - this._pub = void 0; - this._priv = void 0; - - if (malleable) { - this.setPublicKey = setPublicKey; - this.setPrivateKey = setPrivateKey; - defineError(this, checkPrime(this.__prime, generator)); - } else { - defineError(this, 8); - } - this._makeNum = function makeNum() { - return crypto.randomBytes(prime.length); - }; +function DH(prime, generator, malleable) { + this.setGenerator(generator); + this.__prime = new BN(prime); + this._prime = BN.mont(this.__prime); + this._primeLen = prime.length; + this._pub = void 0; + this._priv = void 0; + + if (malleable) { + this.setPublicKey = setPublicKey; + this.setPrivateKey = setPrivateKey; + defineError(this, checkPrime(this.__prime, generator)); + } else { + defineError(this, 8); + } } + DH.prototype.generateKeys = function () { - if (!this._priv) { - this._priv = new BN(this._makeNum()); - } - this._pub = this._gen.toRed(this._prime).redPow(this._priv).fromRed(); - return this.getPublicKey(); + if (!this._priv) { + this._priv = new BN(randomBytes(this._primeLen)); + } + this._pub = this._gen.toRed(this._prime).redPow(this._priv).fromRed(); + return this.getPublicKey(); }; DH.prototype.computeSecret = function (other) { - other = new BN(other); - other = other.toRed(this._prime); - var secret = other.redPow(this._priv).fromRed(); - var out = new Buffer(secret.toArray()); - var prime = this.getPrime(); - if (out.length < prime.length) { - var front = new Buffer(prime.length - out.length); - front.fill(0); - out = Buffer.concat([front, out]); - } - return out; + other = new BN(other); + other = other.toRed(this._prime); + var secret = other.redPow(this._priv).fromRed(); + var out = new Buffer(secret.toArray()); + var prime = this.getPrime(); + if (out.length < prime.length) { + var front = new Buffer(prime.length - out.length); + front.fill(0); + out = Buffer.concat([front, out]); + } + return out; }; + DH.prototype.getPublicKey = function getPublicKey(enc) { - return returnValue(this._pub, enc); + return formatReturnValue(this._pub, enc); }; + DH.prototype.getPrivateKey = function getPrivateKey(enc) { - return returnValue(this._priv, enc); + return formatReturnValue(this._priv, enc); }; DH.prototype.getPrime = function (enc) { - return returnValue(this.__prime, enc); + return formatReturnValue(this.__prime, enc); }; + DH.prototype.getGenerator = function (enc) { - return returnValue(this._gen, enc); + return formatReturnValue(this._gen, enc); }; + DH.prototype.setGenerator = function (gen, enc) { - enc = enc || 'utf8'; - if (!Buffer.isBuffer(gen)) { - gen = new Buffer(gen, enc); - } - this._gen = new BN(gen); + enc = enc || 'utf8'; + if (!Buffer.isBuffer(gen)) { + gen = new Buffer(gen, enc); + } + this._gen = new BN(gen); + return this; }; -function returnValue(bn, enc) { - var buf = new Buffer(bn.toArray()); - if (!enc) { - return buf; - } else { - return buf.toString(enc); - } +function formatReturnValue(bn, enc) { + var buf = new Buffer(bn.toArray()); + if (!enc) { + return buf; + } else { + return buf.toString(enc); + } } }).call(this,require("buffer").Buffer) -},{"./generatePrime":127,"bn.js":129,"buffer":22,"miller-rabin":130}],127:[function(require,module,exports){ - +},{"./generatePrime":129,"bn.js":131,"buffer":23,"miller-rabin":132,"randombytes":161}],129:[function(require,module,exports){ +var randomBytes = require('randombytes'); module.exports = findPrime; findPrime.simpleSieve = simpleSieve; findPrime.fermatTest = fermatTest; @@ -19387,6 +19636,7 @@ function _getPrimes() { primes = res; return res; } + function simpleSieve(p) { var primes = _getPrimes(); @@ -19401,11 +19651,13 @@ function simpleSieve(p) { return true; } + function fermatTest(p) { var red = BN.mont(p); return TWO.toRed(red).redPow(p.subn(1)).fromRed().cmpn(1) === 0; } -function findPrime(bits, gen ,crypto) { + +function findPrime(bits, gen) { if (bits < 16) { // this is what openssl does if (gen === 2 || gen === 5) { @@ -19418,7 +19670,7 @@ function findPrime(bits, gen ,crypto) { var runs, comp; function generateRandom(bits) { runs = -1; - var out = new BN(crypto.randomBytes(Math.ceil(bits / 8))); + var out = new BN(randomBytes(Math.ceil(bits / 8))); while (out.bitLength() > bits) { out.ishrn(1); } @@ -19456,8 +19708,6 @@ function findPrime(bits, gen ,crypto) { } var num = generateRandom(bits); - - var n2 = num.shrn(1); while (true) { @@ -19476,48 +19726,44 @@ function findPrime(bits, gen ,crypto) { } } -},{"bn.js":129,"miller-rabin":130}],128:[function(require,module,exports){ -(function (Buffer){ -var primes = require('./primes.json'); -var DH = require('./dh'); -var generatePrime = require('./generatePrime'); -module.exports = function (crypto, exports) { - exports.DiffieHellmanGroup = - exports.createDiffieHellmanGroup = - exports.getDiffieHellman = DiffieHellmanGroup; - function DiffieHellmanGroup(mod) { - return new DH(new Buffer(primes[mod].prime, 'hex'), - new Buffer(primes[mod].gen, 'hex'), crypto); - } - exports.createDiffieHellman = exports.DiffieHellman = DiffieHellman; - function DiffieHellman(prime, enc, generator, genc) { - - if (Buffer.isBuffer(enc) || - (typeof enc === 'string' && ['hex', 'binary', 'base64'].indexOf(enc) === -1)) { - genc = generator; - generator = enc - enc = void 0; - } - enc = enc || 'binary'; - genc = genc || 'binary'; - generator = generator || new Buffer([2]); - if (!Buffer.isBuffer(generator)) { - generator = new Buffer(generator, genc); - } - if (typeof prime === 'number') { - return new DH(generatePrime(prime, generator, crypto), generator, crypto, true); - } - if (!Buffer.isBuffer(prime)) { - prime = new Buffer(prime, enc); - } - - return new DH(prime, generator, crypto, true); - }; +},{"bn.js":131,"miller-rabin":132,"randombytes":161}],130:[function(require,module,exports){ +module.exports={ + "modp1": { + "gen": "02", + "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a3620ffffffffffffffff" + }, + "modp2": { + "gen": "02", + "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece65381ffffffffffffffff" + }, + "modp5": { + "gen": "02", + "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca237327ffffffffffffffff" + }, + "modp14": { + "gen": "02", + "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aacaa68ffffffffffffffff" + }, + "modp15": { + "gen": "02", + "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a93ad2caffffffffffffffff" + }, + "modp16": { + "gen": "02", + "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c934063199ffffffffffffffff" + }, + "modp17": { + "gen": "02", + "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c93402849236c3fab4d27c7026c1d4dcb2602646dec9751e763dba37bdf8ff9406ad9e530ee5db382f413001aeb06a53ed9027d831179727b0865a8918da3edbebcf9b14ed44ce6cbaced4bb1bdb7f1447e6cc254b332051512bd7af426fb8f401378cd2bf5983ca01c64b92ecf032ea15d1721d03f482d7ce6e74fef6d55e702f46980c82b5a84031900b1c9e59e7c97fbec7e8f323a97a7e36cc88be0f1d45b7ff585ac54bd407b22b4154aacc8f6d7ebf48e1d814cc5ed20f8037e0a79715eef29be32806a1d58bb7c5da76f550aa3d8a1fbff0eb19ccb1a313d55cda56c9ec2ef29632387fe8d76e3c0468043e8f663f4860ee12bf2d5b0b7474d6e694f91e6dcc4024ffffffffffffffff" + }, + "modp18": { + "gen": "02", + "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c93402849236c3fab4d27c7026c1d4dcb2602646dec9751e763dba37bdf8ff9406ad9e530ee5db382f413001aeb06a53ed9027d831179727b0865a8918da3edbebcf9b14ed44ce6cbaced4bb1bdb7f1447e6cc254b332051512bd7af426fb8f401378cd2bf5983ca01c64b92ecf032ea15d1721d03f482d7ce6e74fef6d55e702f46980c82b5a84031900b1c9e59e7c97fbec7e8f323a97a7e36cc88be0f1d45b7ff585ac54bd407b22b4154aacc8f6d7ebf48e1d814cc5ed20f8037e0a79715eef29be32806a1d58bb7c5da76f550aa3d8a1fbff0eb19ccb1a313d55cda56c9ec2ef29632387fe8d76e3c0468043e8f663f4860ee12bf2d5b0b7474d6e694f91e6dbe115974a3926f12fee5e438777cb6a932df8cd8bec4d073b931ba3bc832b68d9dd300741fa7bf8afc47ed2576f6936ba424663aab639c5ae4f5683423b4742bf1c978238f16cbe39d652de3fdb8befc848ad922222e04a4037c0713eb57a81a23f0c73473fc646cea306b4bcbc8862f8385ddfa9d4b7fa2c087e879683303ed5bdd3a062b3cf5b3a278a66d2a13f83f44f82ddf310ee074ab6a364597e899a0255dc164f31cc50846851df9ab48195ded7ea1b1d510bd7ee74d73faf36bc31ecfa268359046f4eb879f924009438b481c6cd7889a002ed5ee382bc9190da6fc026e479558e4475677e9aa9e3050e2765694dfc81f56e880b96e7160c980dd98edd3dfffffffffffffffff" + } } -}).call(this,require("buffer").Buffer) -},{"./dh":126,"./generatePrime":127,"./primes.json":132,"buffer":22}],129:[function(require,module,exports){ -arguments[4][47][0].apply(exports,arguments) -},{"dup":47}],130:[function(require,module,exports){ +},{}],131:[function(require,module,exports){ +arguments[4][48][0].apply(exports,arguments) +},{"dup":48}],132:[function(require,module,exports){ var bn = require('bn.js'); var brorand = require('brorand'); @@ -19633,242 +19879,561 @@ MillerRabin.prototype.getDivisor = function getDivisor(n, k) { return prime; }; -},{"bn.js":129,"brorand":131}],131:[function(require,module,exports){ -arguments[4][61][0].apply(exports,arguments) -},{"dup":61}],132:[function(require,module,exports){ -module.exports={ - "modp1": { - "gen": "02", - "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a3620ffffffffffffffff" - }, - "modp2": { - "gen": "02", - "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece65381ffffffffffffffff" - }, - "modp5": { - "gen": "02", - "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca237327ffffffffffffffff" - }, - "modp14": { - "gen": "02", - "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aacaa68ffffffffffffffff" - }, - "modp15": { - "gen": "02", - "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a93ad2caffffffffffffffff" - }, - "modp16": { - "gen": "02", - "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c934063199ffffffffffffffff" - }, - "modp17": { - "gen": "02", - "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c93402849236c3fab4d27c7026c1d4dcb2602646dec9751e763dba37bdf8ff9406ad9e530ee5db382f413001aeb06a53ed9027d831179727b0865a8918da3edbebcf9b14ed44ce6cbaced4bb1bdb7f1447e6cc254b332051512bd7af426fb8f401378cd2bf5983ca01c64b92ecf032ea15d1721d03f482d7ce6e74fef6d55e702f46980c82b5a84031900b1c9e59e7c97fbec7e8f323a97a7e36cc88be0f1d45b7ff585ac54bd407b22b4154aacc8f6d7ebf48e1d814cc5ed20f8037e0a79715eef29be32806a1d58bb7c5da76f550aa3d8a1fbff0eb19ccb1a313d55cda56c9ec2ef29632387fe8d76e3c0468043e8f663f4860ee12bf2d5b0b7474d6e694f91e6dcc4024ffffffffffffffff" - }, - "modp18": { - "gen": "02", - "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c93402849236c3fab4d27c7026c1d4dcb2602646dec9751e763dba37bdf8ff9406ad9e530ee5db382f413001aeb06a53ed9027d831179727b0865a8918da3edbebcf9b14ed44ce6cbaced4bb1bdb7f1447e6cc254b332051512bd7af426fb8f401378cd2bf5983ca01c64b92ecf032ea15d1721d03f482d7ce6e74fef6d55e702f46980c82b5a84031900b1c9e59e7c97fbec7e8f323a97a7e36cc88be0f1d45b7ff585ac54bd407b22b4154aacc8f6d7ebf48e1d814cc5ed20f8037e0a79715eef29be32806a1d58bb7c5da76f550aa3d8a1fbff0eb19ccb1a313d55cda56c9ec2ef29632387fe8d76e3c0468043e8f663f4860ee12bf2d5b0b7474d6e694f91e6dbe115974a3926f12fee5e438777cb6a932df8cd8bec4d073b931ba3bc832b68d9dd300741fa7bf8afc47ed2576f6936ba424663aab639c5ae4f5683423b4742bf1c978238f16cbe39d652de3fdb8befc848ad922222e04a4037c0713eb57a81a23f0c73473fc646cea306b4bcbc8862f8385ddfa9d4b7fa2c087e879683303ed5bdd3a062b3cf5b3a278a66d2a13f83f44f82ddf310ee074ab6a364597e899a0255dc164f31cc50846851df9ab48195ded7ea1b1d510bd7ee74d73faf36bc31ecfa268359046f4eb879f924009438b481c6cd7889a002ed5ee382bc9190da6fc026e479558e4475677e9aa9e3050e2765694dfc81f56e880b96e7160c980dd98edd3dfffffffffffffffff" - } -} -},{}],133:[function(require,module,exports){ +},{"bn.js":131,"brorand":133}],133:[function(require,module,exports){ +arguments[4][62][0].apply(exports,arguments) +},{"dup":62}],134:[function(require,module,exports){ (function (Buffer){ -module.exports = function(crypto) { - function pbkdf2(password, salt, iterations, keylen, digest, callback) { - if ('function' === typeof digest) { - callback = digest - digest = undefined +var createHmac = require('create-hmac') + +exports.pbkdf2 = pbkdf2 +function pbkdf2 (password, salt, iterations, keylen, digest, callback) { + if (typeof digest === 'function') { + callback = digest + digest = undefined + } + + if (typeof callback !== 'function') { + throw new Error('No callback provided to pbkdf2') + } + + var result = pbkdf2Sync(password, salt, iterations, keylen, digest) + setTimeout(function () { + callback(undefined, result) + }) +} + +exports.pbkdf2Sync = pbkdf2Sync +function pbkdf2Sync (password, salt, iterations, keylen, digest) { + if (typeof iterations !== 'number') + throw new TypeError('Iterations not a number') + + if (iterations < 0) + throw new TypeError('Bad iterations') + + if (typeof keylen !== 'number') + throw new TypeError('Key length not a number') + + if (keylen < 0) + throw new TypeError('Bad key length') + + digest = digest || 'sha1' + + if (!Buffer.isBuffer(password)) password = new Buffer(password) + if (!Buffer.isBuffer(salt)) salt = new Buffer(salt) + + var hLen + var l = 1 + var DK = new Buffer(keylen) + var block1 = new Buffer(salt.length + 4) + salt.copy(block1, 0, 0, salt.length) + + var r + var T + + for (var i = 1; i <= l; i++) { + block1.writeUInt32BE(i, salt.length) + var U = createHmac(digest, password).update(block1).digest() + + if (!hLen) { + hLen = U.length + T = new Buffer(hLen) + l = Math.ceil(keylen / hLen) + r = keylen - (l - 1) * hLen + + if (keylen > (Math.pow(2, 32) - 1) * hLen) + throw new TypeError('keylen exceeds maximum length') } - if ('function' !== typeof callback) - throw new Error('No callback provided to pbkdf2') + U.copy(T, 0, 0, hLen) - setTimeout(function() { - var result + for (var j = 1; j < iterations; j++) { + U = createHmac(digest, password).update(U).digest() - try { - result = pbkdf2Sync(password, salt, iterations, keylen, digest) - } catch (e) { - return callback(e) + for (var k = 0; k < hLen; k++) { + T[k] ^= U[k] } + } - callback(undefined, result) - }) + var destPos = (i - 1) * hLen + var len = (i === l ? r : hLen) + T.copy(DK, destPos, 0, len) } - function pbkdf2Sync(password, salt, iterations, keylen, digest) { - if ('number' !== typeof iterations) - throw new TypeError('Iterations not a number') + return DK +} - if (iterations < 0) - throw new TypeError('Bad iterations') +}).call(this,require("buffer").Buffer) +},{"buffer":23,"create-hmac":126}],135:[function(require,module,exports){ +exports.publicEncrypt = require('./publicEncrypt'); +exports.privateDecrypt = require('./privateDecrypt'); - if ('number' !== typeof keylen) - throw new TypeError('Key length not a number') +exports.privateEncrypt = function privateEncrypt(key, buf) { + return exports.publicEncrypt(key, buf, true); +}; - if (keylen < 0) - throw new TypeError('Bad key length') +exports.publicDecrypt = function publicDecrypt(key, buf) { + return exports.privateDecrypt(key, buf, true); +}; +},{"./privateDecrypt":157,"./publicEncrypt":158}],136:[function(require,module,exports){ +(function (Buffer){ +var createHash = require('create-hash'); +module.exports = function (seed, len) { + var t = new Buffer(''); + var i = 0, c; + while (t.length < len) { + c = i2ops(i++); + t = Buffer.concat([t, createHash('sha1').update(seed).update(c).digest()]); + } + return t.slice(0, len); +}; - digest = digest || 'sha1' +function i2ops(c) { + var out = new Buffer(4); + out.writeUInt32BE(c,0); + return out; +} +}).call(this,require("buffer").Buffer) +},{"buffer":23,"create-hash":115}],137:[function(require,module,exports){ +arguments[4][48][0].apply(exports,arguments) +},{"dup":48}],138:[function(require,module,exports){ +(function (Buffer){ +var bn = require('bn.js'); +var randomBytes = require('randombytes'); +module.exports = crt; +function blind(priv) { + var r = getr(priv); + var blinder = r.toRed(bn.mont(priv.modulus)) + .redPow(new bn(priv.publicExponent)).fromRed(); + return { + blinder: blinder, + unblinder:r.invm(priv.modulus) + }; +} +function crt(msg, priv) { + var blinds = blind(priv); + var len = priv.modulus.byteLength(); + var mod = bn.mont(priv.modulus); + var blinded = new bn(msg).mul(blinds.blinder).mod(priv.modulus); + var c1 = blinded.toRed(bn.mont(priv.prime1)); + var c2 = blinded.toRed(bn.mont(priv.prime2)); + var qinv = priv.coefficient; + var p = priv.prime1; + var q = priv.prime2; + var m1 = c1.redPow(priv.exponent1); + var m2 = c2.redPow(priv.exponent2); + m1 = m1.fromRed(); + m2 = m2.fromRed(); + var h = m1.isub(m2).imul(qinv).mod(p); + h.imul(q); + m2.iadd(h); + var out = new Buffer(m2.imul(blinds.unblinder).mod(priv.modulus).toArray()); + if (out.length < len) { + var prefix = new Buffer(len - out.length); + prefix.fill(0); + out = Buffer.concat([prefix, out], len); + } + return out; +} +crt.getr = getr; +function getr(priv) { + var len = priv.modulus.byteLength(); + var r = new bn(randomBytes(len)); + while (r.cmp(priv.modulus) >= 0 || !r.mod(priv.prime1) || !r.mod(priv.prime2)) { + r = new bn(randomBytes(len)); + } + return r; +} +}).call(this,require("buffer").Buffer) +},{"bn.js":137,"buffer":23,"randombytes":161}],139:[function(require,module,exports){ +(function (Buffer){ +var createHash = require('create-hash'); +module.exports = function evp(password, salt, keyLen) { + keyLen = keyLen/8; + var ki = 0; + var ii = 0; + var key = new Buffer(keyLen); + var addmd = 0; + var md, md_buf; + var i; + while (true) { + md = createHash('md5'); + if(addmd++ > 0) { + md.update(md_buf); + } + md.update(password); + md.update(salt); + md_buf = md.digest(); + i = 0; + if(keyLen > 0) { + while(true) { + if(keyLen === 0) { + break; + } + if(i === md_buf.length) { + break; + } + key[ki++] = md_buf[i++]; + keyLen--; + } + } + if(keyLen === 0) { + break; + } + } + for(i=0;i (Math.pow(2, 32) - 1) * hLen) - throw new TypeError('keylen exceeds maximum length') - } +exports.EncryptedPrivateKey = EncryptedPrivateKeyInfo; - U.copy(T, 0, 0, hLen) +var DSAPrivateKey = asn1.define('DSAPrivateKey', function() { + this.seq().obj( + this.key('version').int(), + this.key('p').int(), + this.key('q').int(), + this.key('g').int(), + this.key('pub_key').int(), + this.key('priv_key').int() + ); +}); +exports.DSAPrivateKey = DSAPrivateKey; - for (var j = 1; j < iterations; j++) { - U = crypto.createHmac(digest, password).update(U).digest() +exports.DSAparam = asn1.define('DSAparam', function () { + this.int(); +}); +var ECPrivateKey = asn1.define('ECPrivateKey', function() { + this.seq().obj( + this.key('version').int(), + this.key('privateKey').octstr(), + this.key('parameters').optional().explicit(0).use(ECParameters), + this.key('publicKey').optional().explicit(1).bitstr() + ); +}); +exports.ECPrivateKey = ECPrivateKey; +var ECParameters = asn1.define('ECParameters', function() { + this.choice({ + namedCurve: this.objid() + }); +}); - for (var k = 0; k < hLen; k++) { - T[k] ^= U[k] - } - } +exports.signature = asn1.define('signature', function() { + this.seq().obj( + this.key('r').int(), + this.key('s').int() + ); +}); - var destPos = (i - 1) * hLen - var len = (i == l ? r : hLen) - T.copy(DK, destPos, 0, len) +},{"asn1.js":144}],142:[function(require,module,exports){ +(function (Buffer){ +// adapted from https://github.com/apatil/pemstrip +var findProc = /Proc-Type: 4,ENCRYPTED\n\r?DEK-Info: AES-((?:128)|(?:192)|(?:256))-CBC,([0-9A-H]+)\n\r?\n\r?([0-9A-z\n\r\+\/\=]+)\n\r?/m; +var startRegex =/^-----BEGIN (.*) KEY-----\n/m; +var fullRegex = /^-----BEGIN (.*) KEY-----\n\r?([0-9A-z\n\r\+\/\=]+)\n\r?-----END \1 KEY-----$/m; +var evp = require('./EVP_BytesToKey'); +var ciphers = require('browserify-aes'); +module.exports = function (okey, password) { + var key = okey.toString(); + var match = key.match(findProc); + var decrypted; + if (!match) { + var match2 = key.match(fullRegex); + decrypted = new Buffer(match2[2].replace(/\n\r?/g, ''), 'base64'); + } else { + var suite = 'aes' + match[1]; + var iv = new Buffer(match[2], 'hex'); + var cipherText = new Buffer(match[3].replace(/\n\r?/g, ''), 'base64'); + var cipherKey = evp(password, iv.slice(0,8), parseInt(match[1])); + var out = []; + var cipher = ciphers.createDecipheriv(suite, cipherKey, iv); + out.push(cipher.update(cipherText)); + out.push(cipher.final()); + decrypted = Buffer.concat(out); + } + var tag = key.match(startRegex)[1] + ' KEY'; + return { + tag: tag, + data: decrypted + }; +}; +// http://stackoverflow.com/a/7033705 +function wrap(str) { + var chunks = []; + while (str) { + if (str.length < 64) { + chunks.push(str); + break; + } + else { + chunks.push(str.slice(0, 64)); + str = str.slice(64); } + } + return chunks.join("\n"); +} +}).call(this,require("buffer").Buffer) +},{"./EVP_BytesToKey":139,"browserify-aes":31,"buffer":23}],143:[function(require,module,exports){ +(function (Buffer){ +var asn1 = require('./asn1'); +var aesid = require('./aesid.json'); +var fixProc = require('./fixProc'); +var ciphers = require('browserify-aes'); +var compat = require('pbkdf2-compat'); +module.exports = parseKeys; - return DK +function parseKeys(buffer) { + var password; + if (typeof buffer === 'object' && !Buffer.isBuffer(buffer)) { + password = buffer.passphrase; + buffer = buffer.key; + } + if (typeof buffer === 'string') { + buffer = new Buffer(buffer); } - return { - pbkdf2: pbkdf2, - pbkdf2Sync: pbkdf2Sync + var stripped = fixProc(buffer, password); + + var type = stripped.tag; + var data = stripped.data; + var subtype,ndata; + switch (type) { + case 'PUBLIC KEY': + ndata = asn1.PublicKey.decode(data, 'der'); + subtype = ndata.algorithm.algorithm.join('.'); + switch(subtype) { + case '1.2.840.113549.1.1.1': + return asn1.RSAPublicKey.decode(ndata.subjectPublicKey.data, 'der'); + case '1.2.840.10045.2.1': + ndata.subjectPrivateKey = ndata.subjectPublicKey; + return { + type: 'ec', + data: ndata + }; + case '1.2.840.10040.4.1': + ndata.algorithm.params.pub_key = asn1.DSAparam.decode(ndata.subjectPublicKey.data, 'der'); + return { + type: 'dsa', + data: ndata.algorithm.params + }; + default: throw new Error('unknown key id ' + subtype); + } + throw new Error('unknown key type ' + type); + case 'ENCRYPTED PRIVATE KEY': + data = asn1.EncryptedPrivateKey.decode(data, 'der'); + data = decrypt(data, password); + //falling through + case 'PRIVATE KEY': + ndata = asn1.PrivateKey.decode(data, 'der'); + subtype = ndata.algorithm.algorithm.join('.'); + switch(subtype) { + case '1.2.840.113549.1.1.1': + return asn1.RSAPrivateKey.decode(ndata.subjectPrivateKey, 'der'); + case '1.2.840.10045.2.1': + return { + curve: ndata.algorithm.curve, + privateKey: asn1.ECPrivateKey.decode(ndata.subjectPrivateKey, 'der').privateKey + }; + case '1.2.840.10040.4.1': + ndata.algorithm.params.priv_key = asn1.DSAparam.decode(ndata.subjectPrivateKey, 'der'); + return { + type: 'dsa', + params: ndata.algorithm.params + }; + default: throw new Error('unknown key id ' + subtype); + } + throw new Error('unknown key type ' + type); + case 'RSA PUBLIC KEY': + return asn1.RSAPublicKey.decode(data, 'der'); + case 'RSA PRIVATE KEY': + return asn1.RSAPrivateKey.decode(data, 'der'); + case 'DSA PRIVATE KEY': + return { + type: 'dsa', + params: asn1.DSAPrivateKey.decode(data, 'der') + }; + case 'EC PRIVATE KEY': + data = asn1.ECPrivateKey.decode(data, 'der'); + return { + curve: data.parameters.value, + privateKey: data.privateKey + }; + default: throw new Error('unknown key type ' + type); } } - -}).call(this,require("buffer").Buffer) -},{"buffer":22}],134:[function(require,module,exports){ - -module.exports = function (exports, crypto) { - exports.publicEncrypt = require('./publicEncrypt')(crypto); - exports.privateDecrypt = require('./privateDecrypt')(crypto); -}; -},{"./privateDecrypt":158,"./publicEncrypt":159}],135:[function(require,module,exports){ -(function (Buffer){ -module.exports = function (seed, len, crypto) { - var t = new Buffer(''); - var i = 0, c; - while (t.length < len) { - c = i2ops(i++); - t = Buffer.concat([t, crypto.createHash('sha1').update(seed).update(c).digest()]); - } - return t.slice(0, len); -}; - -function i2ops(c) { - var out = new Buffer(4); - out.writeUInt32BE(c,0); - return out; +parseKeys.signature = asn1.signature; +function decrypt(data, password) { + var salt = data.algorithm.decrypt.kde.kdeparams.salt; + var iters = data.algorithm.decrypt.kde.kdeparams.iters; + var algo = aesid[data.algorithm.decrypt.cipher.algo.join('.')]; + var iv = data.algorithm.decrypt.cipher.iv; + var cipherText = data.subjectPrivateKey; + var keylen = parseInt(algo.split('-')[1], 10)/8; + var key = compat.pbkdf2Sync(password, salt, iters, keylen); + var cipher = ciphers.createDecipheriv(algo, key, iv); + var out = []; + out.push(cipher.update(cipherText)); + out.push(cipher.final()); + return Buffer.concat(out); } }).call(this,require("buffer").Buffer) -},{"buffer":22}],136:[function(require,module,exports){ -arguments[4][47][0].apply(exports,arguments) -},{"dup":47}],137:[function(require,module,exports){ -arguments[4][48][0].apply(exports,arguments) -},{"bn.js":136,"buffer":22,"dup":48}],138:[function(require,module,exports){ -arguments[4][69][0].apply(exports,arguments) -},{"buffer":22,"dup":69}],139:[function(require,module,exports){ -arguments[4][70][0].apply(exports,arguments) -},{"dup":70}],140:[function(require,module,exports){ -arguments[4][71][0].apply(exports,arguments) -},{"asn1.js":144,"asn1.js-rfc3280":143,"dup":71}],141:[function(require,module,exports){ -arguments[4][72][0].apply(exports,arguments) -},{"./EVP_BytesToKey":138,"buffer":22,"dup":72}],142:[function(require,module,exports){ -arguments[4][73][0].apply(exports,arguments) -},{"./aesid.json":139,"./asn1":140,"./fixProc":141,"buffer":22,"dup":73,"pemstrip":157}],143:[function(require,module,exports){ -arguments[4][74][0].apply(exports,arguments) -},{"asn1.js":144,"dup":74}],144:[function(require,module,exports){ -arguments[4][75][0].apply(exports,arguments) -},{"./asn1/api":145,"./asn1/base":147,"./asn1/constants":151,"./asn1/decoders":153,"./asn1/encoders":155,"bn.js":136,"dup":75}],145:[function(require,module,exports){ +},{"./aesid.json":140,"./asn1":141,"./fixProc":142,"browserify-aes":31,"buffer":23,"pbkdf2-compat":134}],144:[function(require,module,exports){ arguments[4][76][0].apply(exports,arguments) -},{"../asn1":144,"dup":76,"inherits":164,"vm":183}],146:[function(require,module,exports){ +},{"./asn1/api":145,"./asn1/base":147,"./asn1/constants":151,"./asn1/decoders":153,"./asn1/encoders":155,"bn.js":137,"dup":76}],145:[function(require,module,exports){ arguments[4][77][0].apply(exports,arguments) -},{"../base":147,"buffer":22,"dup":77,"inherits":164}],147:[function(require,module,exports){ +},{"../asn1":144,"dup":77,"inherits":163,"vm":182}],146:[function(require,module,exports){ arguments[4][78][0].apply(exports,arguments) -},{"./buffer":146,"./node":148,"./reporter":149,"dup":78}],148:[function(require,module,exports){ +},{"../base":147,"buffer":23,"dup":78,"inherits":163}],147:[function(require,module,exports){ arguments[4][79][0].apply(exports,arguments) -},{"../base":147,"dup":79,"minimalistic-assert":156}],149:[function(require,module,exports){ +},{"./buffer":146,"./node":148,"./reporter":149,"dup":79}],148:[function(require,module,exports){ arguments[4][80][0].apply(exports,arguments) -},{"dup":80,"inherits":164}],150:[function(require,module,exports){ +},{"../base":147,"dup":80,"minimalistic-assert":156}],149:[function(require,module,exports){ arguments[4][81][0].apply(exports,arguments) -},{"../constants":151,"dup":81}],151:[function(require,module,exports){ +},{"dup":81,"inherits":163}],150:[function(require,module,exports){ arguments[4][82][0].apply(exports,arguments) -},{"./der":150,"dup":82}],152:[function(require,module,exports){ +},{"../constants":151,"dup":82}],151:[function(require,module,exports){ arguments[4][83][0].apply(exports,arguments) -},{"../../asn1":144,"dup":83,"inherits":164}],153:[function(require,module,exports){ +},{"./der":150,"dup":83}],152:[function(require,module,exports){ arguments[4][84][0].apply(exports,arguments) -},{"./der":152,"dup":84}],154:[function(require,module,exports){ +},{"../../asn1":144,"dup":84,"inherits":163}],153:[function(require,module,exports){ arguments[4][85][0].apply(exports,arguments) -},{"../../asn1":144,"buffer":22,"dup":85,"inherits":164}],155:[function(require,module,exports){ +},{"./der":152,"dup":85}],154:[function(require,module,exports){ arguments[4][86][0].apply(exports,arguments) -},{"./der":154,"dup":86}],156:[function(require,module,exports){ +},{"../../asn1":144,"buffer":23,"dup":86,"inherits":163}],155:[function(require,module,exports){ arguments[4][87][0].apply(exports,arguments) -},{"dup":87}],157:[function(require,module,exports){ +},{"./der":154,"dup":87}],156:[function(require,module,exports){ arguments[4][88][0].apply(exports,arguments) -},{"dup":88}],158:[function(require,module,exports){ +},{"dup":88}],157:[function(require,module,exports){ (function (Buffer){ var parseKeys = require('parse-asn1'); var mgf = require('./mgf'); var xor = require('./xor'); var bn = require('bn.js'); var crt = require('browserify-rsa'); -module.exports = function (crypto) { - return privateDecrypt; - function privateDecrypt(private_key, enc) { - var padding; - if (private_key.padding) { - padding = private_key.padding; - } else { - padding = 4; - } - - var key = parseKeys(private_key, crypto); - var k = key.modulus.byteLength(); - if (enc.length > k || new bn(enc).cmp(key.modulus) >= 0) { - throw new Error('decryption error'); - } - var msg = crt(enc, key, crypto); - var zBuffer = new Buffer(k - msg.length); - zBuffer.fill(0); - msg = Buffer.concat([zBuffer, msg], k); - if (padding === 4) { - return oaep(key, msg, crypto); - } else if (padding === 1) { - return pkcs1(key, msg, crypto); - } else if (padding === 3) { - return msg; - } else { - throw new Error('unknown padding'); - } +var createHash = require('create-hash'); +var withPublic = require('./withPublic'); +module.exports = function privateDecrypt(private_key, enc, reverse) { + var padding; + if (private_key.padding) { + padding = private_key.padding; + } else if (reverse) { + padding = 1; + } else { + padding = 4; + } + + var key = parseKeys(private_key); + var k = key.modulus.byteLength(); + if (enc.length > k || new bn(enc).cmp(key.modulus) >= 0) { + throw new Error('decryption error'); + } + var msg; + if (reverse) { + msg = withPublic(new bn(enc), key); + } else { + msg = crt(enc, key); + } + var zBuffer = new Buffer(k - msg.length); + zBuffer.fill(0); + msg = Buffer.concat([zBuffer, msg], k); + if (padding === 4) { + return oaep(key, msg); + } else if (padding === 1) { + return pkcs1(key, msg, reverse); + } else if (padding === 3) { + return msg; + } else { + throw new Error('unknown padding'); } }; -function oaep(key, msg, crypto){ +function oaep(key, msg){ var n = key.modulus; var k = key.modulus.byteLength(); var mLen = msg.length; - var iHash = crypto.createHash('sha1').update(new Buffer('')).digest(); + var iHash = createHash('sha1').update(new Buffer('')).digest(); var hLen = iHash.length; var hLen2 = 2 * hLen; if (msg[0] !== 0) { @@ -19876,8 +20441,8 @@ function oaep(key, msg, crypto){ } var maskedSeed = msg.slice(1, hLen + 1); var maskedDb = msg.slice(hLen + 1); - var seed = xor(maskedSeed, mgf(maskedDb, hLen, crypto)); - var db = xor(maskedDb, mgf(seed, k - hLen - 1, crypto)); + var seed = xor(maskedSeed, mgf(maskedDb, hLen)); + var db = xor(maskedDb, mgf(seed, k - hLen - 1)); if (compare(iHash, db.slice(0, hLen))) { throw new Error('decryption error'); } @@ -19891,7 +20456,7 @@ function oaep(key, msg, crypto){ return db.slice(i); } -function pkcs1(key, msg, crypto){ +function pkcs1(key, msg, reverse){ var p1 = msg.slice(0, 2); var i = 2; var status = 0; @@ -19904,15 +20469,20 @@ function pkcs1(key, msg, crypto){ var ps = msg.slice(2, i - 1); var p2 = msg.slice(i - 1, i); - if (p1.toString('hex') !== '0002') { + if ((p1.toString('hex') !== '0002' && !reverse) || (p1.toString('hex') !== '0001' && reverse)){ status++; } if (ps.length < 8) { status++; } + if (status) { + throw new Error('decryption error'); + } return msg.slice(i); } function compare(a, b){ + a = new Buffer(a); + b = new Buffer(b); var dif = 0; var len = a.length; if (a.length !== b.length) { @@ -19926,54 +20496,57 @@ function compare(a, b){ return dif; } }).call(this,require("buffer").Buffer) -},{"./mgf":135,"./xor":160,"bn.js":136,"browserify-rsa":137,"buffer":22,"parse-asn1":142}],159:[function(require,module,exports){ +},{"./mgf":136,"./withPublic":159,"./xor":160,"bn.js":137,"browserify-rsa":138,"buffer":23,"create-hash":115,"parse-asn1":143}],158:[function(require,module,exports){ (function (Buffer){ var parseKeys = require('parse-asn1'); +var randomBytes = require('randombytes'); +var createHash = require('create-hash'); var mgf = require('./mgf'); var xor = require('./xor'); var bn = require('bn.js'); +var withPublic = require('./withPublic'); +var crt = require('browserify-rsa'); + var constants = { RSA_PKCS1_OAEP_PADDING: 4, RSA_PKCS1_PADDIN: 1, RSA_NO_PADDING: 3 }; -module.exports = function (crypto) { - return publicEncrypt; - function publicEncrypt(public_key, msg) { - var padding; - if (public_key.padding) { - padding = public_key.padding; - } else { - padding = 4; - } - var key = parseKeys(public_key); - var paddedMsg; - if (padding === 4) { - paddedMsg = oaep(key, msg, crypto); - } else if (padding === 1) { - paddedMsg = pkcs1(key, msg, crypto); - } else if (padding === 3) { - paddedMsg = new bn(msg); - if (paddedMsg.cmp(key.modulus) >= 0) { - throw new Error('data too long for modulus'); - } - } else { - throw new Error('unknown padding'); +module.exports = function publicEncrypt(public_key, msg, reverse) { + var padding; + if (public_key.padding) { + padding = public_key.padding; + } else if (reverse) { + padding = 1; + } else { + padding = 4; + } + var key = parseKeys(public_key); + var paddedMsg; + if (padding === 4) { + paddedMsg = oaep(key, msg); + } else if (padding === 1) { + paddedMsg = pkcs1(key, msg, reverse); + } else if (padding === 3) { + paddedMsg = new bn(msg); + if (paddedMsg.cmp(key.modulus) >= 0) { + throw new Error('data too long for modulus'); } - var enc = paddedMsg - .toRed(bn.mont(key.modulus)) - .redPow(new bn(key.publicExponent)) - .fromRed() - .toArray(); - return new Buffer(enc); + } else { + throw new Error('unknown padding'); + } + if (reverse) { + return crt(paddedMsg, key); + } else { + return withPublic(paddedMsg, key); } }; -function oaep(key, msg, crypto){ +function oaep(key, msg){ var k = key.modulus.byteLength(); var mLen = msg.length; - var iHash = crypto.createHash('sha1').update(new Buffer('')).digest(); + var iHash = createHash('sha1').update(new Buffer('')).digest(); var hLen = iHash.length; var hLen2 = 2 * hLen; if (mLen > k - hLen2 - 2) { @@ -19982,29 +20555,35 @@ function oaep(key, msg, crypto){ var ps = new Buffer(k - mLen - hLen2 - 2); ps.fill(0); var dblen = k - hLen - 1; - var seed = crypto.randomBytes(hLen); - var maskedDb = xor(Buffer.concat([iHash, ps, new Buffer([1]), msg], dblen), mgf(seed, dblen, crypto)); - var maskedSeed = xor(seed, mgf(maskedDb, hLen, crypto)); + var seed = randomBytes(hLen); + var maskedDb = xor(Buffer.concat([iHash, ps, new Buffer([1]), msg], dblen), mgf(seed, dblen)); + var maskedSeed = xor(seed, mgf(maskedDb, hLen)); return new bn(Buffer.concat([new Buffer([0]), maskedSeed, maskedDb], k)); } -function pkcs1(key, msg, crypto){ +function pkcs1(key, msg, reverse){ var mLen = msg.length; var k = key.modulus.byteLength(); if (mLen > k - 11) { throw new Error('message too long'); } - var ps = nonZero(k - mLen - 3, crypto); - return new bn(Buffer.concat([new Buffer([0, 2]), ps, new Buffer([0]), msg], k)); + var ps; + if (reverse) { + ps = new Buffer(k - mLen - 3); + ps.fill(0xff); + } else { + ps = nonZero(k - mLen - 3); + } + return new bn(Buffer.concat([new Buffer([0, reverse?1:2]), ps, new Buffer([0]), msg], k)); } function nonZero(len, crypto) { var out = new Buffer(len); var i = 0; - var cache = crypto.randomBytes(len*2); + var cache = randomBytes(len*2); var cur = 0; var num; while (i < len) { if (cur === cache.length) { - cache = crypto.randomBytes(len*2); + cache = randomBytes(len*2); cur = 0; } num = cache[cur++]; @@ -20015,7 +20594,20 @@ function nonZero(len, crypto) { return out; } }).call(this,require("buffer").Buffer) -},{"./mgf":135,"./xor":160,"bn.js":136,"buffer":22,"parse-asn1":142}],160:[function(require,module,exports){ +},{"./mgf":136,"./withPublic":159,"./xor":160,"bn.js":137,"browserify-rsa":138,"buffer":23,"create-hash":115,"parse-asn1":143,"randombytes":161}],159:[function(require,module,exports){ +(function (Buffer){ +var bn = require('bn.js'); +function withPublic(paddedMsg, key) { + return new Buffer(paddedMsg + .toRed(bn.mont(key.modulus)) + .redPow(new bn(key.publicExponent)) + .fromRed() + .toArray()); +} + +module.exports = withPublic; +}).call(this,require("buffer").Buffer) +},{"bn.js":137,"buffer":23}],160:[function(require,module,exports){ module.exports = function xor(a, b) { var len = a.length; var i = -1; @@ -20056,22 +20648,7 @@ function oldBrowser() { } }).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {},require("buffer").Buffer) -},{"_process":167,"buffer":22}],162:[function(require,module,exports){ -'use strict'; -var pbkdf2Export = require('pbkdf2-compat/pbkdf2') - -module.exports = function (crypto, exports) { - exports = exports || {} - - var exported = pbkdf2Export(crypto) - - exports.pbkdf2 = exported.pbkdf2 - exports.pbkdf2Sync = exported.pbkdf2Sync - - return exports -} - -},{"pbkdf2-compat/pbkdf2":133}],163:[function(require,module,exports){ +},{"_process":166,"buffer":23}],162:[function(require,module,exports){ // Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a @@ -20374,7 +20951,7 @@ function isUndefined(arg) { return arg === void 0; } -},{}],164:[function(require,module,exports){ +},{}],163:[function(require,module,exports){ if (typeof Object.create === 'function') { // implementation from standard node.js 'util' module module.exports = function inherits(ctor, superCtor) { @@ -20399,12 +20976,12 @@ if (typeof Object.create === 'function') { } } -},{}],165:[function(require,module,exports){ +},{}],164:[function(require,module,exports){ module.exports = Array.isArray || function (arr) { return Object.prototype.toString.call(arr) == '[object Array]'; }; -},{}],166:[function(require,module,exports){ +},{}],165:[function(require,module,exports){ (function (process){ // Copyright Joyent, Inc. and other Node contributors. // @@ -20632,7 +21209,7 @@ var substr = 'ab'.substr(-1) === 'b' ; }).call(this,require('_process')) -},{"_process":167}],167:[function(require,module,exports){ +},{"_process":166}],166:[function(require,module,exports){ // shim for using process in browser var process=module.exports={};process.platform='browser';process.stdout={write:function(x){console.log(x)}};process.stderr={write:function(x){console.error(x)}};process.exit=noop; @@ -20691,10 +21268,10 @@ process.chdir = function (dir) { }; process.umask = function() { return 0; }; -},{}],168:[function(require,module,exports){ +},{}],167:[function(require,module,exports){ module.exports = require("./lib/_stream_duplex.js") -},{"./lib/_stream_duplex.js":169}],169:[function(require,module,exports){ +},{"./lib/_stream_duplex.js":168}],168:[function(require,module,exports){ (function (process){ // Copyright Joyent, Inc. and other Node contributors. // @@ -20787,7 +21364,7 @@ function forEach (xs, f) { } }).call(this,require('_process')) -},{"./_stream_readable":171,"./_stream_writable":173,"_process":167,"core-util-is":174,"inherits":164}],170:[function(require,module,exports){ +},{"./_stream_readable":170,"./_stream_writable":172,"_process":166,"core-util-is":173,"inherits":163}],169:[function(require,module,exports){ // Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a @@ -20835,7 +21412,7 @@ PassThrough.prototype._transform = function(chunk, encoding, cb) { cb(null, chunk); }; -},{"./_stream_transform":172,"core-util-is":174,"inherits":164}],171:[function(require,module,exports){ +},{"./_stream_transform":171,"core-util-is":173,"inherits":163}],170:[function(require,module,exports){ (function (process){ // Copyright Joyent, Inc. and other Node contributors. // @@ -20888,15 +21465,29 @@ util.inherits = require('inherits'); var StringDecoder; + +/**/ +var debug = require('util'); +if (debug && debug.debuglog) { + debug = debug.debuglog('stream'); +} else { + debug = function () {}; +} +/**/ + + util.inherits(Readable, Stream); function ReadableState(options, stream) { + var Duplex = require('./_stream_duplex'); + options = options || {}; // the point at which it stops calling _read() to fill the buffer // Note: 0 is a valid value, means "don't call _read preemptively ever" var hwm = options.highWaterMark; - this.highWaterMark = (hwm || hwm === 0) ? hwm : 16 * 1024; + var defaultHwm = options.objectMode ? 16 : 16 * 1024; + this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm; // cast to ints. this.highWaterMark = ~~this.highWaterMark; @@ -20905,19 +21496,13 @@ function ReadableState(options, stream) { this.length = 0; this.pipes = null; this.pipesCount = 0; - this.flowing = false; + this.flowing = null; this.ended = false; this.endEmitted = false; this.reading = false; - // In streams that never have any data, and do push(null) right away, - // the consumer can miss the 'end' event if they do some I/O before - // consuming the stream. So, we don't emit('end') until some reading - // happens. - this.calledRead = false; - // a flag to be able to tell if the onwrite cb is called immediately, - // or on a later tick. We set this to true at first, becuase any + // or on a later tick. We set this to true at first, because any // actions that shouldn't happen until "later" should generally also // not happen before the first write call. this.sync = true; @@ -20933,6 +21518,9 @@ function ReadableState(options, stream) { // make all the buffer merging and length checks go away this.objectMode = !!options.objectMode; + if (stream instanceof Duplex) + this.objectMode = this.objectMode || !!options.readableObjectMode; + // Crypto is kind of old and crusty. Historically, its default string // encoding is 'binary' so we have to make this configurable. // Everything else in the universe uses 'utf8', though. @@ -20959,6 +21547,8 @@ function ReadableState(options, stream) { } function Readable(options) { + var Duplex = require('./_stream_duplex'); + if (!(this instanceof Readable)) return new Readable(options); @@ -20977,7 +21567,7 @@ function Readable(options) { Readable.prototype.push = function(chunk, encoding) { var state = this._readableState; - if (typeof chunk === 'string' && !state.objectMode) { + if (util.isString(chunk) && !state.objectMode) { encoding = encoding || state.defaultEncoding; if (encoding !== state.encoding) { chunk = new Buffer(chunk, encoding); @@ -20998,7 +21588,7 @@ function readableAddChunk(stream, state, chunk, encoding, addToFront) { var er = chunkInvalid(state, chunk); if (er) { stream.emit('error', er); - } else if (chunk === null || chunk === undefined) { + } else if (util.isNullOrUndefined(chunk)) { state.reading = false; if (!state.ended) onEofChunk(stream, state); @@ -21013,17 +21603,24 @@ function readableAddChunk(stream, state, chunk, encoding, addToFront) { if (state.decoder && !addToFront && !encoding) chunk = state.decoder.write(chunk); - // update the buffer info. - state.length += state.objectMode ? 1 : chunk.length; - if (addToFront) { - state.buffer.unshift(chunk); - } else { + if (!addToFront) state.reading = false; - state.buffer.push(chunk); - } - if (state.needReadable) - emitReadable(stream); + // if we want the data now, just emit it. + if (state.flowing && state.length === 0 && !state.sync) { + stream.emit('data', chunk); + stream.read(0); + } else { + // update the buffer info. + state.length += state.objectMode ? 1 : chunk.length; + if (addToFront) + state.buffer.unshift(chunk); + else + state.buffer.push(chunk); + + if (state.needReadable) + emitReadable(stream); + } maybeReadMore(stream, state); } @@ -21056,6 +21653,7 @@ Readable.prototype.setEncoding = function(enc) { StringDecoder = require('string_decoder/').StringDecoder; this._readableState.decoder = new StringDecoder(enc); this._readableState.encoding = enc; + return this; }; // Don't raise the hwm > 128MB @@ -21079,7 +21677,7 @@ function howMuchToRead(n, state) { if (state.objectMode) return n === 0 ? 0 : 1; - if (n === null || isNaN(n)) { + if (isNaN(n) || util.isNull(n)) { // only flow one buffer at a time if (state.flowing && state.buffer.length) return state.buffer[0].length; @@ -21111,12 +21709,11 @@ function howMuchToRead(n, state) { // you can override either this method, or the async _read(n) below. Readable.prototype.read = function(n) { + debug('read', n); var state = this._readableState; - state.calledRead = true; var nOrig = n; - var ret; - if (typeof n !== 'number' || n > 0) + if (!util.isNumber(n) || n > 0) state.emittedReadable = false; // if we're doing read(0) to trigger a readable event, but we @@ -21125,7 +21722,11 @@ Readable.prototype.read = function(n) { if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) { - emitReadable(this); + debug('read: emitReadable', state.length, state.ended); + if (state.length === 0 && state.ended) + endReadable(this); + else + emitReadable(this); return null; } @@ -21133,28 +21734,9 @@ Readable.prototype.read = function(n) { // if we've ended, and we're now clear, then finish it up. if (n === 0 && state.ended) { - ret = null; - - // In cases where the decoder did not receive enough data - // to produce a full chunk, then immediately received an - // EOF, state.buffer will contain [, ]. - // howMuchToRead will see this and coerce the amount to - // read to zero (because it's looking at the length of the - // first in state.buffer), and we'll end up here. - // - // This can only happen via state.decoder -- no other venue - // exists for pushing a zero-length chunk into state.buffer - // and triggering this behavior. In this case, we return our - // remaining data and end the stream, if appropriate. - if (state.length > 0 && state.decoder) { - ret = fromList(n, state); - state.length -= ret.length; - } - if (state.length === 0) endReadable(this); - - return ret; + return null; } // All the actual chunk generation logic needs to be @@ -21181,17 +21763,23 @@ Readable.prototype.read = function(n) { // if we need a readable event, then we need to do some reading. var doRead = state.needReadable; + debug('need readable', doRead); // if we currently have less than the highWaterMark, then also read some - if (state.length - n <= state.highWaterMark) + if (state.length === 0 || state.length - n < state.highWaterMark) { doRead = true; + debug('length less than watermark', doRead); + } // however, if we've ended, then there's no point, and if we're already // reading, then it's unnecessary. - if (state.ended || state.reading) + if (state.ended || state.reading) { doRead = false; + debug('reading or ended', doRead); + } if (doRead) { + debug('do read'); state.reading = true; state.sync = true; // if the length is currently zero, then we *need* a readable event. @@ -21202,18 +21790,18 @@ Readable.prototype.read = function(n) { state.sync = false; } - // If _read called its callback synchronously, then `reading` - // will be false, and we need to re-evaluate how much data we - // can return to the user. + // If _read pushed data synchronously, then `reading` will be false, + // and we need to re-evaluate how much data we can return to the user. if (doRead && !state.reading) n = howMuchToRead(nOrig, state); + var ret; if (n > 0) ret = fromList(n, state); else ret = null; - if (ret === null) { + if (util.isNull(ret)) { state.needReadable = true; n = 0; } @@ -21225,21 +21813,21 @@ Readable.prototype.read = function(n) { if (state.length === 0 && !state.ended) state.needReadable = true; - // If we happened to read() exactly the remaining amount in the - // buffer, and the EOF has been seen at this point, then make sure - // that we emit 'end' on the very next tick. - if (state.ended && !state.endEmitted && state.length === 0) + // If we tried to read() past the EOF, then emit end on the next tick. + if (nOrig !== n && state.ended && state.length === 0) endReadable(this); + if (!util.isNull(ret)) + this.emit('data', ret); + return ret; }; function chunkInvalid(state, chunk) { var er = null; - if (!Buffer.isBuffer(chunk) && - 'string' !== typeof chunk && - chunk !== null && - chunk !== undefined && + if (!util.isBuffer(chunk) && + !util.isString(chunk) && + !util.isNullOrUndefined(chunk) && !state.objectMode) { er = new TypeError('Invalid non-string/buffer chunk'); } @@ -21257,12 +21845,8 @@ function onEofChunk(stream, state) { } state.ended = true; - // if we've ended and we have some data left, then emit - // 'readable' now to make sure it gets picked up. - if (state.length > 0) - emitReadable(stream); - else - endReadable(stream); + // emit 'readable' now to make sure it gets picked up. + emitReadable(stream); } // Don't emit readable right away in sync mode, because this can trigger @@ -21271,20 +21855,22 @@ function onEofChunk(stream, state) { function emitReadable(stream) { var state = stream._readableState; state.needReadable = false; - if (state.emittedReadable) - return; - - state.emittedReadable = true; - if (state.sync) - process.nextTick(function() { + if (!state.emittedReadable) { + debug('emitReadable', state.flowing); + state.emittedReadable = true; + if (state.sync) + process.nextTick(function() { + emitReadable_(stream); + }); + else emitReadable_(stream); - }); - else - emitReadable_(stream); + } } function emitReadable_(stream) { + debug('emit readable'); stream.emit('readable'); + flow(stream); } @@ -21307,6 +21893,7 @@ function maybeReadMore_(stream, state) { var len = state.length; while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) { + debug('maybeReadMore read 0'); stream.read(0); if (len === state.length) // didn't get any data, stop spinning. @@ -21341,6 +21928,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) { break; } state.pipesCount += 1; + debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts); var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && @@ -21354,11 +21942,14 @@ Readable.prototype.pipe = function(dest, pipeOpts) { dest.on('unpipe', onunpipe); function onunpipe(readable) { - if (readable !== src) return; - cleanup(); + debug('onunpipe'); + if (readable === src) { + cleanup(); + } } function onend() { + debug('onend'); dest.end(); } @@ -21370,6 +21961,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) { dest.on('drain', ondrain); function cleanup() { + debug('cleanup'); // cleanup event handlers once the pipe is broken dest.removeListener('close', onclose); dest.removeListener('finish', onfinish); @@ -21378,19 +21970,34 @@ Readable.prototype.pipe = function(dest, pipeOpts) { dest.removeListener('unpipe', onunpipe); src.removeListener('end', onend); src.removeListener('end', cleanup); + src.removeListener('data', ondata); // if the reader is waiting for a drain event from this // specific writer, then it would cause it to never start // flowing again. // So, if this is awaiting a drain, then we just call it now. // If we don't know, then assume that we are waiting for one. - if (!dest._writableState || dest._writableState.needDrain) + if (state.awaitDrain && + (!dest._writableState || dest._writableState.needDrain)) ondrain(); } + src.on('data', ondata); + function ondata(chunk) { + debug('ondata'); + var ret = dest.write(chunk); + if (false === ret) { + debug('false write response, pause', + src._readableState.awaitDrain); + src._readableState.awaitDrain++; + src.pause(); + } + } + // if the dest has an error, then stop piping into it. // however, don't suppress the throwing behavior for this. function onerror(er) { + debug('onerror', er); unpipe(); dest.removeListener('error', onerror); if (EE.listenerCount(dest, 'error') === 0) @@ -21414,12 +22021,14 @@ Readable.prototype.pipe = function(dest, pipeOpts) { } dest.once('close', onclose); function onfinish() { + debug('onfinish'); dest.removeListener('close', onclose); unpipe(); } dest.once('finish', onfinish); function unpipe() { + debug('unpipe'); src.unpipe(dest); } @@ -21428,16 +22037,8 @@ Readable.prototype.pipe = function(dest, pipeOpts) { // start the flow if it hasn't been started already. if (!state.flowing) { - // the handler that waits for readable events after all - // the data gets sucked out in flow. - // This would be easier to follow with a .once() handler - // in flow(), but that is too slow. - this.on('readable', pipeOnReadable); - - state.flowing = true; - process.nextTick(function() { - flow(src); - }); + debug('pipe resume'); + src.resume(); } return dest; @@ -21445,63 +22046,15 @@ Readable.prototype.pipe = function(dest, pipeOpts) { function pipeOnDrain(src) { return function() { - var dest = this; var state = src._readableState; - state.awaitDrain--; - if (state.awaitDrain === 0) + debug('pipeOnDrain', state.awaitDrain); + if (state.awaitDrain) + state.awaitDrain--; + if (state.awaitDrain === 0 && EE.listenerCount(src, 'data')) { + state.flowing = true; flow(src); - }; -} - -function flow(src) { - var state = src._readableState; - var chunk; - state.awaitDrain = 0; - - function write(dest, i, list) { - var written = dest.write(chunk); - if (false === written) { - state.awaitDrain++; } - } - - while (state.pipesCount && null !== (chunk = src.read())) { - - if (state.pipesCount === 1) - write(state.pipes, 0, null); - else - forEach(state.pipes, write); - - src.emit('data', chunk); - - // if anyone needs a drain, then we have to wait for that. - if (state.awaitDrain > 0) - return; - } - - // if every destination was unpiped, either before entering this - // function, or in the while loop, then stop flowing. - // - // NB: This is a pretty rare edge case. - if (state.pipesCount === 0) { - state.flowing = false; - - // if there were data event listeners added, then switch to old mode. - if (EE.listenerCount(src, 'data') > 0) - emitDataEvents(src); - return; - } - - // at this point, no one needed a drain, so we just ran out of data - // on the next readable event, start it over again. - state.ranOut = true; -} - -function pipeOnReadable() { - if (this._readableState.ranOut) { - this._readableState.ranOut = false; - flow(this); - } + }; } @@ -21524,7 +22077,6 @@ Readable.prototype.unpipe = function(dest) { // got a match. state.pipes = null; state.pipesCount = 0; - this.removeListener('readable', pipeOnReadable); state.flowing = false; if (dest) dest.emit('unpipe', this); @@ -21539,7 +22091,6 @@ Readable.prototype.unpipe = function(dest) { var len = state.pipesCount; state.pipes = null; state.pipesCount = 0; - this.removeListener('readable', pipeOnReadable); state.flowing = false; for (var i = 0; i < len; i++) @@ -21567,8 +22118,11 @@ Readable.prototype.unpipe = function(dest) { Readable.prototype.on = function(ev, fn) { var res = Stream.prototype.on.call(this, ev, fn); - if (ev === 'data' && !this._readableState.flowing) - emitDataEvents(this); + // If listening to data, and it has not explicitly been paused, + // then call resume to start the flow of data on the next tick. + if (ev === 'data' && false !== this._readableState.flowing) { + this.resume(); + } if (ev === 'readable' && this.readable) { var state = this._readableState; @@ -21577,7 +22131,11 @@ Readable.prototype.on = function(ev, fn) { state.emittedReadable = false; state.needReadable = true; if (!state.reading) { - this.read(0); + var self = this; + process.nextTick(function() { + debug('readable nexttick read 0'); + self.read(0); + }); } else if (state.length) { emitReadable(this, state); } @@ -21591,63 +22149,54 @@ Readable.prototype.addListener = Readable.prototype.on; // pause() and resume() are remnants of the legacy readable stream API // If the user uses them, then switch into old mode. Readable.prototype.resume = function() { - emitDataEvents(this); - this.read(0); - this.emit('resume'); + var state = this._readableState; + if (!state.flowing) { + debug('resume'); + state.flowing = true; + if (!state.reading) { + debug('resume read 0'); + this.read(0); + } + resume(this, state); + } + return this; }; +function resume(stream, state) { + if (!state.resumeScheduled) { + state.resumeScheduled = true; + process.nextTick(function() { + resume_(stream, state); + }); + } +} + +function resume_(stream, state) { + state.resumeScheduled = false; + stream.emit('resume'); + flow(stream); + if (state.flowing && !state.reading) + stream.read(0); +} + Readable.prototype.pause = function() { - emitDataEvents(this, true); - this.emit('pause'); + debug('call pause flowing=%j', this._readableState.flowing); + if (false !== this._readableState.flowing) { + debug('pause'); + this._readableState.flowing = false; + this.emit('pause'); + } + return this; }; -function emitDataEvents(stream, startPaused) { +function flow(stream) { var state = stream._readableState; - + debug('flow', state.flowing); if (state.flowing) { - // https://github.com/isaacs/readable-stream/issues/16 - throw new Error('Cannot switch to old mode now.'); + do { + var chunk = stream.read(); + } while (null !== chunk && state.flowing); } - - var paused = startPaused || false; - var readable = false; - - // convert to an old-style stream. - stream.readable = true; - stream.pipe = Stream.prototype.pipe; - stream.on = stream.addListener = Stream.prototype.on; - - stream.on('readable', function() { - readable = true; - - var c; - while (!paused && (null !== (c = stream.read()))) - stream.emit('data', c); - - if (c === null) { - readable = false; - stream._readableState.needReadable = true; - } - }); - - stream.pause = function() { - paused = true; - this.emit('pause'); - }; - - stream.resume = function() { - paused = false; - if (readable) - process.nextTick(function() { - stream.emit('readable'); - }); - else - this.read(0); - this.emit('resume'); - }; - - // now make it start, just in case it hadn't already. - stream.emit('readable'); } // wrap an old-style stream as the async data source. @@ -21659,6 +22208,7 @@ Readable.prototype.wrap = function(stream) { var self = this; stream.on('end', function() { + debug('wrapped end'); if (state.decoder && !state.ended) { var chunk = state.decoder.end(); if (chunk && chunk.length) @@ -21669,14 +22219,10 @@ Readable.prototype.wrap = function(stream) { }); stream.on('data', function(chunk) { + debug('wrapped data'); if (state.decoder) chunk = state.decoder.write(chunk); - - // don't skip over falsy values in objectMode - //if (state.objectMode && util.isNullOrUndefined(chunk)) - if (state.objectMode && (chunk === null || chunk === undefined)) - return; - else if (!state.objectMode && (!chunk || !chunk.length)) + if (!chunk || !state.objectMode && !chunk.length) return; var ret = self.push(chunk); @@ -21689,8 +22235,7 @@ Readable.prototype.wrap = function(stream) { // proxy all the other methods. // important when wrapping filters and duplexes. for (var i in stream) { - if (typeof stream[i] === 'function' && - typeof this[i] === 'undefined') { + if (util.isFunction(stream[i]) && util.isUndefined(this[i])) { this[i] = function(method) { return function() { return stream[method].apply(stream, arguments); }}(i); @@ -21706,6 +22251,7 @@ Readable.prototype.wrap = function(stream) { // when we try to consume some more bytes, simply unpause the // underlying stream. self._read = function(n) { + debug('wrapped _read', n); if (paused) { paused = false; stream.resume(); @@ -21794,7 +22340,7 @@ function endReadable(stream) { if (state.length > 0) throw new Error('endReadable called on non-empty stream'); - if (!state.endEmitted && state.calledRead) { + if (!state.endEmitted) { state.ended = true; process.nextTick(function() { // Check that we didn't get one last unshift. @@ -21821,7 +22367,7 @@ function indexOf (xs, x) { } }).call(this,require('_process')) -},{"_process":167,"buffer":22,"core-util-is":174,"events":163,"inherits":164,"isarray":165,"stream":179,"string_decoder/":180}],172:[function(require,module,exports){ +},{"./_stream_duplex":168,"_process":166,"buffer":23,"core-util-is":173,"events":162,"inherits":163,"isarray":164,"stream":178,"string_decoder/":179,"util":22}],171:[function(require,module,exports){ // Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a @@ -21921,7 +22467,7 @@ function afterTransform(stream, er, data) { ts.writechunk = null; ts.writecb = null; - if (data !== null && data !== undefined) + if (!util.isNullOrUndefined(data)) stream.push(data); if (cb) @@ -21941,7 +22487,7 @@ function Transform(options) { Duplex.call(this, options); - var ts = this._transformState = new TransformState(options, this); + this._transformState = new TransformState(options, this); // when the writable side finishes, then flush out anything remaining. var stream = this; @@ -21954,8 +22500,8 @@ function Transform(options) { // sync guard flag. this._readableState.sync = false; - this.once('finish', function() { - if ('function' === typeof this._flush) + this.once('prefinish', function() { + if (util.isFunction(this._flush)) this._flush(function(er) { done(stream, er); }); @@ -22003,7 +22549,7 @@ Transform.prototype._write = function(chunk, encoding, cb) { Transform.prototype._read = function(n) { var ts = this._transformState; - if (ts.writechunk !== null && ts.writecb && !ts.transforming) { + if (!util.isNull(ts.writechunk) && ts.writecb && !ts.transforming) { ts.transforming = true; this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform); } else { @@ -22021,7 +22567,6 @@ function done(stream, er) { // if there's nothing in the write buffer, then that means // that nothing more will ever be provided var ws = stream._writableState; - var rs = stream._readableState; var ts = stream._transformState; if (ws.length) @@ -22033,7 +22578,7 @@ function done(stream, er) { return stream.push(null); } -},{"./_stream_duplex":169,"core-util-is":174,"inherits":164}],173:[function(require,module,exports){ +},{"./_stream_duplex":168,"core-util-is":173,"inherits":163}],172:[function(require,module,exports){ (function (process){ // Copyright Joyent, Inc. and other Node contributors. // @@ -22085,18 +22630,24 @@ function WriteReq(chunk, encoding, cb) { } function WritableState(options, stream) { + var Duplex = require('./_stream_duplex'); + options = options || {}; // the point at which write() starts returning false // Note: 0 is a valid value, means that we always return false if // the entire buffer is not flushed immediately on write() var hwm = options.highWaterMark; - this.highWaterMark = (hwm || hwm === 0) ? hwm : 16 * 1024; + var defaultHwm = options.objectMode ? 16 : 16 * 1024; + this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm; // object stream flag to indicate whether or not this stream // contains buffers or objects. this.objectMode = !!options.objectMode; + if (stream instanceof Duplex) + this.objectMode = this.objectMode || !!options.writableObjectMode; + // cast to ints. this.highWaterMark = ~~this.highWaterMark; @@ -22127,8 +22678,11 @@ function WritableState(options, stream) { // a flag to see when we're in the middle of a write. this.writing = false; + // when true all writes will be buffered until .uncork() call + this.corked = 0; + // a flag to be able to tell if the onwrite cb is called immediately, - // or on a later tick. We set this to true at first, becuase any + // or on a later tick. We set this to true at first, because any // actions that shouldn't happen until "later" should generally also // not happen before the first write call. this.sync = true; @@ -22151,6 +22705,14 @@ function WritableState(options, stream) { this.buffer = []; + // number of pending user-supplied write callbacks + // this must be 0 before 'finish' can be emitted + this.pendingcb = 0; + + // emit prefinish if the only thing we're waiting for is _write cbs + // This is relevant for synchronous Transform streams + this.prefinished = false; + // True if the error was already emitted and should not be thrown again this.errorEmitted = false; } @@ -22193,10 +22755,9 @@ function writeAfterEnd(stream, state, cb) { // how many bytes or characters. function validChunk(stream, state, chunk, cb) { var valid = true; - if (!Buffer.isBuffer(chunk) && - 'string' !== typeof chunk && - chunk !== null && - chunk !== undefined && + if (!util.isBuffer(chunk) && + !util.isString(chunk) && + !util.isNullOrUndefined(chunk) && !state.objectMode) { var er = new TypeError('Invalid non-string/buffer chunk'); stream.emit('error', er); @@ -22212,31 +22773,54 @@ Writable.prototype.write = function(chunk, encoding, cb) { var state = this._writableState; var ret = false; - if (typeof encoding === 'function') { + if (util.isFunction(encoding)) { cb = encoding; encoding = null; } - if (Buffer.isBuffer(chunk)) + if (util.isBuffer(chunk)) encoding = 'buffer'; else if (!encoding) encoding = state.defaultEncoding; - if (typeof cb !== 'function') + if (!util.isFunction(cb)) cb = function() {}; if (state.ended) writeAfterEnd(this, state, cb); - else if (validChunk(this, state, chunk, cb)) + else if (validChunk(this, state, chunk, cb)) { + state.pendingcb++; ret = writeOrBuffer(this, state, chunk, encoding, cb); + } return ret; }; +Writable.prototype.cork = function() { + var state = this._writableState; + + state.corked++; +}; + +Writable.prototype.uncork = function() { + var state = this._writableState; + + if (state.corked) { + state.corked--; + + if (!state.writing && + !state.corked && + !state.finished && + !state.bufferProcessing && + state.buffer.length) + clearBuffer(this, state); + } +}; + function decodeChunk(state, chunk, encoding) { if (!state.objectMode && state.decodeStrings !== false && - typeof chunk === 'string') { + util.isString(chunk)) { chunk = new Buffer(chunk, encoding); } return chunk; @@ -22247,7 +22831,7 @@ function decodeChunk(state, chunk, encoding) { // If we return false, then we need a drain event, so set that flag. function writeOrBuffer(stream, state, chunk, encoding, cb) { chunk = decodeChunk(state, chunk, encoding); - if (Buffer.isBuffer(chunk)) + if (util.isBuffer(chunk)) encoding = 'buffer'; var len = state.objectMode ? 1 : chunk.length; @@ -22258,30 +22842,36 @@ function writeOrBuffer(stream, state, chunk, encoding, cb) { if (!ret) state.needDrain = true; - if (state.writing) + if (state.writing || state.corked) state.buffer.push(new WriteReq(chunk, encoding, cb)); else - doWrite(stream, state, len, chunk, encoding, cb); + doWrite(stream, state, false, len, chunk, encoding, cb); return ret; } -function doWrite(stream, state, len, chunk, encoding, cb) { +function doWrite(stream, state, writev, len, chunk, encoding, cb) { state.writelen = len; state.writecb = cb; state.writing = true; state.sync = true; - stream._write(chunk, encoding, state.onwrite); + if (writev) + stream._writev(chunk, state.onwrite); + else + stream._write(chunk, encoding, state.onwrite); state.sync = false; } function onwriteError(stream, state, sync, er, cb) { if (sync) process.nextTick(function() { + state.pendingcb--; cb(er); }); - else + else { + state.pendingcb--; cb(er); + } stream._writableState.errorEmitted = true; stream.emit('error', er); @@ -22307,8 +22897,12 @@ function onwrite(stream, er) { // Check if we're actually ready to finish, but don't emit yet var finished = needFinish(stream, state); - if (!finished && !state.bufferProcessing && state.buffer.length) + if (!finished && + !state.corked && + !state.bufferProcessing && + state.buffer.length) { clearBuffer(stream, state); + } if (sync) { process.nextTick(function() { @@ -22323,9 +22917,9 @@ function onwrite(stream, er) { function afterWrite(stream, state, finished, cb) { if (!finished) onwriteDrain(stream, state); + state.pendingcb--; cb(); - if (finished) - finishMaybe(stream, state); + finishMaybe(stream, state); } // Must force callback to be called on nextTick, so that we don't @@ -22343,51 +22937,82 @@ function onwriteDrain(stream, state) { function clearBuffer(stream, state) { state.bufferProcessing = true; - for (var c = 0; c < state.buffer.length; c++) { - var entry = state.buffer[c]; - var chunk = entry.chunk; - var encoding = entry.encoding; - var cb = entry.callback; - var len = state.objectMode ? 1 : chunk.length; - - doWrite(stream, state, len, chunk, encoding, cb); - - // if we didn't call the onwrite immediately, then - // it means that we need to wait until it does. - // also, that means that the chunk and cb are currently - // being processed, so move the buffer counter past them. - if (state.writing) { - c++; - break; + if (stream._writev && state.buffer.length > 1) { + // Fast case, write everything using _writev() + var cbs = []; + for (var c = 0; c < state.buffer.length; c++) + cbs.push(state.buffer[c].callback); + + // count the one we are adding, as well. + // TODO(isaacs) clean this up + state.pendingcb++; + doWrite(stream, state, true, state.length, state.buffer, '', function(err) { + for (var i = 0; i < cbs.length; i++) { + state.pendingcb--; + cbs[i](err); + } + }); + + // Clear buffer + state.buffer = []; + } else { + // Slow case, write chunks one-by-one + for (var c = 0; c < state.buffer.length; c++) { + var entry = state.buffer[c]; + var chunk = entry.chunk; + var encoding = entry.encoding; + var cb = entry.callback; + var len = state.objectMode ? 1 : chunk.length; + + doWrite(stream, state, false, len, chunk, encoding, cb); + + // if we didn't call the onwrite immediately, then + // it means that we need to wait until it does. + // also, that means that the chunk and cb are currently + // being processed, so move the buffer counter past them. + if (state.writing) { + c++; + break; + } } + + if (c < state.buffer.length) + state.buffer = state.buffer.slice(c); + else + state.buffer.length = 0; } state.bufferProcessing = false; - if (c < state.buffer.length) - state.buffer = state.buffer.slice(c); - else - state.buffer.length = 0; } Writable.prototype._write = function(chunk, encoding, cb) { cb(new Error('not implemented')); + }; +Writable.prototype._writev = null; + Writable.prototype.end = function(chunk, encoding, cb) { var state = this._writableState; - if (typeof chunk === 'function') { + if (util.isFunction(chunk)) { cb = chunk; chunk = null; encoding = null; - } else if (typeof encoding === 'function') { + } else if (util.isFunction(encoding)) { cb = encoding; encoding = null; } - if (typeof chunk !== 'undefined' && chunk !== null) + if (!util.isNullOrUndefined(chunk)) this.write(chunk, encoding); + // .end() fully uncorks + if (state.corked) { + state.corked = 1; + this.uncork(); + } + // ignore unnecessary end() calls. if (!state.ending && !state.finished) endWritable(this, state, cb); @@ -22401,11 +23026,22 @@ function needFinish(stream, state) { !state.writing); } +function prefinish(stream, state) { + if (!state.prefinished) { + state.prefinished = true; + stream.emit('prefinish'); + } +} + function finishMaybe(stream, state) { var need = needFinish(stream, state); if (need) { - state.finished = true; - stream.emit('finish'); + if (state.pendingcb === 0) { + prefinish(stream, state); + state.finished = true; + stream.emit('finish'); + } else + prefinish(stream, state); } return need; } @@ -22423,7 +23059,7 @@ function endWritable(stream, state, cb) { } }).call(this,require('_process')) -},{"./_stream_duplex":169,"_process":167,"buffer":22,"core-util-is":174,"inherits":164,"stream":179}],174:[function(require,module,exports){ +},{"./_stream_duplex":168,"_process":166,"buffer":23,"core-util-is":173,"inherits":163,"stream":178}],173:[function(require,module,exports){ (function (Buffer){ // Copyright Joyent, Inc. and other Node contributors. // @@ -22533,26 +23169,25 @@ function objectToString(o) { return Object.prototype.toString.call(o); } }).call(this,require("buffer").Buffer) -},{"buffer":22}],175:[function(require,module,exports){ +},{"buffer":23}],174:[function(require,module,exports){ module.exports = require("./lib/_stream_passthrough.js") -},{"./lib/_stream_passthrough.js":170}],176:[function(require,module,exports){ -var Stream = require('stream'); // hack to fix a circular dependency issue when used with browserify +},{"./lib/_stream_passthrough.js":169}],175:[function(require,module,exports){ exports = module.exports = require('./lib/_stream_readable.js'); -exports.Stream = Stream; +exports.Stream = require('stream'); exports.Readable = exports; exports.Writable = require('./lib/_stream_writable.js'); exports.Duplex = require('./lib/_stream_duplex.js'); exports.Transform = require('./lib/_stream_transform.js'); exports.PassThrough = require('./lib/_stream_passthrough.js'); -},{"./lib/_stream_duplex.js":169,"./lib/_stream_passthrough.js":170,"./lib/_stream_readable.js":171,"./lib/_stream_transform.js":172,"./lib/_stream_writable.js":173,"stream":179}],177:[function(require,module,exports){ +},{"./lib/_stream_duplex.js":168,"./lib/_stream_passthrough.js":169,"./lib/_stream_readable.js":170,"./lib/_stream_transform.js":171,"./lib/_stream_writable.js":172,"stream":178}],176:[function(require,module,exports){ module.exports = require("./lib/_stream_transform.js") -},{"./lib/_stream_transform.js":172}],178:[function(require,module,exports){ +},{"./lib/_stream_transform.js":171}],177:[function(require,module,exports){ module.exports = require("./lib/_stream_writable.js") -},{"./lib/_stream_writable.js":173}],179:[function(require,module,exports){ +},{"./lib/_stream_writable.js":172}],178:[function(require,module,exports){ // Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a @@ -22681,7 +23316,7 @@ Stream.prototype.pipe = function(dest, options) { return dest; }; -},{"events":163,"inherits":164,"readable-stream/duplex.js":168,"readable-stream/passthrough.js":175,"readable-stream/readable.js":176,"readable-stream/transform.js":177,"readable-stream/writable.js":178}],180:[function(require,module,exports){ +},{"events":162,"inherits":163,"readable-stream/duplex.js":167,"readable-stream/passthrough.js":174,"readable-stream/readable.js":175,"readable-stream/transform.js":176,"readable-stream/writable.js":177}],179:[function(require,module,exports){ // Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a @@ -22904,14 +23539,14 @@ function base64DetectIncompleteChar(buffer) { this.charLength = this.charReceived ? 3 : 0; } -},{"buffer":22}],181:[function(require,module,exports){ +},{"buffer":23}],180:[function(require,module,exports){ module.exports = function isBuffer(arg) { return arg && typeof arg === 'object' && typeof arg.copy === 'function' && typeof arg.fill === 'function' && typeof arg.readUInt8 === 'function'; } -},{}],182:[function(require,module,exports){ +},{}],181:[function(require,module,exports){ (function (process,global){ // Copyright Joyent, Inc. and other Node contributors. // @@ -23501,7 +24136,7 @@ function hasOwnProperty(obj, prop) { } }).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) -},{"./support/isBuffer":181,"_process":167,"inherits":164}],183:[function(require,module,exports){ +},{"./support/isBuffer":180,"_process":166,"inherits":163}],182:[function(require,module,exports){ var indexOf = require('indexof'); var Object_keys = function (obj) { @@ -23641,7 +24276,7 @@ exports.createContext = Script.createContext = function (context) { return copy; }; -},{"indexof":184}],184:[function(require,module,exports){ +},{"indexof":183}],183:[function(require,module,exports){ var indexOf = [].indexOf;