Joi bundle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!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.Joi=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<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | |
},{}],2:[function(require,module,exports){ | |
// http://wiki.commonjs.org/wiki/Unit_Testing/1.0 | |
// | |
// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8! | |
// | |
// Originally from narwhal.js (http://narwhaljs.org) | |
// Copyright (c) 2009 Thomas Robinson <280north.com> | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the 'Software'), to | |
// deal in the Software without restriction, including without limitation the | |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
// sell copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
// when used in node, this will actually load the util module we depend on | |
// versus loading the builtin util module as happens otherwise | |
// this is a bug in node module loading as far as I am concerned | |
var util = require('util/'); | |
var pSlice = Array.prototype.slice; | |
var hasOwn = Object.prototype.hasOwnProperty; | |
// 1. The assert module provides functions that throw | |
// AssertionError's when particular conditions are not met. The | |
// assert module must conform to the following interface. | |
var assert = module.exports = ok; | |
// 2. The AssertionError is defined in assert. | |
// new assert.AssertionError({ message: message, | |
// actual: actual, | |
// expected: expected }) | |
assert.AssertionError = function AssertionError(options) { | |
this.name = 'AssertionError'; | |
this.actual = options.actual; | |
this.expected = options.expected; | |
this.operator = options.operator; | |
if (options.message) { | |
this.message = options.message; | |
this.generatedMessage = false; | |
} else { | |
this.message = getMessage(this); | |
this.generatedMessage = true; | |
} | |
var stackStartFunction = options.stackStartFunction || fail; | |
if (Error.captureStackTrace) { | |
Error.captureStackTrace(this, stackStartFunction); | |
} | |
else { | |
// non v8 browsers so we can have a stacktrace | |
var err = new Error(); | |
if (err.stack) { | |
var out = err.stack; | |
// try to strip useless frames | |
var fn_name = stackStartFunction.name; | |
var idx = out.indexOf('\n' + fn_name); | |
if (idx >= 0) { | |
// once we have located the function frame | |
// we need to strip out everything before it (and its line) | |
var next_line = out.indexOf('\n', idx + 1); | |
out = out.substring(next_line + 1); | |
} | |
this.stack = out; | |
} | |
} | |
}; | |
// assert.AssertionError instanceof Error | |
util.inherits(assert.AssertionError, Error); | |
function replacer(key, value) { | |
if (util.isUndefined(value)) { | |
return '' + value; | |
} | |
if (util.isNumber(value) && (isNaN(value) || !isFinite(value))) { | |
return value.toString(); | |
} | |
if (util.isFunction(value) || util.isRegExp(value)) { | |
return value.toString(); | |
} | |
return value; | |
} | |
function truncate(s, n) { | |
if (util.isString(s)) { | |
return s.length < n ? s : s.slice(0, n); | |
} else { | |
return s; | |
} | |
} | |
function getMessage(self) { | |
return truncate(JSON.stringify(self.actual, replacer), 128) + ' ' + | |
self.operator + ' ' + | |
truncate(JSON.stringify(self.expected, replacer), 128); | |
} | |
// At present only the three keys mentioned above are used and | |
// understood by the spec. Implementations or sub modules can pass | |
// other keys to the AssertionError's constructor - they will be | |
// ignored. | |
// 3. All of the following functions must throw an AssertionError | |
// when a corresponding condition is not met, with a message that | |
// may be undefined if not provided. All assertion methods provide | |
// both the actual and expected values to the assertion error for | |
// display purposes. | |
function fail(actual, expected, message, operator, stackStartFunction) { | |
throw new assert.AssertionError({ | |
message: message, | |
actual: actual, | |
expected: expected, | |
operator: operator, | |
stackStartFunction: stackStartFunction | |
}); | |
} | |
// EXTENSION! allows for well behaved errors defined elsewhere. | |
assert.fail = fail; | |
// 4. Pure assertion tests whether a value is truthy, as determined | |
// by !!guard. | |
// assert.ok(guard, message_opt); | |
// This statement is equivalent to assert.equal(true, !!guard, | |
// message_opt);. To test strictly for the value true, use | |
// assert.strictEqual(true, guard, message_opt);. | |
function ok(value, message) { | |
if (!value) fail(value, true, message, '==', assert.ok); | |
} | |
assert.ok = ok; | |
// 5. The equality assertion tests shallow, coercive equality with | |
// ==. | |
// assert.equal(actual, expected, message_opt); | |
assert.equal = function equal(actual, expected, message) { | |
if (actual != expected) fail(actual, expected, message, '==', assert.equal); | |
}; | |
// 6. The non-equality assertion tests for whether two objects are not equal | |
// with != assert.notEqual(actual, expected, message_opt); | |
assert.notEqual = function notEqual(actual, expected, message) { | |
if (actual == expected) { | |
fail(actual, expected, message, '!=', assert.notEqual); | |
} | |
}; | |
// 7. The equivalence assertion tests a deep equality relation. | |
// assert.deepEqual(actual, expected, message_opt); | |
assert.deepEqual = function deepEqual(actual, expected, message) { | |
if (!_deepEqual(actual, expected)) { | |
fail(actual, expected, message, 'deepEqual', assert.deepEqual); | |
} | |
}; | |
function _deepEqual(actual, expected) { | |
// 7.1. All identical values are equivalent, as determined by ===. | |
if (actual === expected) { | |
return true; | |
} else if (util.isBuffer(actual) && util.isBuffer(expected)) { | |
if (actual.length != expected.length) return false; | |
for (var i = 0; i < actual.length; i++) { | |
if (actual[i] !== expected[i]) return false; | |
} | |
return true; | |
// 7.2. If the expected value is a Date object, the actual value is | |
// equivalent if it is also a Date object that refers to the same time. | |
} else if (util.isDate(actual) && util.isDate(expected)) { | |
return actual.getTime() === expected.getTime(); | |
// 7.3 If the expected value is a RegExp object, the actual value is | |
// equivalent if it is also a RegExp object with the same source and | |
// properties (`global`, `multiline`, `lastIndex`, `ignoreCase`). | |
} else if (util.isRegExp(actual) && util.isRegExp(expected)) { | |
return actual.source === expected.source && | |
actual.global === expected.global && | |
actual.multiline === expected.multiline && | |
actual.lastIndex === expected.lastIndex && | |
actual.ignoreCase === expected.ignoreCase; | |
// 7.4. Other pairs that do not both pass typeof value == 'object', | |
// equivalence is determined by ==. | |
} else if (!util.isObject(actual) && !util.isObject(expected)) { | |
return actual == expected; | |
// 7.5 For all other Object pairs, including Array objects, equivalence is | |
// determined by having the same number of owned properties (as verified | |
// with Object.prototype.hasOwnProperty.call), the same set of keys | |
// (although not necessarily the same order), equivalent values for every | |
// corresponding key, and an identical 'prototype' property. Note: this | |
// accounts for both named and indexed properties on Arrays. | |
} else { | |
return objEquiv(actual, expected); | |
} | |
} | |
function isArguments(object) { | |
return Object.prototype.toString.call(object) == '[object Arguments]'; | |
} | |
function objEquiv(a, b) { | |
if (util.isNullOrUndefined(a) || util.isNullOrUndefined(b)) | |
return false; | |
// an identical 'prototype' property. | |
if (a.prototype !== b.prototype) return false; | |
//~~~I've managed to break Object.keys through screwy arguments passing. | |
// Converting to array solves the problem. | |
if (isArguments(a)) { | |
if (!isArguments(b)) { | |
return false; | |
} | |
a = pSlice.call(a); | |
b = pSlice.call(b); | |
return _deepEqual(a, b); | |
} | |
try { | |
var ka = objectKeys(a), | |
kb = objectKeys(b), | |
key, i; | |
} catch (e) {//happens when one is a string literal and the other isn't | |
return false; | |
} | |
// having the same number of owned properties (keys incorporates | |
// hasOwnProperty) | |
if (ka.length != kb.length) | |
return false; | |
//the same set of keys (although not necessarily the same order), | |
ka.sort(); | |
kb.sort(); | |
//~~~cheap key test | |
for (i = ka.length - 1; i >= 0; i--) { | |
if (ka[i] != kb[i]) | |
return false; | |
} | |
//equivalent values for every corresponding key, and | |
//~~~possibly expensive deep test | |
for (i = ka.length - 1; i >= 0; i--) { | |
key = ka[i]; | |
if (!_deepEqual(a[key], b[key])) return false; | |
} | |
return true; | |
} | |
// 8. The non-equivalence assertion tests for any deep inequality. | |
// assert.notDeepEqual(actual, expected, message_opt); | |
assert.notDeepEqual = function notDeepEqual(actual, expected, message) { | |
if (_deepEqual(actual, expected)) { | |
fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual); | |
} | |
}; | |
// 9. The strict equality assertion tests strict equality, as determined by ===. | |
// assert.strictEqual(actual, expected, message_opt); | |
assert.strictEqual = function strictEqual(actual, expected, message) { | |
if (actual !== expected) { | |
fail(actual, expected, message, '===', assert.strictEqual); | |
} | |
}; | |
// 10. The strict non-equality assertion tests for strict inequality, as | |
// determined by !==. assert.notStrictEqual(actual, expected, message_opt); | |
assert.notStrictEqual = function notStrictEqual(actual, expected, message) { | |
if (actual === expected) { | |
fail(actual, expected, message, '!==', assert.notStrictEqual); | |
} | |
}; | |
function expectedException(actual, expected) { | |
if (!actual || !expected) { | |
return false; | |
} | |
if (Object.prototype.toString.call(expected) == '[object RegExp]') { | |
return expected.test(actual); | |
} else if (actual instanceof expected) { | |
return true; | |
} else if (expected.call({}, actual) === true) { | |
return true; | |
} | |
return false; | |
} | |
function _throws(shouldThrow, block, expected, message) { | |
var actual; | |
if (util.isString(expected)) { | |
message = expected; | |
expected = null; | |
} | |
try { | |
block(); | |
} catch (e) { | |
actual = e; | |
} | |
message = (expected && expected.name ? ' (' + expected.name + ').' : '.') + | |
(message ? ' ' + message : '.'); | |
if (shouldThrow && !actual) { | |
fail(actual, expected, 'Missing expected exception' + message); | |
} | |
if (!shouldThrow && expectedException(actual, expected)) { | |
fail(actual, expected, 'Got unwanted exception' + message); | |
} | |
if ((shouldThrow && actual && expected && | |
!expectedException(actual, expected)) || (!shouldThrow && actual)) { | |
throw actual; | |
} | |
} | |
// 11. Expected to throw an error: | |
// assert.throws(block, Error_opt, message_opt); | |
assert.throws = function(block, /*optional*/error, /*optional*/message) { | |
_throws.apply(this, [true].concat(pSlice.call(arguments))); | |
}; | |
// EXTENSION! This is annoying to write outside this module. | |
assert.doesNotThrow = function(block, /*optional*/message) { | |
_throws.apply(this, [false].concat(pSlice.call(arguments))); | |
}; | |
assert.ifError = function(err) { if (err) {throw err;}}; | |
var objectKeys = Object.keys || function (obj) { | |
var keys = []; | |
for (var key in obj) { | |
if (hasOwn.call(obj, key)) keys.push(key); | |
} | |
return keys; | |
}; | |
},{"util/":134}],3:[function(require,module,exports){ | |
module.exports=require(1) | |
},{"/home/gwarner/portalplug/server/node_modules/browserify/lib/_empty.js":1}],4:[function(require,module,exports){ | |
/*! | |
* The buffer module from node.js, for the browser. | |
* | |
* @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org> | |
* @license MIT | |
*/ | |
var base64 = require('base64-js') | |
var ieee754 = require('ieee754') | |
var isArray = require('is-array') | |
exports.Buffer = Buffer | |
exports.SlowBuffer = Buffer | |
exports.INSPECT_MAX_BYTES = 50 | |
Buffer.poolSize = 8192 // not used by this implementation | |
var kMaxLength = 0x3fffffff | |
/** | |
* If `Buffer.TYPED_ARRAY_SUPPORT`: | |
* === true Use Uint8Array implementation (fastest) | |
* === false Use Object implementation (most compatible, even IE6) | |
* | |
* Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+, | |
* Opera 11.6+, iOS 4.2+. | |
* | |
* Note: | |
* | |
* - Implementation must support adding new properties to `Uint8Array` instances. | |
* Firefox 4-29 lacked support, fixed in Firefox 30+. | |
* See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438. | |
* | |
* - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function. | |
* | |
* - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of | |
* incorrect length in some situations. | |
* | |
* We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they will | |
* get the Object implementation, which is slower but will work correctly. | |
*/ | |
Buffer.TYPED_ARRAY_SUPPORT = (function () { | |
try { | |
var buf = new ArrayBuffer(0) | |
var arr = new Uint8Array(buf) | |
arr.foo = function () { return 42 } | |
return 42 === arr.foo() && // typed array instances can be augmented | |
typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray` | |
new Uint8Array(1).subarray(1, 1).byteLength === 0 // ie10 has broken `subarray` | |
} catch (e) { | |
return false | |
} | |
})() | |
/** | |
* Class: Buffer | |
* ============= | |
* | |
* The Buffer constructor returns instances of `Uint8Array` that are augmented | |
* with function properties for all the node `Buffer` API functions. We use | |
* `Uint8Array` so that square bracket notation works as expected -- it returns | |
* a single octet. | |
* | |
* By augmenting the instances, we can avoid modifying the `Uint8Array` | |
* prototype. | |
*/ | |
function Buffer (subject, encoding, noZero) { | |
if (!(this instanceof Buffer)) | |
return new Buffer(subject, encoding, noZero) | |
var type = typeof subject | |
// Find the length | |
var length | |
if (type === 'number') | |
length = subject > 0 ? subject >>> 0 : 0 | |
else if (type === 'string') { | |
if (encoding === 'base64') | |
subject = base64clean(subject) | |
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 | |
throw new TypeError('must start with number, buffer, array or string') | |
if (this.length > kMaxLength) | |
throw new RangeError('Attempt to allocate Buffer larger than maximum ' + | |
'size: 0x' + kMaxLength.toString(16) + ' bytes') | |
var buf | |
if (Buffer.TYPED_ARRAY_SUPPORT) { | |
// Preferred: Return an augmented `Uint8Array` instance for best performance | |
buf = Buffer._augment(new Uint8Array(length)) | |
} else { | |
// Fallback: Return THIS instance of Buffer (created by `new`) | |
buf = this | |
buf.length = length | |
buf._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) | |
} 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) | |
} else { | |
for (i = 0; i < length; i++) | |
buf[i] = ((subject[i] % 256) + 256) % 256 | |
} | |
} else if (type === 'string') { | |
buf.write(subject, 0, encoding) | |
} else if (type === 'number' && !Buffer.TYPED_ARRAY_SUPPORT && !noZero) { | |
for (i = 0; i < length; i++) { | |
buf[i] = 0 | |
} | |
} | |
return buf | |
} | |
Buffer.isBuffer = function (b) { | |
return !!(b != null && b._isBuffer) | |
} | |
Buffer.compare = function (a, b) { | |
if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) | |
throw new TypeError('Arguments must be Buffers') | |
var x = a.length | |
var y = b.length | |
for (var i = 0, len = Math.min(x, y); i < len && a[i] === b[i]; i++) {} | |
if (i !== len) { | |
x = a[i] | |
y = b[i] | |
} | |
if (x < y) return -1 | |
if (y < x) return 1 | |
return 0 | |
} | |
Buffer.isEncoding = function (encoding) { | |
switch (String(encoding).toLowerCase()) { | |
case 'hex': | |
case 'utf8': | |
case 'utf-8': | |
case 'ascii': | |
case 'binary': | |
case 'base64': | |
case 'raw': | |
case 'ucs2': | |
case 'ucs-2': | |
case 'utf16le': | |
case 'utf-16le': | |
return true | |
default: | |
return false | |
} | |
} | |
Buffer.concat = function (list, totalLength) { | |
if (!isArray(list)) throw new TypeError('Usage: Buffer.concat(list[, length])') | |
if (list.length === 0) { | |
return new Buffer(0) | |
} else if (list.length === 1) { | |
return list[0] | |
} | |
var i | |
if (totalLength === undefined) { | |
totalLength = 0 | |
for (i = 0; i < list.length; i++) { | |
totalLength += list[i].length | |
} | |
} | |
var buf = new Buffer(totalLength) | |
var pos = 0 | |
for (i = 0; i < list.length; i++) { | |
var item = list[i] | |
item.copy(buf, pos) | |
pos += item.length | |
} | |
return buf | |
} | |
Buffer.byteLength = function (str, encoding) { | |
var ret | |
str = str + '' | |
switch (encoding || 'utf8') { | |
case 'ascii': | |
case 'binary': | |
case 'raw': | |
ret = str.length | |
break | |
case 'ucs2': | |
case 'ucs-2': | |
case 'utf16le': | |
case 'utf-16le': | |
ret = str.length * 2 | |
break | |
case 'hex': | |
ret = str.length >>> 1 | |
break | |
case 'utf8': | |
case 'utf-8': | |
ret = utf8ToBytes(str).length | |
break | |
case 'base64': | |
ret = base64ToBytes(str).length | |
break | |
default: | |
ret = str.length | |
} | |
return ret | |
} | |
// pre-set for values that may exist in the future | |
Buffer.prototype.length = undefined | |
Buffer.prototype.parent = undefined | |
// toString(encoding, start=0, end=buffer.length) | |
Buffer.prototype.toString = function (encoding, start, end) { | |
var loweredCase = false | |
start = start >>> 0 | |
end = end === undefined || end === Infinity ? this.length : end >>> 0 | |
if (!encoding) encoding = 'utf8' | |
if (start < 0) start = 0 | |
if (end > this.length) end = this.length | |
if (end <= start) return '' | |
while (true) { | |
switch (encoding) { | |
case 'hex': | |
return hexSlice(this, start, end) | |
case 'utf8': | |
case 'utf-8': | |
return utf8Slice(this, start, end) | |
case 'ascii': | |
return asciiSlice(this, start, end) | |
case 'binary': | |
return binarySlice(this, start, end) | |
case 'base64': | |
return base64Slice(this, start, end) | |
case 'ucs2': | |
case 'ucs-2': | |
case 'utf16le': | |
case 'utf-16le': | |
return utf16leSlice(this, start, end) | |
default: | |
if (loweredCase) | |
throw new TypeError('Unknown encoding: ' + encoding) | |
encoding = (encoding + '').toLowerCase() | |
loweredCase = true | |
} | |
} | |
} | |
Buffer.prototype.equals = function (b) { | |
if(!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') | |
return Buffer.compare(this, b) === 0 | |
} | |
Buffer.prototype.inspect = function () { | |
var str = '' | |
var max = exports.INSPECT_MAX_BYTES | |
if (this.length > 0) { | |
str = this.toString('hex', 0, max).match(/.{2}/g).join(' ') | |
if (this.length > max) | |
str += ' ... ' | |
} | |
return '<Buffer ' + str + '>' | |
} | |
Buffer.prototype.compare = function (b) { | |
if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') | |
return Buffer.compare(this, b) | |
} | |
// `get` will be removed in Node 0.13+ | |
Buffer.prototype.get = function (offset) { | |
console.log('.get() is deprecated. Access using array indexes instead.') | |
return this.readUInt8(offset) | |
} | |
// `set` will be removed in Node 0.13+ | |
Buffer.prototype.set = function (v, offset) { | |
console.log('.set() is deprecated. Access using array indexes instead.') | |
return this.writeUInt8(v, offset) | |
} | |
function hexWrite (buf, string, offset, length) { | |
offset = Number(offset) || 0 | |
var remaining = buf.length - offset | |
if (!length) { | |
length = remaining | |
} else { | |
length = Number(length) | |
if (length > remaining) { | |
length = remaining | |
} | |
} | |
// must be an even number of digits | |
var strLen = string.length | |
if (strLen % 2 !== 0) throw new Error('Invalid hex string') | |
if (length > strLen / 2) { | |
length = strLen / 2 | |
} | |
for (var i = 0; i < length; i++) { | |
var byte = parseInt(string.substr(i * 2, 2), 16) | |
if (isNaN(byte)) throw new Error('Invalid hex string') | |
buf[offset + i] = byte | |
} | |
return i | |
} | |
function utf8Write (buf, string, offset, length) { | |
var charsWritten = blitBuffer(utf8ToBytes(string), buf, offset, length) | |
return charsWritten | |
} | |
function asciiWrite (buf, string, offset, length) { | |
var charsWritten = blitBuffer(asciiToBytes(string), buf, offset, length) | |
return charsWritten | |
} | |
function binaryWrite (buf, string, offset, length) { | |
return asciiWrite(buf, string, offset, length) | |
} | |
function base64Write (buf, string, offset, length) { | |
var charsWritten = blitBuffer(base64ToBytes(string), buf, offset, length) | |
return charsWritten | |
} | |
function utf16leWrite (buf, string, offset, length) { | |
var charsWritten = blitBuffer(utf16leToBytes(string), buf, offset, length) | |
return charsWritten | |
} | |
Buffer.prototype.write = function (string, offset, length, encoding) { | |
// Support both (string, offset, length, encoding) | |
// and the legacy (string, encoding, offset, length) | |
if (isFinite(offset)) { | |
if (!isFinite(length)) { | |
encoding = length | |
length = undefined | |
} | |
} else { // legacy | |
var swap = encoding | |
encoding = offset | |
offset = length | |
length = swap | |
} | |
offset = Number(offset) || 0 | |
var remaining = this.length - offset | |
if (!length) { | |
length = remaining | |
} else { | |
length = Number(length) | |
if (length > remaining) { | |
length = remaining | |
} | |
} | |
encoding = String(encoding || 'utf8').toLowerCase() | |
var ret | |
switch (encoding) { | |
case 'hex': | |
ret = hexWrite(this, string, offset, length) | |
break | |
case 'utf8': | |
case 'utf-8': | |
ret = utf8Write(this, string, offset, length) | |
break | |
case 'ascii': | |
ret = asciiWrite(this, string, offset, length) | |
break | |
case 'binary': | |
ret = binaryWrite(this, string, offset, length) | |
break | |
case 'base64': | |
ret = base64Write(this, string, offset, length) | |
break | |
case 'ucs2': | |
case 'ucs-2': | |
case 'utf16le': | |
case 'utf-16le': | |
ret = utf16leWrite(this, string, offset, length) | |
break | |
default: | |
throw new TypeError('Unknown encoding: ' + encoding) | |
} | |
return ret | |
} | |
Buffer.prototype.toJSON = function () { | |
return { | |
type: 'Buffer', | |
data: Array.prototype.slice.call(this._arr || this, 0) | |
} | |
} | |
function base64Slice (buf, start, end) { | |
if (start === 0 && end === buf.length) { | |
return base64.fromByteArray(buf) | |
} else { | |
return base64.fromByteArray(buf.slice(start, end)) | |
} | |
} | |
function utf8Slice (buf, start, end) { | |
var res = '' | |
var tmp = '' | |
end = Math.min(buf.length, end) | |
for (var i = start; i < end; i++) { | |
if (buf[i] <= 0x7F) { | |
res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i]) | |
tmp = '' | |
} else { | |
tmp += '%' + buf[i].toString(16) | |
} | |
} | |
return res + decodeUtf8Char(tmp) | |
} | |
function asciiSlice (buf, start, end) { | |
var ret = '' | |
end = Math.min(buf.length, end) | |
for (var i = start; i < end; i++) { | |
ret += String.fromCharCode(buf[i]) | |
} | |
return ret | |
} | |
function binarySlice (buf, start, end) { | |
return asciiSlice(buf, start, end) | |
} | |
function hexSlice (buf, start, end) { | |
var len = buf.length | |
if (!start || start < 0) start = 0 | |
if (!end || end < 0 || end > len) end = len | |
var out = '' | |
for (var i = start; i < end; i++) { | |
out += toHex(buf[i]) | |
} | |
return out | |
} | |
function utf16leSlice (buf, start, end) { | |
var bytes = buf.slice(start, end) | |
var res = '' | |
for (var i = 0; i < bytes.length; i += 2) { | |
res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256) | |
} | |
return res | |
} | |
Buffer.prototype.slice = function (start, end) { | |
var len = this.length | |
start = ~~start | |
end = end === undefined ? len : ~~end | |
if (start < 0) { | |
start += len; | |
if (start < 0) | |
start = 0 | |
} else if (start > len) { | |
start = len | |
} | |
if (end < 0) { | |
end += len | |
if (end < 0) | |
end = 0 | |
} else if (end > len) { | |
end = len | |
} | |
if (end < start) | |
end = start | |
if (Buffer.TYPED_ARRAY_SUPPORT) { | |
return Buffer._augment(this.subarray(start, end)) | |
} else { | |
var sliceLen = end - start | |
var newBuf = new Buffer(sliceLen, undefined, true) | |
for (var i = 0; i < sliceLen; i++) { | |
newBuf[i] = this[i + start] | |
} | |
return newBuf | |
} | |
} | |
/* | |
* Need to make sure that buffer isn't trying to write out of bounds. | |
*/ | |
function checkOffset (offset, ext, length) { | |
if ((offset % 1) !== 0 || offset < 0) | |
throw new RangeError('offset is not uint') | |
if (offset + ext > length) | |
throw new RangeError('Trying to access beyond buffer length') | |
} | |
Buffer.prototype.readUInt8 = function (offset, noAssert) { | |
if (!noAssert) | |
checkOffset(offset, 1, this.length) | |
return this[offset] | |
} | |
Buffer.prototype.readUInt16LE = function (offset, noAssert) { | |
if (!noAssert) | |
checkOffset(offset, 2, this.length) | |
return this[offset] | (this[offset + 1] << 8) | |
} | |
Buffer.prototype.readUInt16BE = function (offset, noAssert) { | |
if (!noAssert) | |
checkOffset(offset, 2, this.length) | |
return (this[offset] << 8) | this[offset + 1] | |
} | |
Buffer.prototype.readUInt32LE = function (offset, noAssert) { | |
if (!noAssert) | |
checkOffset(offset, 4, this.length) | |
return ((this[offset]) | | |
(this[offset + 1] << 8) | | |
(this[offset + 2] << 16)) + | |
(this[offset + 3] * 0x1000000) | |
} | |
Buffer.prototype.readUInt32BE = function (offset, noAssert) { | |
if (!noAssert) | |
checkOffset(offset, 4, this.length) | |
return (this[offset] * 0x1000000) + | |
((this[offset + 1] << 16) | | |
(this[offset + 2] << 8) | | |
this[offset + 3]) | |
} | |
Buffer.prototype.readInt8 = function (offset, noAssert) { | |
if (!noAssert) | |
checkOffset(offset, 1, this.length) | |
if (!(this[offset] & 0x80)) | |
return (this[offset]) | |
return ((0xff - this[offset] + 1) * -1) | |
} | |
Buffer.prototype.readInt16LE = function (offset, noAssert) { | |
if (!noAssert) | |
checkOffset(offset, 2, this.length) | |
var val = this[offset] | (this[offset + 1] << 8) | |
return (val & 0x8000) ? val | 0xFFFF0000 : val | |
} | |
Buffer.prototype.readInt16BE = function (offset, noAssert) { | |
if (!noAssert) | |
checkOffset(offset, 2, this.length) | |
var val = this[offset + 1] | (this[offset] << 8) | |
return (val & 0x8000) ? val | 0xFFFF0000 : val | |
} | |
Buffer.prototype.readInt32LE = function (offset, noAssert) { | |
if (!noAssert) | |
checkOffset(offset, 4, this.length) | |
return (this[offset]) | | |
(this[offset + 1] << 8) | | |
(this[offset + 2] << 16) | | |
(this[offset + 3] << 24) | |
} | |
Buffer.prototype.readInt32BE = function (offset, noAssert) { | |
if (!noAssert) | |
checkOffset(offset, 4, this.length) | |
return (this[offset] << 24) | | |
(this[offset + 1] << 16) | | |
(this[offset + 2] << 8) | | |
(this[offset + 3]) | |
} | |
Buffer.prototype.readFloatLE = function (offset, noAssert) { | |
if (!noAssert) | |
checkOffset(offset, 4, this.length) | |
return ieee754.read(this, offset, true, 23, 4) | |
} | |
Buffer.prototype.readFloatBE = function (offset, noAssert) { | |
if (!noAssert) | |
checkOffset(offset, 4, this.length) | |
return ieee754.read(this, offset, false, 23, 4) | |
} | |
Buffer.prototype.readDoubleLE = function (offset, noAssert) { | |
if (!noAssert) | |
checkOffset(offset, 8, this.length) | |
return ieee754.read(this, offset, true, 52, 8) | |
} | |
Buffer.prototype.readDoubleBE = function (offset, noAssert) { | |
if (!noAssert) | |
checkOffset(offset, 8, this.length) | |
return ieee754.read(this, offset, false, 52, 8) | |
} | |
function checkInt (buf, value, offset, ext, max, min) { | |
if (!Buffer.isBuffer(buf)) throw new TypeError('buffer must be a Buffer instance') | |
if (value > max || value < min) throw new TypeError('value is out of bounds') | |
if (offset + ext > buf.length) throw new TypeError('index out of range') | |
} | |
Buffer.prototype.writeUInt8 = function (value, offset, noAssert) { | |
value = +value | |
offset = offset >>> 0 | |
if (!noAssert) | |
checkInt(this, value, offset, 1, 0xff, 0) | |
if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value) | |
this[offset] = value | |
return offset + 1 | |
} | |
function objectWriteUInt16 (buf, value, offset, littleEndian) { | |
if (value < 0) value = 0xffff + value + 1 | |
for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; i++) { | |
buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>> | |
(littleEndian ? i : 1 - i) * 8 | |
} | |
} | |
Buffer.prototype.writeUInt16LE = function (value, offset, noAssert) { | |
value = +value | |
offset = offset >>> 0 | |
if (!noAssert) | |
checkInt(this, value, offset, 2, 0xffff, 0) | |
if (Buffer.TYPED_ARRAY_SUPPORT) { | |
this[offset] = value | |
this[offset + 1] = (value >>> 8) | |
} else objectWriteUInt16(this, value, offset, true) | |
return offset + 2 | |
} | |
Buffer.prototype.writeUInt16BE = function (value, offset, noAssert) { | |
value = +value | |
offset = offset >>> 0 | |
if (!noAssert) | |
checkInt(this, value, offset, 2, 0xffff, 0) | |
if (Buffer.TYPED_ARRAY_SUPPORT) { | |
this[offset] = (value >>> 8) | |
this[offset + 1] = value | |
} else objectWriteUInt16(this, value, offset, false) | |
return offset + 2 | |
} | |
function objectWriteUInt32 (buf, value, offset, littleEndian) { | |
if (value < 0) value = 0xffffffff + value + 1 | |
for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; i++) { | |
buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff | |
} | |
} | |
Buffer.prototype.writeUInt32LE = function (value, offset, noAssert) { | |
value = +value | |
offset = offset >>> 0 | |
if (!noAssert) | |
checkInt(this, value, offset, 4, 0xffffffff, 0) | |
if (Buffer.TYPED_ARRAY_SUPPORT) { | |
this[offset + 3] = (value >>> 24) | |
this[offset + 2] = (value >>> 16) | |
this[offset + 1] = (value >>> 8) | |
this[offset] = value | |
} else objectWriteUInt32(this, value, offset, true) | |
return offset + 4 | |
} | |
Buffer.prototype.writeUInt32BE = function (value, offset, noAssert) { | |
value = +value | |
offset = offset >>> 0 | |
if (!noAssert) | |
checkInt(this, value, offset, 4, 0xffffffff, 0) | |
if (Buffer.TYPED_ARRAY_SUPPORT) { | |
this[offset] = (value >>> 24) | |
this[offset + 1] = (value >>> 16) | |
this[offset + 2] = (value >>> 8) | |
this[offset + 3] = value | |
} else objectWriteUInt32(this, value, offset, false) | |
return offset + 4 | |
} | |
Buffer.prototype.writeInt8 = function (value, offset, noAssert) { | |
value = +value | |
offset = offset >>> 0 | |
if (!noAssert) | |
checkInt(this, value, offset, 1, 0x7f, -0x80) | |
if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value) | |
if (value < 0) value = 0xff + value + 1 | |
this[offset] = value | |
return offset + 1 | |
} | |
Buffer.prototype.writeInt16LE = function (value, offset, noAssert) { | |
value = +value | |
offset = offset >>> 0 | |
if (!noAssert) | |
checkInt(this, value, offset, 2, 0x7fff, -0x8000) | |
if (Buffer.TYPED_ARRAY_SUPPORT) { | |
this[offset] = value | |
this[offset + 1] = (value >>> 8) | |
} else objectWriteUInt16(this, value, offset, true) | |
return offset + 2 | |
} | |
Buffer.prototype.writeInt16BE = function (value, offset, noAssert) { | |
value = +value | |
offset = offset >>> 0 | |
if (!noAssert) | |
checkInt(this, value, offset, 2, 0x7fff, -0x8000) | |
if (Buffer.TYPED_ARRAY_SUPPORT) { | |
this[offset] = (value >>> 8) | |
this[offset + 1] = value | |
} else objectWriteUInt16(this, value, offset, false) | |
return offset + 2 | |
} | |
Buffer.prototype.writeInt32LE = function (value, offset, noAssert) { | |
value = +value | |
offset = offset >>> 0 | |
if (!noAssert) | |
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) | |
if (Buffer.TYPED_ARRAY_SUPPORT) { | |
this[offset] = value | |
this[offset + 1] = (value >>> 8) | |
this[offset + 2] = (value >>> 16) | |
this[offset + 3] = (value >>> 24) | |
} else objectWriteUInt32(this, value, offset, true) | |
return offset + 4 | |
} | |
Buffer.prototype.writeInt32BE = function (value, offset, noAssert) { | |
value = +value | |
offset = offset >>> 0 | |
if (!noAssert) | |
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) | |
if (value < 0) value = 0xffffffff + value + 1 | |
if (Buffer.TYPED_ARRAY_SUPPORT) { | |
this[offset] = (value >>> 24) | |
this[offset + 1] = (value >>> 16) | |
this[offset + 2] = (value >>> 8) | |
this[offset + 3] = value | |
} else objectWriteUInt32(this, value, offset, false) | |
return offset + 4 | |
} | |
function checkIEEE754 (buf, value, offset, ext, max, min) { | |
if (value > max || value < min) throw new TypeError('value is out of bounds') | |
if (offset + ext > buf.length) throw new TypeError('index out of range') | |
} | |
function writeFloat (buf, value, offset, littleEndian, noAssert) { | |
if (!noAssert) | |
checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38) | |
ieee754.write(buf, value, offset, littleEndian, 23, 4) | |
return offset + 4 | |
} | |
Buffer.prototype.writeFloatLE = function (value, offset, noAssert) { | |
return writeFloat(this, value, offset, true, noAssert) | |
} | |
Buffer.prototype.writeFloatBE = function (value, offset, noAssert) { | |
return writeFloat(this, value, offset, false, noAssert) | |
} | |
function writeDouble (buf, value, offset, littleEndian, noAssert) { | |
if (!noAssert) | |
checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308) | |
ieee754.write(buf, value, offset, littleEndian, 52, 8) | |
return offset + 8 | |
} | |
Buffer.prototype.writeDoubleLE = function (value, offset, noAssert) { | |
return writeDouble(this, value, offset, true, noAssert) | |
} | |
Buffer.prototype.writeDoubleBE = function (value, offset, noAssert) { | |
return writeDouble(this, value, offset, false, noAssert) | |
} | |
// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) | |
Buffer.prototype.copy = function (target, target_start, start, end) { | |
var source = this | |
if (!start) start = 0 | |
if (!end && end !== 0) end = this.length | |
if (!target_start) target_start = 0 | |
// Copy 0 bytes; we're done | |
if (end === start) return | |
if (target.length === 0 || source.length === 0) return | |
// Fatal error conditions | |
if (end < start) throw new TypeError('sourceEnd < sourceStart') | |
if (target_start < 0 || target_start >= target.length) | |
throw new TypeError('targetStart out of bounds') | |
if (start < 0 || start >= source.length) throw new TypeError('sourceStart out of bounds') | |
if (end < 0 || end > source.length) throw new TypeError('sourceEnd out of bounds') | |
// Are we oob? | |
if (end > this.length) | |
end = this.length | |
if (target.length - target_start < end - start) | |
end = target.length - target_start + start | |
var len = end - start | |
if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) { | |
for (var i = 0; i < len; i++) { | |
target[i + target_start] = this[i + start] | |
} | |
} else { | |
target._set(this.subarray(start, start + len), target_start) | |
} | |
} | |
// fill(value, start=0, end=buffer.length) | |
Buffer.prototype.fill = function (value, start, end) { | |
if (!value) value = 0 | |
if (!start) start = 0 | |
if (!end) end = this.length | |
if (end < start) throw new TypeError('end < start') | |
// Fill 0 bytes; we're done | |
if (end === start) return | |
if (this.length === 0) return | |
if (start < 0 || start >= this.length) throw new TypeError('start out of bounds') | |
if (end < 0 || end > this.length) throw new TypeError('end out of bounds') | |
var i | |
if (typeof value === 'number') { | |
for (i = start; i < end; i++) { | |
this[i] = value | |
} | |
} else { | |
var bytes = utf8ToBytes(value.toString()) | |
var len = bytes.length | |
for (i = start; i < end; i++) { | |
this[i] = bytes[i % len] | |
} | |
} | |
return this | |
} | |
/** | |
* Creates a new `ArrayBuffer` with the *copied* memory of the buffer instance. | |
* Added in Node 0.12. Only available in browsers that support ArrayBuffer. | |
*/ | |
Buffer.prototype.toArrayBuffer = function () { | |
if (typeof Uint8Array !== 'undefined') { | |
if (Buffer.TYPED_ARRAY_SUPPORT) { | |
return (new Buffer(this)).buffer | |
} else { | |
var buf = new Uint8Array(this.length) | |
for (var i = 0, len = buf.length; i < len; i += 1) { | |
buf[i] = this[i] | |
} | |
return buf.buffer | |
} | |
} else { | |
throw new TypeError('Buffer.toArrayBuffer not supported in this browser') | |
} | |
} | |
// HELPER FUNCTIONS | |
// ================ | |
var BP = Buffer.prototype | |
/** | |
* Augment a Uint8Array *instance* (not the Uint8Array class!) with Buffer methods | |
*/ | |
Buffer._augment = function (arr) { | |
arr.constructor = Buffer | |
arr._isBuffer = true | |
// save reference to original Uint8Array get/set methods before overwriting | |
arr._get = arr.get | |
arr._set = arr.set | |
// deprecated, will be removed in node 0.13+ | |
arr.get = BP.get | |
arr.set = BP.set | |
arr.write = BP.write | |
arr.toString = BP.toString | |
arr.toLocaleString = BP.toString | |
arr.toJSON = BP.toJSON | |
arr.equals = BP.equals | |
arr.compare = BP.compare | |
arr.copy = BP.copy | |
arr.slice = BP.slice | |
arr.readUInt8 = BP.readUInt8 | |
arr.readUInt16LE = BP.readUInt16LE | |
arr.readUInt16BE = BP.readUInt16BE | |
arr.readUInt32LE = BP.readUInt32LE | |
arr.readUInt32BE = BP.readUInt32BE | |
arr.readInt8 = BP.readInt8 | |
arr.readInt16LE = BP.readInt16LE | |
arr.readInt16BE = BP.readInt16BE | |
arr.readInt32LE = BP.readInt32LE | |
arr.readInt32BE = BP.readInt32BE | |
arr.readFloatLE = BP.readFloatLE | |
arr.readFloatBE = BP.readFloatBE | |
arr.readDoubleLE = BP.readDoubleLE | |
arr.readDoubleBE = BP.readDoubleBE | |
arr.writeUInt8 = BP.writeUInt8 | |
arr.writeUInt16LE = BP.writeUInt16LE | |
arr.writeUInt16BE = BP.writeUInt16BE | |
arr.writeUInt32LE = BP.writeUInt32LE | |
arr.writeUInt32BE = BP.writeUInt32BE | |
arr.writeInt8 = BP.writeInt8 | |
arr.writeInt16LE = BP.writeInt16LE | |
arr.writeInt16BE = BP.writeInt16BE | |
arr.writeInt32LE = BP.writeInt32LE | |
arr.writeInt32BE = BP.writeInt32BE | |
arr.writeFloatLE = BP.writeFloatLE | |
arr.writeFloatBE = BP.writeFloatBE | |
arr.writeDoubleLE = BP.writeDoubleLE | |
arr.writeDoubleBE = BP.writeDoubleBE | |
arr.fill = BP.fill | |
arr.inspect = BP.inspect | |
arr.toArrayBuffer = BP.toArrayBuffer | |
return arr | |
} | |
var INVALID_BASE64_RE = /[^+\/0-9A-z]/g | |
function base64clean (str) { | |
// Node strips out invalid characters like \n and \t from the string, base64-js does not | |
str = stringtrim(str).replace(INVALID_BASE64_RE, '') | |
// Node allows for non-padded base64 strings (missing trailing ===), base64-js does not | |
while (str.length % 4 !== 0) { | |
str = str + '=' | |
} | |
return str | |
} | |
function stringtrim (str) { | |
if (str.trim) return str.trim() | |
return str.replace(/^\s+|\s+$/g, '') | |
} | |
function isArrayish (subject) { | |
return isArray(subject) || Buffer.isBuffer(subject) || | |
subject && typeof subject === 'object' && | |
typeof subject.length === 'number' | |
} | |
function toHex (n) { | |
if (n < 16) return '0' + n.toString(16) | |
return n.toString(16) | |
} | |
function utf8ToBytes (str) { | |
var byteArray = [] | |
for (var i = 0; i < str.length; i++) { | |
var b = str.charCodeAt(i) | |
if (b <= 0x7F) { | |
byteArray.push(b) | |
} else { | |
var start = i | |
if (b >= 0xD800 && b <= 0xDFFF) i++ | |
var h = encodeURIComponent(str.slice(start, i+1)).substr(1).split('%') | |
for (var j = 0; j < h.length; j++) { | |
byteArray.push(parseInt(h[j], 16)) | |
} | |
} | |
} | |
return byteArray | |
} | |
function asciiToBytes (str) { | |
var byteArray = [] | |
for (var i = 0; i < str.length; i++) { | |
// Node's code seems to be doing this and not & 0x7F.. | |
byteArray.push(str.charCodeAt(i) & 0xFF) | |
} | |
return byteArray | |
} | |
function utf16leToBytes (str) { | |
var c, hi, lo | |
var byteArray = [] | |
for (var i = 0; i < str.length; i++) { | |
c = str.charCodeAt(i) | |
hi = c >> 8 | |
lo = c % 256 | |
byteArray.push(lo) | |
byteArray.push(hi) | |
} | |
return byteArray | |
} | |
function base64ToBytes (str) { | |
return base64.toByteArray(str) | |
} | |
function blitBuffer (src, dst, offset, length) { | |
for (var i = 0; i < length; i++) { | |
if ((i + offset >= dst.length) || (i >= src.length)) | |
break | |
dst[i + offset] = src[i] | |
} | |
return i | |
} | |
function decodeUtf8Char (str) { | |
try { | |
return decodeURIComponent(str) | |
} catch (err) { | |
return String.fromCharCode(0xFFFD) // UTF 8 invalid char | |
} | |
} | |
},{"base64-js":5,"ieee754":6,"is-array":7}],5:[function(require,module,exports){ | |
var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | |
;(function (exports) { | |
'use strict'; | |
var Arr = (typeof Uint8Array !== 'undefined') | |
? Uint8Array | |
: Array | |
var PLUS = '+'.charCodeAt(0) | |
var SLASH = '/'.charCodeAt(0) | |
var NUMBER = '0'.charCodeAt(0) | |
var LOWER = 'a'.charCodeAt(0) | |
var UPPER = 'A'.charCodeAt(0) | |
function decode (elt) { | |
var code = elt.charCodeAt(0) | |
if (code === PLUS) | |
return 62 // '+' | |
if (code === SLASH) | |
return 63 // '/' | |
if (code < NUMBER) | |
return -1 //no match | |
if (code < NUMBER + 10) | |
return code - NUMBER + 26 + 26 | |
if (code < UPPER + 26) | |
return code - UPPER | |
if (code < LOWER + 26) | |
return code - LOWER + 26 | |
} | |
function b64ToByteArray (b64) { | |
var i, j, l, tmp, placeHolders, arr | |
if (b64.length % 4 > 0) { | |
throw new Error('Invalid string. Length must be a multiple of 4') | |
} | |
// the number of equal signs (place holders) | |
// if there are two placeholders, than the two characters before it | |
// represent one byte | |
// if there is only one, then the three characters before it represent 2 bytes | |
// this is just a cheap hack to not do indexOf twice | |
var len = b64.length | |
placeHolders = '=' === b64.charAt(len - 2) ? 2 : '=' === b64.charAt(len - 1) ? 1 : 0 | |
// base64 is 4/3 + up to two characters of the original data | |
arr = new Arr(b64.length * 3 / 4 - placeHolders) | |
// if there are placeholders, only get up to the last complete 4 chars | |
l = placeHolders > 0 ? b64.length - 4 : b64.length | |
var L = 0 | |
function push (v) { | |
arr[L++] = v | |
} | |
for (i = 0, j = 0; i < l; i += 4, j += 3) { | |
tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3)) | |
push((tmp & 0xFF0000) >> 16) | |
push((tmp & 0xFF00) >> 8) | |
push(tmp & 0xFF) | |
} | |
if (placeHolders === 2) { | |
tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4) | |
push(tmp & 0xFF) | |
} else if (placeHolders === 1) { | |
tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2) | |
push((tmp >> 8) & 0xFF) | |
push(tmp & 0xFF) | |
} | |
return arr | |
} | |
function uint8ToBase64 (uint8) { | |
var i, | |
extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes | |
output = "", | |
temp, length | |
function encode (num) { | |
return lookup.charAt(num) | |
} | |
function tripletToBase64 (num) { | |
return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F) | |
} | |
// go through the array every three bytes, we'll deal with trailing stuff later | |
for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) { | |
temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]) | |
output += tripletToBase64(temp) | |
} | |
// pad the end with zeros, but make sure to not forget the extra bytes | |
switch (extraBytes) { | |
case 1: | |
temp = uint8[uint8.length - 1] | |
output += encode(temp >> 2) | |
output += encode((temp << 4) & 0x3F) | |
output += '==' | |
break | |
case 2: | |
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]) | |
output += encode(temp >> 10) | |
output += encode((temp >> 4) & 0x3F) | |
output += encode((temp << 2) & 0x3F) | |
output += '=' | |
break | |
} | |
return output | |
} | |
exports.toByteArray = b64ToByteArray | |
exports.fromByteArray = uint8ToBase64 | |
}(typeof exports === 'undefined' ? (this.base64js = {}) : exports)) | |
},{}],6:[function(require,module,exports){ | |
exports.read = function(buffer, offset, isLE, mLen, nBytes) { | |
var e, m, | |
eLen = nBytes * 8 - mLen - 1, | |
eMax = (1 << eLen) - 1, | |
eBias = eMax >> 1, | |
nBits = -7, | |
i = isLE ? (nBytes - 1) : 0, | |
d = isLE ? -1 : 1, | |
s = buffer[offset + i]; | |
i += d; | |
e = s & ((1 << (-nBits)) - 1); | |
s >>= (-nBits); | |
nBits += eLen; | |
for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8); | |
m = e & ((1 << (-nBits)) - 1); | |
e >>= (-nBits); | |
nBits += mLen; | |
for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8); | |
if (e === 0) { | |
e = 1 - eBias; | |
} else if (e === eMax) { | |
return m ? NaN : ((s ? -1 : 1) * Infinity); | |
} else { | |
m = m + Math.pow(2, mLen); | |
e = e - eBias; | |
} | |
return (s ? -1 : 1) * m * Math.pow(2, e - mLen); | |
}; | |
exports.write = function(buffer, value, offset, isLE, mLen, nBytes) { | |
var e, m, c, | |
eLen = nBytes * 8 - mLen - 1, | |
eMax = (1 << eLen) - 1, | |
eBias = eMax >> 1, | |
rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0), | |
i = isLE ? 0 : (nBytes - 1), | |
d = isLE ? 1 : -1, | |
s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0; | |
value = Math.abs(value); | |
if (isNaN(value) || value === Infinity) { | |
m = isNaN(value) ? 1 : 0; | |
e = eMax; | |
} else { | |
e = Math.floor(Math.log(value) / Math.LN2); | |
if (value * (c = Math.pow(2, -e)) < 1) { | |
e--; | |
c *= 2; | |
} | |
if (e + eBias >= 1) { | |
value += rt / c; | |
} else { | |
value += rt * Math.pow(2, 1 - eBias); | |
} | |
if (value * c >= 2) { | |
e++; | |
c /= 2; | |
} | |
if (e + eBias >= eMax) { | |
m = 0; | |
e = eMax; | |
} else if (e + eBias >= 1) { | |
m = (value * c - 1) * Math.pow(2, mLen); | |
e = e + eBias; | |
} else { | |
m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen); | |
e = 0; | |
} | |
} | |
for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8); | |
e = (e << mLen) | m; | |
eLen += mLen; | |
for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8); | |
buffer[offset + i - d] |= s * 128; | |
}; | |
},{}],7:[function(require,module,exports){ | |
/** | |
* isArray | |
*/ | |
var isArray = Array.isArray; | |
/** | |
* toString | |
*/ | |
var str = Object.prototype.toString; | |
/** | |
* Whether or not the given `val` | |
* is an array. | |
* | |
* example: | |
* | |
* isArray([]); | |
* // > true | |
* isArray(arguments); | |
* // > false | |
* isArray(''); | |
* // > false | |
* | |
* @param {mixed} val | |
* @return {bool} | |
*/ | |
module.exports = isArray || function (val) { | |
return !! val && '[object Array]' == str.call(val); | |
}; | |
},{}],8:[function(require,module,exports){ | |
(function (Buffer){ | |
'use strict'; | |
var createHash = require('sha.js') | |
var md5 = require('./md5') | |
var rmd160 = require('ripemd160') | |
var Transform = require('stream').Transform; | |
var inherits = require('util').inherits | |
module.exports = function (alg) { | |
if('md5' === alg) return new HashNoConstructor(md5) | |
if('rmd160' === alg) return new HashNoConstructor(rmd160) | |
return new Hash(createHash(alg)) | |
} | |
inherits(HashNoConstructor, Transform) | |
function HashNoConstructor(hash) { | |
Transform.call(this); | |
this._hash = hash | |
this.buffers = [] | |
} | |
HashNoConstructor.prototype._transform = function (data, _, done) { | |
this.buffers.push(data) | |
done() | |
} | |
HashNoConstructor.prototype._flush = function (done) { | |
var buf = Buffer.concat(this.buffers) | |
var r = this._hash(buf) | |
this.buffers = null | |
this.push(r) | |
done() | |
} | |
HashNoConstructor.prototype.update = function (data, enc) { | |
this.write(data, enc) | |
return this | |
} | |
HashNoConstructor.prototype.digest = function (enc) { | |
this.end() | |
var outData = new Buffer('') | |
var chunk | |
while ((chunk = this.read())) { | |
outData = Buffer.concat([outData, chunk]) | |
} | |
if (enc) { | |
outData = outData.toString(enc) | |
} | |
return outData | |
} | |
inherits(Hash, Transform) | |
function Hash(hash) { | |
Transform.call(this); | |
this._hash = hash | |
} | |
Hash.prototype._transform = function (data, _, done) { | |
this._hash.update(data) | |
done() | |
} | |
Hash.prototype._flush = function (done) { | |
this.push(this._hash.digest()) | |
this._hash = null | |
done() | |
} | |
Hash.prototype.update = function (data, enc) { | |
this.write(data, enc) | |
return this | |
} | |
Hash.prototype.digest = function (enc) { | |
this.end() | |
var outData = new Buffer('') | |
var chunk | |
while ((chunk = this.read())) { | |
outData = Buffer.concat([outData, chunk]) | |
} | |
if (enc) { | |
outData = outData.toString(enc) | |
} | |
return outData | |
} | |
}).call(this,require("buffer").Buffer) | |
},{"./md5":12,"buffer":4,"ripemd160":105,"sha.js":107,"stream":131,"util":134}],9:[function(require,module,exports){ | |
(function (Buffer){ | |
'use strict'; | |
var createHash = require('./create-hash') | |
var Transform = require('stream').Transform; | |
var inherits = require('util').inherits | |
var zeroBuffer = new Buffer(128) | |
zeroBuffer.fill(0) | |
module.exports = Hmac | |
inherits(Hmac, Transform) | |
function Hmac (alg, key) { | |
if(!(this instanceof Hmac)) return new Hmac(alg, key) | |
Transform.call(this) | |
this._opad = opad | |
this._alg = alg | |
var blocksize = (alg === 'sha512' || alg === 'sha384') ? 128 : 64 | |
key = this._key = !Buffer.isBuffer(key) ? new Buffer(key) : key | |
if(key.length > blocksize) { | |
key = createHash(alg).update(key).digest() | |
} else if(key.length < blocksize) { | |
key = Buffer.concat([key, zeroBuffer], blocksize) | |
} | |
var ipad = this._ipad = new Buffer(blocksize) | |
var opad = this._opad = new Buffer(blocksize) | |
for(var i = 0; i < blocksize; i++) { | |
ipad[i] = key[i] ^ 0x36 | |
opad[i] = key[i] ^ 0x5C | |
} | |
this._hash = createHash(alg).update(ipad) | |
} | |
Hmac.prototype.update = function (data, enc) { | |
this.write(data, enc) | |
return this | |
} | |
Hmac.prototype._transform = function (data, _, next) { | |
this._hash.update(data) | |
next() | |
} | |
Hmac.prototype._flush = function (next) { | |
var h = this._hash.digest() | |
this.push(createHash(this._alg).update(this._opad).update(h).digest()) | |
next() | |
} | |
Hmac.prototype.digest = function (enc) { | |
this.end() | |
var outData = new Buffer('') | |
var chunk | |
while ((chunk = this.read())) { | |
outData = Buffer.concat([outData, chunk]) | |
} | |
if (enc) { | |
outData = outData.toString(enc) | |
} | |
return outData | |
} | |
}).call(this,require("buffer").Buffer) | |
},{"./create-hash":8,"buffer":4,"stream":131,"util":134}],10:[function(require,module,exports){ | |
(function (Buffer){ | |
'use strict'; | |
var intSize = 4; | |
var zeroBuffer = new Buffer(intSize); zeroBuffer.fill(0); | |
var chrsz = 8; | |
function toArray(buf, bigEndian) { | |
if ((buf.length % intSize) !== 0) { | |
var len = buf.length + (intSize - (buf.length % intSize)); | |
buf = Buffer.concat([buf, zeroBuffer], len); | |
} | |
var arr = []; | |
var fn = bigEndian ? buf.readInt32BE : buf.readInt32LE; | |
for (var i = 0; i < buf.length; i += intSize) { | |
arr.push(fn.call(buf, i)); | |
} | |
return arr; | |
} | |
function toBuffer(arr, size, bigEndian) { | |
var buf = new Buffer(size); | |
var fn = bigEndian ? buf.writeInt32BE : buf.writeInt32LE; | |
for (var i = 0; i < arr.length; i++) { | |
fn.call(buf, arr[i], i * 4, true); | |
} | |
return buf; | |
} | |
function hash(buf, fn, hashSize, bigEndian) { | |
if (!Buffer.isBuffer(buf)) buf = new Buffer(buf); | |
var arr = fn(toArray(buf, bigEndian), buf.length * chrsz); | |
return toBuffer(arr, hashSize, bigEndian); | |
} | |
module.exports = { hash: hash }; | |
}).call(this,require("buffer").Buffer) | |
},{"buffer":4}],11:[function(require,module,exports){ | |
(function (Buffer){ | |
'use strict'; | |
var rng = require('./rng') | |
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 = require('./create-hash') | |
exports.createHmac = require('./create-hmac') | |
exports.randomBytes = function(size, callback) { | |
if (callback && callback.call) { | |
try { | |
callback.call(this, undefined, new Buffer(rng(size))) | |
} catch (err) { callback(err) } | |
} else { | |
return new Buffer(rng(size)) | |
} | |
} | |
function each(a, f) { | |
for(var i in a) | |
f(a[i], i) | |
} | |
var hashes = ['sha1', 'sha256', 'sha512', 'md5', 'rmd160'].concat(Object.keys(require('browserify-sign/algos'))) | |
exports.getHashes = function () { | |
return hashes; | |
} | |
var p = require('./pbkdf2')(exports) | |
exports.pbkdf2 = p.pbkdf2 | |
exports.pbkdf2Sync = p.pbkdf2Sync | |
require('browserify-aes/inject')(exports, module.exports); | |
require('browserify-sign/inject')(module.exports, exports); | |
require('diffie-hellman/inject')(exports, module.exports); | |
require('create-ecdh/inject')(module.exports, exports); | |
// the least I can do is make error messages for the rest of the node.js/crypto api. | |
each([ | |
'createCredentials', 'publicEncrypt', 'privateDecrypt' | |
], function (name) { | |
exports[name] = function () { | |
error('sorry,', name, 'is not implemented yet') | |
} | |
}) | |
}).call(this,require("buffer").Buffer) | |
},{"./create-hash":8,"./create-hmac":9,"./pbkdf2":113,"./rng":114,"browserify-aes/inject":20,"browserify-sign/algos":32,"browserify-sign/inject":35,"buffer":4,"create-ecdh/inject":75,"diffie-hellman/inject":99}],12:[function(require,module,exports){ | |
'use strict'; | |
/* | |
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message | |
* Digest Algorithm, as defined in RFC 1321. | |
* Version 2.1 Copyright (C) Paul Johnston 1999 - 2002. | |
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet | |
* Distributed under the BSD License | |
* See http://pajhome.org.uk/crypt/md5 for more info. | |
*/ | |
var helpers = require('./helpers'); | |
/* | |
* Calculate the MD5 of an array of little-endian words, and a bit length | |
*/ | |
function core_md5(x, len) | |
{ | |
/* append padding */ | |
x[len >> 5] |= 0x80 << ((len) % 32); | |
x[(((len + 64) >>> 9) << 4) + 14] = len; | |
var a = 1732584193; | |
var b = -271733879; | |
var c = -1732584194; | |
var d = 271733878; | |
for(var i = 0; i < x.length; i += 16) | |
{ | |
var olda = a; | |
var oldb = b; | |
var oldc = c; | |
var oldd = d; | |
a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936); | |
d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586); | |
c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819); | |
b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330); | |
a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897); | |
d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426); | |
c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341); | |
b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983); | |
a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416); | |
d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417); | |
c = md5_ff(c, d, a, b, x[i+10], 17, -42063); | |
b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162); | |
a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682); | |
d = md5_ff(d, a, b, c, x[i+13], 12, -40341101); | |
c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290); | |
b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329); | |
a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510); | |
d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632); | |
c = md5_gg(c, d, a, b, x[i+11], 14, 643717713); | |
b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302); | |
a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691); | |
d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083); | |
c = md5_gg(c, d, a, b, x[i+15], 14, -660478335); | |
b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848); | |
a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438); | |
d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690); | |
c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961); | |
b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501); | |
a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467); | |
d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784); | |
c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473); | |
b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734); | |
a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558); | |
d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463); | |
c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562); | |
b = md5_hh(b, c, d, a, x[i+14], 23, -35309556); | |
a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060); | |
d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353); | |
c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632); | |
b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640); | |
a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174); | |
d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222); | |
c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979); | |
b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189); | |
a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487); | |
d = md5_hh(d, a, b, c, x[i+12], 11, -421815835); | |
c = md5_hh(c, d, a, b, x[i+15], 16, 530742520); | |
b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651); | |
a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844); | |
d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415); | |
c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905); | |
b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055); | |
a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571); | |
d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606); | |
c = md5_ii(c, d, a, b, x[i+10], 15, -1051523); | |
b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799); | |
a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359); | |
d = md5_ii(d, a, b, c, x[i+15], 10, -30611744); | |
c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380); | |
b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649); | |
a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070); | |
d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379); | |
c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259); | |
b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551); | |
a = safe_add(a, olda); | |
b = safe_add(b, oldb); | |
c = safe_add(c, oldc); | |
d = safe_add(d, oldd); | |
} | |
return Array(a, b, c, d); | |
} | |
/* | |
* These functions implement the four basic operations the algorithm uses. | |
*/ | |
function md5_cmn(q, a, b, x, s, t) | |
{ | |
return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b); | |
} | |
function md5_ff(a, b, c, d, x, s, t) | |
{ | |
return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t); | |
} | |
function md5_gg(a, b, c, d, x, s, t) | |
{ | |
return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t); | |
} | |
function md5_hh(a, b, c, d, x, s, t) | |
{ | |
return md5_cmn(b ^ c ^ d, a, b, x, s, t); | |
} | |
function md5_ii(a, b, c, d, x, s, t) | |
{ | |
return md5_cmn(c ^ (b | (~d)), a, b, x, s, t); | |
} | |
/* | |
* Add integers, wrapping at 2^32. This uses 16-bit operations internally | |
* to work around bugs in some JS interpreters. | |
*/ | |
function safe_add(x, y) | |
{ | |
var lsw = (x & 0xFFFF) + (y & 0xFFFF); | |
var msw = (x >> 16) + (y >> 16) + (lsw >> 16); | |
return (msw << 16) | (lsw & 0xFFFF); | |
} | |
/* | |
* Bitwise rotate a 32-bit number to the left. | |
*/ | |
function bit_rol(num, cnt) | |
{ | |
return (num << cnt) | (num >>> (32 - cnt)); | |
} | |
module.exports = function md5(buf) { | |
return helpers.hash(buf, core_md5, 16); | |
}; | |
},{"./helpers":10}],13:[function(require,module,exports){ | |
(function (Buffer){ | |
module.exports = function (crypto, password, keyLen, ivLen) { | |
keyLen = keyLen/8; | |
ivLen = ivLen || 0; | |
var ki = 0; | |
var ii = 0; | |
var key = new Buffer(keyLen); | |
var iv = new Buffer(ivLen); | |
var addmd = 0; | |
var md, md_buf; | |
var i; | |
while (true) { | |
md = crypto.createHash('md5'); | |
if(addmd++ > 0) { | |
md.update(md_buf); | |
} | |
md.update(password); | |
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--; | |
i++; | |
} | |
} | |
if(ivLen > 0 && i !== md_buf.length) { | |
while(true) { | |
if(ivLen === 0) { | |
break; | |
} | |
if(i === md_buf.length) { | |
break; | |
} | |
iv[ii++] = md_buf[i]; | |
ivLen--; | |
i++; | |
} | |
} | |
if(keyLen === 0 && ivLen === 0) { | |
break; | |
} | |
} | |
for(i=0;i<md_buf.length;i++) { | |
md_buf[i] = 0; | |
} | |
return { | |
key: key, | |
iv: iv | |
}; | |
}; | |
}).call(this,require("buffer").Buffer) | |
},{"buffer":4}],14:[function(require,module,exports){ | |
(function (Buffer){ | |
var uint_max = Math.pow(2, 32); | |
function fixup_uint32(x) { | |
var ret, x_pos; | |
ret = x > uint_max || x < 0 ? (x_pos = Math.abs(x) % uint_max, x < 0 ? uint_max - x_pos : x_pos) : x; | |
return ret; | |
} | |
function scrub_vec(v) { | |
var i, _i, _ref; | |
for (i = _i = 0, _ref = v.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { | |
v[i] = 0; | |
} | |
return false; | |
} | |
function Global() { | |
var i; | |
this.SBOX = []; | |
this.INV_SBOX = []; | |
this.SUB_MIX = (function() { | |
var _i, _results; | |
_results = []; | |
for (i = _i = 0; _i < 4; i = ++_i) { | |
_results.push([]); | |
} | |
return _results; | |
})(); | |
this.INV_SUB_MIX = (function() { | |
var _i, _results; | |
_results = []; | |
for (i = _i = 0; _i < 4; i = ++_i) { | |
_results.push([]); | |
} | |
return _results; | |
})(); | |
this.init(); | |
this.RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36]; | |
} | |
Global.prototype.init = function() { | |
var d, i, sx, t, x, x2, x4, x8, xi, _i; | |
d = (function() { | |
var _i, _results; | |
_results = []; | |
for (i = _i = 0; _i < 256; i = ++_i) { | |
if (i < 128) { | |
_results.push(i << 1); | |
} else { | |
_results.push((i << 1) ^ 0x11b); | |
} | |
} | |
return _results; | |
})(); | |
x = 0; | |
xi = 0; | |
for (i = _i = 0; _i < 256; i = ++_i) { | |
sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4); | |
sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63; | |
this.SBOX[x] = sx; | |
this.INV_SBOX[sx] = x; | |
x2 = d[x]; | |
x4 = d[x2]; | |
x8 = d[x4]; | |
t = (d[sx] * 0x101) ^ (sx * 0x1010100); | |
this.SUB_MIX[0][x] = (t << 24) | (t >>> 8); | |
this.SUB_MIX[1][x] = (t << 16) | (t >>> 16); | |
this.SUB_MIX[2][x] = (t << 8) | (t >>> 24); | |
this.SUB_MIX[3][x] = t; | |
t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100); | |
this.INV_SUB_MIX[0][sx] = (t << 24) | (t >>> 8); | |
this.INV_SUB_MIX[1][sx] = (t << 16) | (t >>> 16); | |
this.INV_SUB_MIX[2][sx] = (t << 8) | (t >>> 24); | |
this.INV_SUB_MIX[3][sx] = t; | |
if (x === 0) { | |
x = xi = 1; | |
} else { | |
x = x2 ^ d[d[d[x8 ^ x2]]]; | |
xi ^= d[d[xi]]; | |
} | |
} | |
return true; | |
}; | |
var G = new Global(); | |
AES.blockSize = 4 * 4; | |
AES.prototype.blockSize = AES.blockSize; | |
AES.keySize = 256 / 8; | |
AES.prototype.keySize = AES.keySize; | |
AES.ivSize = AES.blockSize; | |
AES.prototype.ivSize = AES.ivSize; | |
function bufferToArray(buf) { | |
var len = buf.length/4; | |
var out = new Array(len); | |
var i = -1; | |
while (++i < len) { | |
out[i] = buf.readUInt32BE(i * 4); | |
} | |
return out; | |
} | |
function AES(key) { | |
this._key = bufferToArray(key); | |
this._doReset(); | |
} | |
AES.prototype._doReset = function() { | |
var invKsRow, keySize, keyWords, ksRow, ksRows, t, _i, _j; | |
keyWords = this._key; | |
keySize = keyWords.length; | |
this._nRounds = keySize + 6; | |
ksRows = (this._nRounds + 1) * 4; | |
this._keySchedule = []; | |
for (ksRow = _i = 0; 0 <= ksRows ? _i < ksRows : _i > ksRows; ksRow = 0 <= ksRows ? ++_i : --_i) { | |
this._keySchedule[ksRow] = ksRow < keySize ? keyWords[ksRow] : (t = this._keySchedule[ksRow - 1], (ksRow % keySize) === 0 ? (t = (t << 8) | (t >>> 24), t = (G.SBOX[t >>> 24] << 24) | (G.SBOX[(t >>> 16) & 0xff] << 16) | (G.SBOX[(t >>> 8) & 0xff] << 8) | G.SBOX[t & 0xff], t ^= G.RCON[(ksRow / keySize) | 0] << 24) : keySize > 6 && ksRow % keySize === 4 ? t = (G.SBOX[t >>> 24] << 24) | (G.SBOX[(t >>> 16) & 0xff] << 16) | (G.SBOX[(t >>> 8) & 0xff] << 8) | G.SBOX[t & 0xff] : void 0, this._keySchedule[ksRow - keySize] ^ t); | |
} | |
this._invKeySchedule = []; | |
for (invKsRow = _j = 0; 0 <= ksRows ? _j < ksRows : _j > ksRows; invKsRow = 0 <= ksRows ? ++_j : --_j) { | |
ksRow = ksRows - invKsRow; | |
t = this._keySchedule[ksRow - (invKsRow % 4 ? 0 : 4)]; | |
this._invKeySchedule[invKsRow] = invKsRow < 4 || ksRow <= 4 ? t : G.INV_SUB_MIX[0][G.SBOX[t >>> 24]] ^ G.INV_SUB_MIX[1][G.SBOX[(t >>> 16) & 0xff]] ^ G.INV_SUB_MIX[2][G.SBOX[(t >>> 8) & 0xff]] ^ G.INV_SUB_MIX[3][G.SBOX[t & 0xff]]; | |
} | |
return true; | |
}; | |
AES.prototype.encryptBlock = function(M) { | |
M = bufferToArray(new Buffer(M)); | |
var out = this._doCryptBlock(M, this._keySchedule, G.SUB_MIX, G.SBOX); | |
var buf = new Buffer(16); | |
buf.writeUInt32BE(out[0], 0); | |
buf.writeUInt32BE(out[1], 4); | |
buf.writeUInt32BE(out[2], 8); | |
buf.writeUInt32BE(out[3], 12); | |
return buf; | |
}; | |
AES.prototype.decryptBlock = function(M) { | |
M = bufferToArray(new Buffer(M)); | |
var temp = [M[3], M[1]]; | |
M[1] = temp[0]; | |
M[3] = temp[1]; | |
var out = this._doCryptBlock(M, this._invKeySchedule, G.INV_SUB_MIX, G.INV_SBOX); | |
var buf = new Buffer(16); | |
buf.writeUInt32BE(out[0], 0); | |
buf.writeUInt32BE(out[3], 4); | |
buf.writeUInt32BE(out[2], 8); | |
buf.writeUInt32BE(out[1], 12); | |
return buf; | |
}; | |
AES.prototype.scrub = function() { | |
scrub_vec(this._keySchedule); | |
scrub_vec(this._invKeySchedule); | |
scrub_vec(this._key); | |
}; | |
AES.prototype._doCryptBlock = function(M, keySchedule, SUB_MIX, SBOX) { | |
var ksRow, round, s0, s1, s2, s3, t0, t1, t2, t3, _i, _ref; | |
s0 = M[0] ^ keySchedule[0]; | |
s1 = M[1] ^ keySchedule[1]; | |
s2 = M[2] ^ keySchedule[2]; | |
s3 = M[3] ^ keySchedule[3]; | |
ksRow = 4; | |
for (round = _i = 1, _ref = this._nRounds; 1 <= _ref ? _i < _ref : _i > _ref; round = 1 <= _ref ? ++_i : --_i) { | |
t0 = SUB_MIX[0][s0 >>> 24] ^ SUB_MIX[1][(s1 >>> 16) & 0xff] ^ SUB_MIX[2][(s2 >>> 8) & 0xff] ^ SUB_MIX[3][s3 & 0xff] ^ keySchedule[ksRow++]; | |
t1 = SUB_MIX[0][s1 >>> 24] ^ SUB_MIX[1][(s2 >>> 16) & 0xff] ^ SUB_MIX[2][(s3 >>> 8) & 0xff] ^ SUB_MIX[3][s0 & 0xff] ^ keySchedule[ksRow++]; | |
t2 = SUB_MIX[0][s2 >>> 24] ^ SUB_MIX[1][(s3 >>> 16) & 0xff] ^ SUB_MIX[2][(s0 >>> 8) & 0xff] ^ SUB_MIX[3][s1 & 0xff] ^ keySchedule[ksRow++]; | |
t3 = SUB_MIX[0][s3 >>> 24] ^ SUB_MIX[1][(s0 >>> 16) & 0xff] ^ SUB_MIX[2][(s1 >>> 8) & 0xff] ^ SUB_MIX[3][s2 & 0xff] ^ keySchedule[ksRow++]; | |
s0 = t0; | |
s1 = t1; | |
s2 = t2; | |
s3 = t3; | |
} | |
t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++]; | |
t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++]; | |
t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++]; | |
t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++]; | |
return [ | |
fixup_uint32(t0), | |
fixup_uint32(t1), | |
fixup_uint32(t2), | |
fixup_uint32(t3) | |
]; | |
}; | |
exports.AES = AES; | |
}).call(this,require("buffer").Buffer) | |
},{"buffer":4}],15:[function(require,module,exports){ | |
(function (Buffer){ | |
var aes = require('./aes'); | |
var Transform = require('./cipherBase'); | |
var inherits = require('inherits'); | |
var GHASH = require('./ghash'); | |
var xor = require('./xor'); | |
inherits(StreamCipher, Transform); | |
module.exports = StreamCipher; | |
function StreamCipher(mode, key, iv, decrypt) { | |
if (!(this instanceof StreamCipher)) { | |
return new StreamCipher(mode, key, iv); | |
} | |
Transform.call(this); | |
this._finID = Buffer.concat([iv, new Buffer([0, 0, 0, 1])]); | |
iv = Buffer.concat([iv, new Buffer([0, 0, 0, 2])]); | |
this._cipher = new aes.AES(key); | |
this._prev = new Buffer(iv.length); | |
this._cache = new Buffer(''); | |
this._secCache = new Buffer(''); | |
this._decrypt = decrypt; | |
this._alen = 0; | |
this._len = 0; | |
iv.copy(this._prev); | |
this._mode = mode; | |
var h = new Buffer(4); | |
h.fill(0); | |
this._ghash = new GHASH(this._cipher.encryptBlock(h)); | |
this._authTag = null; | |
this._called = false; | |
} | |
StreamCipher.prototype._transform = function (chunk, _, next) { | |
if (!this._called && this._alen) { | |
var rump = 16 - (this._alen % 16); | |
if (rump <16) { | |
rump = new Buffer(rump); | |
rump.fill(0); | |
this._ghash.update(rump); | |
} | |
} | |
this._called = true; | |
var out = this._mode.encrypt(this, chunk); | |
if (this._decrypt) { | |
this._ghash.update(chunk); | |
} else { | |
this._ghash.update(out); | |
} | |
this._len += chunk.length; | |
next(null, out); | |
}; | |
StreamCipher.prototype._flush = function (next) { | |
if (this._decrypt && !this._authTag) { | |
throw new Error('Unsupported state or unable to authenticate data'); | |
} | |
var tag = xor(this._ghash.final(this._alen * 8, this._len * 8), this._cipher.encryptBlock(this._finID)); | |
if (this._decrypt) { | |
if (xorTest(tag, this._authTag)) { | |
throw new Error('Unsupported state or unable to authenticate data'); | |
} | |
} else { | |
this._authTag = tag; | |
} | |
this._cipher.scrub(); | |
next(); | |
}; | |
StreamCipher.prototype.getAuthTag = function getAuthTag () { | |
if (!this._decrypt && Buffer.isBuffer(this._authTag)) { | |
return this._authTag; | |
} else { | |
throw new Error('Attempting to get auth tag in unsupported state'); | |
} | |
}; | |
StreamCipher.prototype.setAuthTag = function setAuthTag (tag) { | |
if (this._decrypt) { | |
this._authTag = tag; | |
} else { | |
throw new Error('Attempting to set auth tag in unsupported state'); | |
} | |
}; | |
StreamCipher.prototype.setAAD = function setAAD (buf) { | |
if (!this._called) { | |
this._ghash.update(buf); | |
this._alen += buf.length; | |
} else { | |
throw new Error('Attempting to set AAD in unsupported state'); | |
} | |
}; | |
function xorTest(a, b) { | |
var out = 0; | |
if (a.length !== b.length) { | |
out++; | |
} | |
var len = Math.min(a.length, b.length); | |
var i = -1; | |
while (++i < len) { | |
out += (a[i] ^ b[i]); | |
} | |
return out; | |
} | |
}).call(this,require("buffer").Buffer) | |
},{"./aes":14,"./cipherBase":16,"./ghash":19,"./xor":30,"buffer":4,"inherits":116}],16:[function(require,module,exports){ | |
(function (Buffer){ | |
var Transform = require('stream').Transform; | |
var inherits = require('inherits'); | |
module.exports = CipherBase; | |
inherits(CipherBase, Transform); | |
function CipherBase() { | |
Transform.call(this); | |
} | |
CipherBase.prototype.update = function (data, inputEnd, outputEnc) { | |
this.write(data, inputEnd); | |
var outData = new Buffer(''); | |
var chunk; | |
while ((chunk = this.read())) { | |
outData = Buffer.concat([outData, chunk]); | |
} | |
if (outputEnc) { | |
outData = outData.toString(outputEnc); | |
} | |
return outData; | |
}; | |
CipherBase.prototype.final = function (outputEnc) { | |
this.end(); | |
var outData = new Buffer(''); | |
var chunk; | |
while ((chunk = this.read())) { | |
outData = Buffer.concat([outData, chunk]); | |
} | |
if (outputEnc) { | |
outData = outData.toString(outputEnc); | |
} | |
return outData; | |
}; | |
}).call(this,require("buffer").Buffer) | |
},{"buffer":4,"inherits":116,"stream":131}],17:[function(require,module,exports){ | |
(function (Buffer){ | |
var aes = require('./aes'); | |
var Transform = require('./cipherBase'); | |
var inherits = require('inherits'); | |
var modes = require('./modes'); | |
var StreamCipher = require('./streamCipher'); | |
var AuthCipher = require('./authCipher'); | |
var ebtk = require('./EVP_BytesToKey'); | |
inherits(Decipher, Transform); | |
function Decipher(mode, key, iv) { | |
if (!(this instanceof Decipher)) { | |
return new Decipher(mode, key, iv); | |
} | |
Transform.call(this); | |
this._cache = new Splitter(); | |
this._last = void 0; | |
this._cipher = new aes.AES(key); | |
this._prev = new Buffer(iv.length); | |
iv.copy(this._prev); | |
this._mode = mode; | |
} | |
Decipher.prototype._transform = function (data, _, next) { | |
this._cache.add(data); | |
var chunk; | |
var thing; | |
while ((chunk = this._cache.get())) { | |
thing = this._mode.decrypt(this, chunk); | |
this.push(thing); | |
} | |
next(); | |
}; | |
Decipher.prototype._flush = function (next) { | |
var chunk = this._cache.flush(); | |
if (!chunk) { | |
return next; | |
} | |
this.push(unpad(this._mode.decrypt(this, chunk))); | |
next(); | |
}; | |
function Splitter() { | |
if (!(this instanceof Splitter)) { | |
return new Splitter(); | |
} | |
this.cache = new Buffer(''); | |
} | |
Splitter.prototype.add = function (data) { | |
this.cache = Buffer.concat([this.cache, data]); | |
}; | |
Splitter.prototype.get = function () { | |
if (this.cache.length > 16) { | |
var out = this.cache.slice(0, 16); | |
this.cache = this.cache.slice(16); | |
return out; | |
} | |
return null; | |
}; | |
Splitter.prototype.flush = function () { | |
if (this.cache.length) { | |
return this.cache; | |
} | |
}; | |
function unpad(last) { | |
var padded = last[15]; | |
if (padded === 16) { | |
return; | |
} | |
return last.slice(0, 16 - padded); | |
} | |
var modelist = { | |
ECB: require('./modes/ecb'), | |
CBC: require('./modes/cbc'), | |
CFB: require('./modes/cfb'), | |
CFB8: require('./modes/cfb8'), | |
CFB1: require('./modes/cfb1'), | |
OFB: require('./modes/ofb'), | |
CTR: require('./modes/ctr'), | |
GCM: require('./modes/ctr') | |
}; | |
module.exports = function (crypto) { | |
function createDecipheriv(suite, password, iv) { | |
var config = modes[suite]; | |
if (!config) { | |
throw new TypeError('invalid suite type'); | |
} | |
if (typeof iv === 'string') { | |
iv = new Buffer(iv); | |
} | |
if (typeof password === 'string') { | |
password = new Buffer(password); | |
} | |
if (password.length !== config.key/8) { | |
throw new TypeError('invalid key length ' + password.length); | |
} | |
if (iv.length !== config.iv) { | |
throw new TypeError('invalid iv length ' + iv.length); | |
} | |
if (config.type === 'stream') { | |
return new StreamCipher(modelist[config.mode], password, iv, true); | |
} else if (config.type === 'auth') { | |
return new AuthCipher(modelist[config.mode], password, iv, true); | |
} | |
return new Decipher(modelist[config.mode], password, iv); | |
} | |
function createDecipher (suite, password) { | |
var config = modes[suite]; | |
if (!config) { | |
throw new TypeError('invalid suite type'); | |
} | |
var keys = ebtk(crypto, password, config.key, config.iv); | |
return createDecipheriv(suite, keys.key, keys.iv); | |
} | |
return { | |
createDecipher: createDecipher, | |
createDecipheriv: createDecipheriv | |
}; | |
}; | |
}).call(this,require("buffer").Buffer) | |
},{"./EVP_BytesToKey":13,"./aes":14,"./authCipher":15,"./cipherBase":16,"./modes":21,"./modes/cbc":22,"./modes/cfb":23,"./modes/cfb1":24,"./modes/cfb8":25,"./modes/ctr":26,"./modes/ecb":27,"./modes/ofb":28,"./streamCipher":29,"buffer":4,"inherits":116}],18:[function(require,module,exports){ | |
(function (Buffer){ | |
var aes = require('./aes'); | |
var Transform = require('./cipherBase'); | |
var inherits = require('inherits'); | |
var modes = require('./modes'); | |
var ebtk = require('./EVP_BytesToKey'); | |
var StreamCipher = require('./streamCipher'); | |
var AuthCipher = require('./authCipher'); | |
inherits(Cipher, Transform); | |
function Cipher(mode, key, iv) { | |
if (!(this instanceof Cipher)) { | |
return new Cipher(mode, key, iv); | |
} | |
Transform.call(this); | |
this._cache = new Splitter(); | |
this._cipher = new aes.AES(key); | |
this._prev = new Buffer(iv.length); | |
iv.copy(this._prev); | |
this._mode = mode; | |
} | |
Cipher.prototype._transform = function (data, _, next) { | |
this._cache.add(data); | |
var chunk; | |
var thing; | |
while ((chunk = this._cache.get())) { | |
thing = this._mode.encrypt(this, chunk); | |
this.push(thing); | |
} | |
next(); | |
}; | |
Cipher.prototype._flush = function (next) { | |
var chunk = this._cache.flush(); | |
this.push(this._mode.encrypt(this, chunk)); | |
this._cipher.scrub(); | |
next(); | |
}; | |
function Splitter() { | |
if (!(this instanceof Splitter)) { | |
return new Splitter(); | |
} | |
this.cache = new Buffer(''); | |
} | |
Splitter.prototype.add = function (data) { | |
this.cache = Buffer.concat([this.cache, data]); | |
}; | |
Splitter.prototype.get = function () { | |
if (this.cache.length > 15) { | |
var out = this.cache.slice(0, 16); | |
this.cache = this.cache.slice(16); | |
return out; | |
} | |
return null; | |
}; | |
Splitter.prototype.flush = function () { | |
var len = 16 - this.cache.length; | |
var padBuff = new Buffer(len); | |
var i = -1; | |
while (++i < len) { | |
padBuff.writeUInt8(len, i); | |
} | |
var out = Buffer.concat([this.cache, padBuff]); | |
return out; | |
}; | |
var modelist = { | |
ECB: require('./modes/ecb'), | |
CBC: require('./modes/cbc'), | |
CFB: require('./modes/cfb'), | |
CFB8: require('./modes/cfb8'), | |
CFB1: require('./modes/cfb1'), | |
OFB: require('./modes/ofb'), | |
CTR: require('./modes/ctr'), | |
GCM: require('./modes/ctr') | |
}; | |
module.exports = function (crypto) { | |
function createCipheriv(suite, password, iv) { | |
var config = modes[suite]; | |
if (!config) { | |
throw new TypeError('invalid suite type'); | |
} | |
if (typeof iv === 'string') { | |
iv = new Buffer(iv); | |
} | |
if (typeof password === 'string') { | |
password = new Buffer(password); | |
} | |
if (password.length !== config.key/8) { | |
throw new TypeError('invalid key length ' + password.length); | |
} | |
if (iv.length !== config.iv) { | |
throw new TypeError('invalid iv length ' + iv.length); | |
} | |
if (config.type === 'stream') { | |
return new StreamCipher(modelist[config.mode], password, iv); | |
} else if (config.type === 'auth') { | |
return new AuthCipher(modelist[config.mode], password, iv); | |
} | |
return new Cipher(modelist[config.mode], password, iv); | |
} | |
function createCipher (suite, password) { | |
var config = modes[suite]; | |
if (!config) { | |
throw new TypeError('invalid suite type'); | |
} | |
var keys = ebtk(crypto, password, config.key, config.iv); | |
return createCipheriv(suite, keys.key, keys.iv); | |
} | |
return { | |
createCipher: createCipher, | |
createCipheriv: createCipheriv | |
}; | |
}; | |
}).call(this,require("buffer").Buffer) | |
},{"./EVP_BytesToKey":13,"./aes":14,"./authCipher":15,"./cipherBase":16,"./modes":21,"./modes/cbc":22,"./modes/cfb":23,"./modes/cfb1":24,"./modes/cfb8":25,"./modes/ctr":26,"./modes/ecb":27,"./modes/ofb":28,"./streamCipher":29,"buffer":4,"inherits":116}],19:[function(require,module,exports){ | |
(function (Buffer){ | |
var zeros = new Buffer(16); | |
zeros.fill(0); | |
module.exports = GHASH; | |
function GHASH(key){ | |
this.h = key; | |
this.state = new Buffer(16); | |
this.state.fill(0); | |
this.cache = new Buffer(''); | |
} | |
// from http://bitwiseshiftleft.github.io/sjcl/doc/symbols/src/core_gcm.js.html | |
// by Juho Vähä-Herttua | |
GHASH.prototype.ghash = function (block) { | |
var i = -1; | |
while (++i < block.length) { | |
this.state[i] ^= 0xff & block[i]; | |
} | |
this._multiply(); | |
}; | |
GHASH.prototype._multiply = function () { | |
var Vi = toArray(this.h); | |
var Zi = [0, 0, 0, 0]; | |
var j, xi, lsb_Vi; | |
var i = -1; | |
while (++i < 128) { | |
xi = (this.state[~~(i/8)] & (1 << (7-i%8))) !== 0; | |
if (xi) { | |
// Z_i+1 = Z_i ^ V_i | |
Zi = xor(Zi, Vi); | |
} | |
// Store the value of LSB(V_i) | |
lsb_Vi = (Vi[3] & 1) !== 0; | |
// V_i+1 = V_i >> 1 | |
for (j=3; j>0; j--) { | |
Vi[j] = (Vi[j] >>> 1) | ((Vi[j-1]&1) << 31); | |
} | |
Vi[0] = Vi[0] >>> 1; | |
// If LSB(V_i) is 1, V_i+1 = (V_i >> 1) ^ R | |
if (lsb_Vi) { | |
Vi[0] = Vi[0] ^ (0xe1 << 24); | |
} | |
} | |
this.state = fromArray(Zi); | |
}; | |
GHASH.prototype.update = function (buf) { | |
this.cache = Buffer.concat([this.cache, buf]); | |
var chunk; | |
while (this.cache.length >= 16) { | |
chunk = this.cache.slice(0, 16); | |
this.cache = this.cache.slice(16); | |
this.ghash(chunk); | |
} | |
}; | |
GHASH.prototype.final = function (abl, bl) { | |
if (this.cache.length) { | |
this.ghash(Buffer.concat([this.cache, zeros], 16)); | |
} | |
this.ghash(fromArray([ | |
0, abl, | |
0, bl | |
])); | |
return this.state; | |
}; | |
function toArray(buf) { | |
return [ | |
buf.readUInt32BE(0), | |
buf.readUInt32BE(4), | |
buf.readUInt32BE(8), | |
buf.readUInt32BE(12) | |
]; | |
} | |
function fromArray(out) { | |
out = out.map(fixup_uint32); | |
var buf = new Buffer(16); | |
buf.writeUInt32BE(out[0], 0); | |
buf.writeUInt32BE(out[1], 4); | |
buf.writeUInt32BE(out[2], 8); | |
buf.writeUInt32BE(out[3], 12); | |
return buf; | |
} | |
var uint_max = Math.pow(2, 32); | |
function fixup_uint32(x) { | |
var ret, x_pos; | |
ret = x > uint_max || x < 0 ? (x_pos = Math.abs(x) % uint_max, x < 0 ? uint_max - x_pos : x_pos) : x; | |
return ret; | |
} | |
function xor(a, b) { | |
return [ | |
a[0] ^ b[0], | |
a[1] ^ b[1], | |
a[2] ^ b[2], | |
a[3] ^ b[3], | |
]; | |
} | |
}).call(this,require("buffer").Buffer) | |
},{"buffer":4}],20:[function(require,module,exports){ | |
module.exports = function (crypto, exports) { | |
exports = exports || {}; | |
var ciphers = require('./encrypter')(crypto); | |
exports.createCipher = ciphers.createCipher; | |
exports.createCipheriv = ciphers.createCipheriv; | |
var deciphers = require('./decrypter')(crypto); | |
exports.createDecipher = deciphers.createDecipher; | |
exports.createDecipheriv = deciphers.createDecipheriv; | |
var modes = require('./modes'); | |
function listCiphers () { | |
return Object.keys(modes); | |
} | |
exports.listCiphers = listCiphers; | |
}; | |
},{"./decrypter":17,"./encrypter":18,"./modes":21}],21:[function(require,module,exports){ | |
exports['aes-128-ecb'] = { | |
cipher: 'AES', | |
key: 128, | |
iv: 0, | |
mode: 'ECB', | |
type: 'block' | |
}; | |
exports['aes-192-ecb'] = { | |
cipher: 'AES', | |
key: 192, | |
iv: 0, | |
mode: 'ECB', | |
type: 'block' | |
}; | |
exports['aes-256-ecb'] = { | |
cipher: 'AES', | |
key: 256, | |
iv: 0, | |
mode: 'ECB', | |
type: 'block' | |
}; | |
exports['aes-128-cbc'] = { | |
cipher: 'AES', | |
key: 128, | |
iv: 16, | |
mode: 'CBC', | |
type: 'block' | |
}; | |
exports['aes-192-cbc'] = { | |
cipher: 'AES', | |
key: 192, | |
iv: 16, | |
mode: 'CBC', | |
type: 'block' | |
}; | |
exports['aes-256-cbc'] = { | |
cipher: 'AES', | |
key: 256, | |
iv: 16, | |
mode: 'CBC', | |
type: 'block' | |
}; | |
exports['aes128'] = exports['aes-128-cbc']; | |
exports['aes192'] = exports['aes-192-cbc']; | |
exports['aes256'] = exports['aes-256-cbc']; | |
exports['aes-128-cfb'] = { | |
cipher: 'AES', | |
key: 128, | |
iv: 16, | |
mode: 'CFB', | |
type: 'stream' | |
}; | |
exports['aes-192-cfb'] = { | |
cipher: 'AES', | |
key: 192, | |
iv: 16, | |
mode: 'CFB', | |
type: 'stream' | |
}; | |
exports['aes-256-cfb'] = { | |
cipher: 'AES', | |
key: 256, | |
iv: 16, | |
mode: 'CFB', | |
type: 'stream' | |
}; | |
exports['aes-128-cfb8'] = { | |
cipher: 'AES', | |
key: 128, | |
iv: 16, | |
mode: 'CFB8', | |
type: 'stream' | |
}; | |
exports['aes-192-cfb8'] = { | |
cipher: 'AES', | |
key: 192, | |
iv: 16, | |
mode: 'CFB8', | |
type: 'stream' | |
}; | |
exports['aes-256-cfb8'] = { | |
cipher: 'AES', | |
key: 256, | |
iv: 16, | |
mode: 'CFB8', | |
type: 'stream' | |
}; | |
exports['aes-128-cfb1'] = { | |
cipher: 'AES', | |
key: 128, | |
iv: 16, | |
mode: 'CFB1', | |
type: 'stream' | |
}; | |
exports['aes-192-cfb1'] = { | |
cipher: 'AES', | |
key: 192, | |
iv: 16, | |
mode: 'CFB1', | |
type: 'stream' | |
}; | |
exports['aes-256-cfb1'] = { | |
cipher: 'AES', | |
key: 256, | |
iv: 16, | |
mode: 'CFB1', | |
type: 'stream' | |
}; | |
exports['aes-128-ofb'] = { | |
cipher: 'AES', | |
key: 128, | |
iv: 16, | |
mode: 'OFB', | |
type: 'stream' | |
}; | |
exports['aes-192-ofb'] = { | |
cipher: 'AES', | |
key: 192, | |
iv: 16, | |
mode: 'OFB', | |
type: 'stream' | |
}; | |
exports['aes-256-ofb'] = { | |
cipher: 'AES', | |
key: 256, | |
iv: 16, | |
mode: 'OFB', | |
type: 'stream' | |
}; | |
exports['aes-128-ctr'] = { | |
cipher: 'AES', | |
key: 128, | |
iv: 16, | |
mode: 'CTR', | |
type: 'stream' | |
}; | |
exports['aes-192-ctr'] = { | |
cipher: 'AES', | |
key: 192, | |
iv: 16, | |
mode: 'CTR', | |
type: 'stream' | |
}; | |
exports['aes-256-ctr'] = { | |
cipher: 'AES', | |
key: 256, | |
iv: 16, | |
mode: 'CTR', | |
type: 'stream' | |
}; | |
exports['aes-128-gcm'] = { | |
cipher: 'AES', | |
key: 128, | |
iv: 12, | |
mode: 'GCM', | |
type: 'auth' | |
}; | |
exports['aes-192-gcm'] = { | |
cipher: 'AES', | |
key: 192, | |
iv: 12, | |
mode: 'GCM', | |
type: 'auth' | |
}; | |
exports['aes-256-gcm'] = { | |
cipher: 'AES', | |
key: 256, | |
iv: 12, | |
mode: 'GCM', | |
type: 'auth' | |
}; | |
},{}],22:[function(require,module,exports){ | |
var xor = require('../xor'); | |
exports.encrypt = function (self, block) { | |
var data = xor(block, self._prev); | |
self._prev = self._cipher.encryptBlock(data); | |
return self._prev; | |
}; | |
exports.decrypt = function (self, block) { | |
var pad = self._prev; | |
self._prev = block; | |
var out = self._cipher.decryptBlock(block); | |
return xor(out, pad); | |
}; | |
},{"../xor":30}],23:[function(require,module,exports){ | |
(function (Buffer){ | |
var xor = require('../xor'); | |
exports.encrypt = function (self, data, decrypt) { | |
var out = new Buffer(''); | |
var len; | |
while (data.length) { | |
if (self._cache.length === 0) { | |
self._cache = self._cipher.encryptBlock(self._prev); | |
self._prev = new Buffer(''); | |
} | |
if (self._cache.length <= data.length) { | |
len = self._cache.length; | |
out = Buffer.concat([out, encryptStart(self, data.slice(0, len), decrypt)]); | |
data = data.slice(len); | |
} else { | |
out = Buffer.concat([out, encryptStart(self, data, decrypt)]); | |
break; | |
} | |
} | |
return out; | |
}; | |
function encryptStart(self, data, decrypt) { | |
var len = data.length; | |
var out = xor(data, self._cache); | |
self._cache = self._cache.slice(len); | |
self._prev = Buffer.concat([self._prev, decrypt?data:out]); | |
return out; | |
} | |
}).call(this,require("buffer").Buffer) | |
},{"../xor":30,"buffer":4}],24:[function(require,module,exports){ | |
(function (Buffer){ | |
function encryptByte(self, byte, decrypt) { | |
var pad; | |
var i = -1; | |
var len = 8; | |
var out = 0; | |
var bit, value; | |
while (++i < len) { | |
pad = self._cipher.encryptBlock(self._prev); | |
bit = (byte & (1 << (7-i))) ? 0x80:0; | |
value = pad[0] ^ bit; | |
out += ((value&0x80) >> (i%8)); | |
self._prev = shiftIn(self._prev, decrypt?bit:value); | |
} | |
return out; | |
} | |
exports.encrypt = function (self, chunk, decrypt) { | |
var len = chunk.length; | |
var out = new Buffer(len); | |
var i = -1; | |
while (++i < len) { | |
out[i] = encryptByte(self, chunk[i], decrypt); | |
} | |
return out; | |
}; | |
function shiftIn(buffer, value) { | |
var len = buffer.length; | |
var i = -1; | |
var out = new Buffer(buffer.length); | |
buffer = Buffer.concat([buffer, new Buffer([value])]); | |
while(++i < len) { | |
out[i] = buffer[i]<<1 | buffer[i+1]>>(7); | |
} | |
return out; | |
} | |
}).call(this,require("buffer").Buffer) | |
},{"buffer":4}],25:[function(require,module,exports){ | |
(function (Buffer){ | |
function encryptByte(self, byte, decrypt) { | |
var pad = self._cipher.encryptBlock(self._prev); | |
var out = pad[0] ^ byte; | |
self._prev = Buffer.concat([self._prev.slice(1), new Buffer([decrypt?byte:out])]); | |
return out; | |
} | |
exports.encrypt = function (self, chunk, decrypt) { | |
var len = chunk.length; | |
var out = new Buffer(len); | |
var i = -1; | |
while (++i < len) { | |
out[i] = encryptByte(self, chunk[i], decrypt); | |
} | |
return out; | |
}; | |
}).call(this,require("buffer").Buffer) | |
},{"buffer":4}],26:[function(require,module,exports){ | |
(function (Buffer){ | |
var xor = require('../xor'); | |
function getBlock(self) { | |
var out = self._cipher.encryptBlock(self._prev); | |
incr32(self._prev); | |
return out; | |
} | |
exports.encrypt = function (self, chunk) { | |
while (self._cache.length < chunk.length) { | |
self._cache = Buffer.concat([self._cache, getBlock(self)]); | |
} | |
var pad = self._cache.slice(0, chunk.length); | |
self._cache = self._cache.slice(chunk.length); | |
return xor(chunk, pad); | |
}; | |
function incr32(iv) { | |
var len = iv.length; | |
var item; | |
while (len--) { | |
item = iv.readUInt8(len); | |
if (item === 255) { | |
iv.writeUInt8(0, len); | |
} else { | |
item++; | |
iv.writeUInt8(item, len); | |
break; | |
} | |
} | |
} | |
}).call(this,require("buffer").Buffer) | |
},{"../xor":30,"buffer":4}],27:[function(require,module,exports){ | |
exports.encrypt = function (self, block) { | |
return self._cipher.encryptBlock(block); | |
}; | |
exports.decrypt = function (self, block) { | |
return self._cipher.decryptBlock(block); | |
}; | |
},{}],28:[function(require,module,exports){ | |
(function (Buffer){ | |
var xor = require('../xor'); | |
function getBlock(self) { | |
self._prev = self._cipher.encryptBlock(self._prev); | |
return self._prev; | |
} | |
exports.encrypt = function (self, chunk) { | |
while (self._cache.length < chunk.length) { | |
self._cache = Buffer.concat([self._cache, getBlock(self)]); | |
} | |
var pad = self._cache.slice(0, chunk.length); | |
self._cache = self._cache.slice(chunk.length); | |
return xor(chunk, pad); | |
}; | |
}).call(this,require("buffer").Buffer) | |
},{"../xor":30,"buffer":4}],29:[function(require,module,exports){ | |
(function (Buffer){ | |
var aes = require('./aes'); | |
var Transform = require('./cipherBase'); | |
var inherits = require('inherits'); | |
inherits(StreamCipher, Transform); | |
module.exports = StreamCipher; | |
function StreamCipher(mode, key, iv, decrypt) { | |
if (!(this instanceof StreamCipher)) { | |
return new StreamCipher(mode, key, iv); | |
} | |
Transform.call(this); | |
this._cipher = new aes.AES(key); | |
this._prev = new Buffer(iv.length); | |
this._cache = new Buffer(''); | |
this._secCache = new Buffer(''); | |
this._decrypt = decrypt; | |
iv.copy(this._prev); | |
this._mode = mode; | |
} | |
StreamCipher.prototype._transform = function (chunk, _, next) { | |
next(null, this._mode.encrypt(this, chunk, this._decrypt)); | |
}; | |
StreamCipher.prototype._flush = function (next) { | |
this._cipher.scrub(); | |
next(); | |
}; | |
}).call(this,require("buffer").Buffer) | |
},{"./aes":14,"./cipherBase":16,"buffer":4,"inherits":116}],30:[function(require,module,exports){ | |
(function (Buffer){ | |
module.exports = xor; | |
function xor(a, b) { | |
var len = Math.min(a.length, b.length); | |
var out = new Buffer(len); | |
var i = -1; | |
while (++i < len) { | |
out.writeUInt8(a[i] ^ b[i], i); | |
} | |
return out; | |
} | |
}).call(this,require("buffer").Buffer) | |
},{"buffer":4}],31:[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", | |
"2.16.840.1.101.3.4.1.4": "aes-128-cfb", | |
"2.16.840.1.101.3.4.1.21": "aes-192-ecb", | |
"2.16.840.1.101.3.4.1.22": "aes-192-cbc", | |
"2.16.840.1.101.3.4.1.23": "aes-192-ofb", | |
"2.16.840.1.101.3.4.1.24": "aes-192-cfb", | |
"2.16.840.1.101.3.4.1.41": "aes-256-ecb", | |
"2.16.840.1.101.3.4.1.42": "aes-256-cbc", | |
"2.16.840.1.101.3.4.1.43": "aes-256-ofb", | |
"2.16.840.1.101.3.4.1.44": "aes-256-cfb" | |
} | |
},{}],32:[function(require,module,exports){ | |
(function (Buffer){ | |
exports['RSA-SHA224'] = exports.sha224WithRSAEncryption = { | |
sign: 'rsa', | |
hash: 'sha224', | |
id: new Buffer('302d300d06096086480165030402040500041c', 'hex') | |
}; | |
exports['RSA-SHA256'] = exports.sha256WithRSAEncryption = { | |
sign: 'rsa', | |
hash: 'sha256', | |
id: new Buffer('3031300d060960864801650304020105000420', 'hex') | |
}; | |
exports['RSA-SHA384'] = exports.sha384WithRSAEncryption = { | |
sign: 'rsa', | |
hash: 'sha384', | |
id: new Buffer('3041300d060960864801650304020205000430', 'hex') | |
}; | |
exports['RSA-SHA512'] = exports.sha512WithRSAEncryption = { | |
sign: 'rsa', | |
hash: 'sha512', | |
id: new Buffer('3051300d060960864801650304020305000440', 'hex') | |
}; | |
exports['RSA-SHA1'] = { | |
sign: 'rsa', | |
hash: 'sha1', | |
id: new Buffer('3021300906052b0e03021a05000414', 'hex') | |
} | |
exports['ecdsa-with-SHA1'] = { | |
sign: 'ecdsa', | |
hash: 'sha1', | |
id: new Buffer(/*'3021300906052b0e03021a05000414'*/'', 'hex') | |
} | |
}).call(this,require("buffer").Buffer) | |
},{"buffer":4}],33:[function(require,module,exports){ | |
// from https://github.com/indutny/self-signed/blob/gh-pages/lib/asn1.js | |
// Fedor, you are amazing. | |
var asn1 = require('asn1.js'); | |
var rfc3280 = require('asn1.js-rfc3280'); | |
var RSAPrivateKey = asn1.define('RSAPrivateKey', function() { | |
this.seq().obj( | |
this.key('version').int(), | |
this.key('modulus').int(), | |
this.key('publicExponent').int(), | |
this.key('privateExponent').int(), | |
this.key('prime1').int(), | |
this.key('prime2').int(), | |
this.key('exponent1').int(), | |
this.key('exponent2').int(), | |
this.key('coefficient').int() | |
); | |
}); | |
exports.RSAPrivateKey = RSAPrivateKey; | |
var RSAPublicKey = asn1.define('RSAPublicKey', function() { | |
this.seq().obj( | |
this.key('modulus').int(), | |
this.key('publicExponent').int() | |
); | |
}); | |
exports.RSAPublicKey = RSAPublicKey; | |
var PublicKey = rfc3280.SubjectPublicKeyInfo; | |
exports.PublicKey = PublicKey; | |
var ECPublicKey = asn1.define('ECPublicKey', function() { | |
this.seq().obj( | |
this.key('algorithm').seq().obj( | |
this.key('id').objid(), | |
this.key('curve').objid() | |
), | |
this.key('subjectPrivateKey').bitstr() | |
); | |
}); | |
exports.ECPublicKey = ECPublicKey; | |
var ECPrivateWrap = asn1.define('ECPrivateWrap', function() { | |
this.seq().obj( | |
this.key('version').int(), | |
this.key('algorithm').seq().obj( | |
this.key('id').objid(), | |
this.key('curve').objid() | |
), | |
this.key('subjectPrivateKey').octstr() | |
); | |
}); | |
exports.ECPrivateWrap = ECPrivateWrap; | |
var PrivateKeyInfo = asn1.define('PrivateKeyInfo', function() { | |
this.seq().obj( | |
this.key('version').int(), | |
this.key('algorithm').use(rfc3280.AlgorithmIdentifier), | |
this.key('subjectPrivateKey').octstr() | |
); | |
}); | |
exports.PrivateKey = PrivateKeyInfo; | |
var EncryptedPrivateKeyInfo = asn1.define('EncryptedPrivateKeyInfo', function() { | |
this.seq().obj( | |
this.key('algorithm').seq().obj( | |
this.key('id').objid(), | |
this.key('decrypt').seq().obj( | |
this.key('kde').seq().obj( | |
this.key('id').objid(), | |
this.key('kdeparams').seq().obj( | |
this.key('salt').octstr(), | |
this.key('iters').int() | |
) | |
), | |
this.key('cipher').seq().obj( | |
this.key('algo').objid(), | |
this.key('iv').octstr() | |
) | |
) | |
), | |
this.key('subjectPrivateKey').octstr() | |
); | |
}); | |
exports.EncryptedPrivateKey = EncryptedPrivateKeyInfo; | |
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() | |
); | |