Current Folder
This commit is contained in:
1 parent
8b3382289b
commit
cfcada880d
4379 files changed
No files matched your search
-99
@@ -1,99 +0,0 @@
|
||||
var Buffer = require('safe-buffer').Buffer
|
||||
var Transform = require('stream').Transform
|
||||
var StringDecoder = require('string_decoder').StringDecoder
|
||||
var inherits = require('inherits')
|
||||
|
||||
function CipherBase (hashMode) {
|
||||
Transform.call(this)
|
||||
this.hashMode = typeof hashMode === 'string'
|
||||
if (this.hashMode) {
|
||||
this[hashMode] = this._finalOrDigest
|
||||
} else {
|
||||
this.final = this._finalOrDigest
|
||||
}
|
||||
if (this._final) {
|
||||
this.__final = this._final
|
||||
this._final = null
|
||||
}
|
||||
this._decoder = null
|
||||
this._encoding = null
|
||||
}
|
||||
inherits(CipherBase, Transform)
|
||||
|
||||
CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
|
||||
if (typeof data === 'string') {
|
||||
data = Buffer.from(data, inputEnc)
|
||||
}
|
||||
|
||||
var outData = this._update(data)
|
||||
if (this.hashMode) return this
|
||||
|
||||
if (outputEnc) {
|
||||
outData = this._toString(outData, outputEnc)
|
||||
}
|
||||
|
||||
return outData
|
||||
}
|
||||
|
||||
CipherBase.prototype.setAutoPadding = function () {}
|
||||
CipherBase.prototype.getAuthTag = function () {
|
||||
throw new Error('trying to get auth tag in unsupported state')
|
||||
}
|
||||
|
||||
CipherBase.prototype.setAuthTag = function () {
|
||||
throw new Error('trying to set auth tag in unsupported state')
|
||||
}
|
||||
|
||||
CipherBase.prototype.setAAD = function () {
|
||||
throw new Error('trying to set aad in unsupported state')
|
||||
}
|
||||
|
||||
CipherBase.prototype._transform = function (data, _, next) {
|
||||
var err
|
||||
try {
|
||||
if (this.hashMode) {
|
||||
this._update(data)
|
||||
} else {
|
||||
this.push(this._update(data))
|
||||
}
|
||||
} catch (e) {
|
||||
err = e
|
||||
} finally {
|
||||
next(err)
|
||||
}
|
||||
}
|
||||
CipherBase.prototype._flush = function (done) {
|
||||
var err
|
||||
try {
|
||||
this.push(this.__final())
|
||||
} catch (e) {
|
||||
err = e
|
||||
}
|
||||
|
||||
done(err)
|
||||
}
|
||||
CipherBase.prototype._finalOrDigest = function (outputEnc) {
|
||||
var outData = this.__final() || Buffer.alloc(0)
|
||||
if (outputEnc) {
|
||||
outData = this._toString(outData, outputEnc, true)
|
||||
}
|
||||
return outData
|
||||
}
|
||||
|
||||
CipherBase.prototype._toString = function (value, enc, fin) {
|
||||
if (!this._decoder) {
|
||||
this._decoder = new StringDecoder(enc)
|
||||
this._encoding = enc
|
||||
}
|
||||
|
||||
if (this._encoding !== enc) throw new Error('can\'t switch encodings')
|
||||
|
||||
var out = this._decoder.write(value)
|
||||
if (fin) {
|
||||
out += this._decoder.end()
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
module.exports = CipherBase
|
||||
-36
@@ -1,36 +0,0 @@
|
||||
{
|
||||
"name": "cipher-base",
|
||||
"version": "1.0.4",
|
||||
"description": "abstract base class for crypto-streams",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "node test.js | tspec"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/crypto-browserify/cipher-base.git"
|
||||
},
|
||||
"keywords": [
|
||||
"cipher",
|
||||
"stream"
|
||||
],
|
||||
"author": "Calvin Metcalf <calvin.metcalf@gmail.com>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/crypto-browserify/cipher-base/issues"
|
||||
},
|
||||
"homepage": "https://github.com/crypto-browserify/cipher-base#readme",
|
||||
"dependencies": {
|
||||
"inherits": "^2.0.1",
|
||||
"safe-buffer": "^5.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"standard": "^10.0.2",
|
||||
"tap-spec": "^4.1.0",
|
||||
"tape": "^4.2.0"
|
||||
}
|
||||
|
||||
,"_resolved": "https://registry.npmjs.org/cipher-base/-/cipher-base-1.0.4.tgz"
|
||||
,"_integrity": "sha512-Kkht5ye6ZGmwv40uUDZztayT2ThLQGfnj/T71N/XzeZeo3nf8foyW7zGTsPYkEya3m5f3cAypH+qe7YOrM1U2Q=="
|
||||
,"_from": "cipher-base@^1.0.0"
|
||||
}
|
||||
-111
@@ -1,111 +0,0 @@
|
||||
var Buffer = require('safe-buffer').Buffer
|
||||
var CipherBase = require('./')
|
||||
|
||||
var test = require('tape')
|
||||
var inherits = require('inherits')
|
||||
|
||||
test('basic version', function (t) {
|
||||
function Cipher () {
|
||||
CipherBase.call(this)
|
||||
}
|
||||
inherits(Cipher, CipherBase)
|
||||
Cipher.prototype._update = function (input) {
|
||||
t.ok(Buffer.isBuffer(input))
|
||||
return input
|
||||
}
|
||||
Cipher.prototype._final = function () {
|
||||
// noop
|
||||
}
|
||||
var cipher = new Cipher()
|
||||
var utf8 = 'abc123abcd'
|
||||
var update = cipher.update(utf8, 'utf8', 'base64') + cipher.final('base64')
|
||||
var string = (Buffer.from(update, 'base64')).toString()
|
||||
t.equals(utf8, string)
|
||||
t.end()
|
||||
})
|
||||
test('hash mode', function (t) {
|
||||
function Cipher () {
|
||||
CipherBase.call(this, 'finalName')
|
||||
this._cache = []
|
||||
}
|
||||
inherits(Cipher, CipherBase)
|
||||
Cipher.prototype._update = function (input) {
|
||||
t.ok(Buffer.isBuffer(input))
|
||||
this._cache.push(input)
|
||||
}
|
||||
Cipher.prototype._final = function () {
|
||||
return Buffer.concat(this._cache)
|
||||
}
|
||||
var cipher = new Cipher()
|
||||
var utf8 = 'abc123abcd'
|
||||
var update = cipher.update(utf8, 'utf8').finalName('base64')
|
||||
var string = (Buffer.from(update, 'base64')).toString()
|
||||
|
||||
t.equals(utf8, string)
|
||||
t.end()
|
||||
})
|
||||
test('hash mode as stream', function (t) {
|
||||
function Cipher () {
|
||||
CipherBase.call(this, 'finalName')
|
||||
this._cache = []
|
||||
}
|
||||
inherits(Cipher, CipherBase)
|
||||
Cipher.prototype._update = function (input) {
|
||||
t.ok(Buffer.isBuffer(input))
|
||||
this._cache.push(input)
|
||||
}
|
||||
Cipher.prototype._final = function () {
|
||||
return Buffer.concat(this._cache)
|
||||
}
|
||||
var cipher = new Cipher()
|
||||
cipher.on('error', function (e) {
|
||||
t.notOk(e)
|
||||
})
|
||||
var utf8 = 'abc123abcd'
|
||||
cipher.end(utf8, 'utf8')
|
||||
var update = cipher.read().toString('base64')
|
||||
var string = (Buffer.from(update, 'base64')).toString()
|
||||
|
||||
t.equals(utf8, string)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('encodings', function (t) {
|
||||
inherits(Cipher, CipherBase)
|
||||
function Cipher () {
|
||||
CipherBase.call(this)
|
||||
}
|
||||
Cipher.prototype._update = function (input) {
|
||||
return input
|
||||
}
|
||||
Cipher.prototype._final = function () {
|
||||
// noop
|
||||
}
|
||||
t.test('mix and match encoding', function (t) {
|
||||
t.plan(2)
|
||||
|
||||
var cipher = new Cipher()
|
||||
cipher.update('foo', 'utf8', 'utf8')
|
||||
t.throws(function () {
|
||||
cipher.update('foo', 'utf8', 'base64')
|
||||
})
|
||||
cipher = new Cipher()
|
||||
cipher.update('foo', 'utf8', 'base64')
|
||||
t.doesNotThrow(function () {
|
||||
cipher.update('foo', 'utf8')
|
||||
cipher.final('base64')
|
||||
})
|
||||
})
|
||||
t.test('handle long uft8 plaintexts', function (t) {
|
||||
t.plan(1)
|
||||
var txt = 'ふっかつ あきる すぶり はやい つける まゆげ たんさん みんぞく ねほりはほり せまい たいまつばな ひはん'
|
||||
|
||||
var cipher = new Cipher()
|
||||
var decipher = new Cipher()
|
||||
var enc = decipher.update(cipher.update(txt, 'utf8', 'base64'), 'base64', 'utf8')
|
||||
enc += decipher.update(cipher.final('base64'), 'base64', 'utf8')
|
||||
enc += decipher.final('utf8')
|
||||
|
||||
t.equals(txt, enc)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user