[Pkg-javascript-commits] [node-hash-base] 01/04: Import Upstream version 3.0.3
Shirish Togarla
shirish12-guest at moszumanska.debian.org
Tue Feb 7 19:56:49 UTC 2017
This is an automated email from the git hooks/post-receive script.
shirish12-guest pushed a commit to branch master
in repository node-hash-base.
commit 22240ee2ed90242fd7af171dac8e9bef6a9702da
Author: Shirish Togarla <shirishtogarla533 at gmail.com>
Date: Tue Feb 7 19:38:53 2017 +0000
Import Upstream version 3.0.3
---
.gitignore | 5 ++
.travis.yml | 17 +++++
LICENSE | 21 +++++++
README.md | 38 +++++++++++
index.js | 82 ++++++++++++++++++++++++
package.json | 37 +++++++++++
test/index.js | 199 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 399 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8afa17e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+.nyc_output
+coverage
+node_modules
+
+npm-debug.log
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..296bd19
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,17 @@
+sudo: false
+language: node_js
+node_js:
+ - "0.10"
+ - "0.11"
+ - "0.12"
+ - "4"
+ - "5"
+ - "6"
+env:
+ matrix:
+ - TEST_SUITE=unit
+matrix:
+ include:
+ - node_js: "4"
+ env: TEST_SUITE=lint
+script: npm run-script $TEST_SUITE
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..6f02ae8
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2016 Kirill Fomichev
+
+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 OR COPYRIGHT HOLDERS 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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..c5f2df5
--- /dev/null
+++ b/README.md
@@ -0,0 +1,38 @@
+# hash-base
+
+[![NPM Package](https://img.shields.io/npm/v/hash-base.svg?style=flat-square)](https://www.npmjs.org/package/hash-base)
+[![Build Status](https://img.shields.io/travis/crypto-browserify/hash-base.svg?branch=master&style=flat-square)](https://travis-ci.org/crypto-browserify/hash-base)
+[![Dependency status](https://img.shields.io/david/crypto-browserify/hash-base.svg?style=flat-square)](https://david-dm.org/crypto-browserify/hash-base#info=dependencies)
+
+[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
+
+Abstract base class to inherit from if you want to create streams implementing the same API as node crypto [Hash][1] (for [Cipher][2] / [Decipher][3] check [crypto-browserify/cipher-base][4]).
+
+## Example
+
+```js
+function MyHash () {
+ HashBase.call(64) // in bytes
+}
+
+inherits(MyHash, HashBase)
+
+MyHash.prototype._update = function () {
+ // hashing one block with buffer this._block
+}
+
+MyHash.prototype._digest = function () {
+ // create padding and produce result
+}
+```
+You also can check [source code](index.js) or [crypto-browserify/md5.js][5]
+
+## LICENSE
+
+MIT
+
+[1]: https://nodejs.org/api/crypto.html#crypto_class_hash
+[2]: https://nodejs.org/api/crypto.html#crypto_class_cipher
+[3]: https://nodejs.org/api/crypto.html#crypto_class_decipher
+[4]: https://github.com/crypto-browserify/cipher-base
+[5]: https://github.com/crypto-browserify/md5.js
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..01371cd
--- /dev/null
+++ b/index.js
@@ -0,0 +1,82 @@
+'use strict'
+var Transform = require('stream').Transform
+var inherits = require('inherits')
+
+function HashBase (blockSize) {
+ Transform.call(this)
+
+ this._block = new Buffer(blockSize)
+ this._blockSize = blockSize
+ this._blockOffset = 0
+ this._length = [0, 0, 0, 0]
+
+ this._finalized = false
+}
+
+inherits(HashBase, Transform)
+
+HashBase.prototype._transform = function (chunk, encoding, callback) {
+ var error = null
+ try {
+ this.update(chunk, encoding)
+ } catch (err) {
+ error = err
+ }
+
+ callback(error)
+}
+
+HashBase.prototype._flush = function (callback) {
+ var error = null
+ try {
+ this.push(this.digest())
+ } catch (err) {
+ error = err
+ }
+
+ callback(error)
+}
+
+HashBase.prototype.update = function (data, encoding) {
+ if (!Buffer.isBuffer(data) && typeof data !== 'string') throw new TypeError('Data must be a string or a buffer')
+ if (this._finalized) throw new Error('Digest already called')
+ if (!Buffer.isBuffer(data)) data = new Buffer(data, encoding)
+
+ // consume data
+ var block = this._block
+ var offset = 0
+ while (this._blockOffset + data.length - offset >= this._blockSize) {
+ for (var i = this._blockOffset; i < this._blockSize;) block[i++] = data[offset++]
+ this._update()
+ this._blockOffset = 0
+ }
+ while (offset < data.length) block[this._blockOffset++] = data[offset++]
+
+ // update length
+ for (var j = 0, carry = data.length * 8; carry > 0; ++j) {
+ this._length[j] += carry
+ carry = (this._length[j] / 0x0100000000) | 0
+ if (carry > 0) this._length[j] -= 0x0100000000 * carry
+ }
+
+ return this
+}
+
+HashBase.prototype._update = function (data) {
+ throw new Error('_update is not implemented')
+}
+
+HashBase.prototype.digest = function (encoding) {
+ if (this._finalized) throw new Error('Digest already called')
+ this._finalized = true
+
+ var digest = this._digest()
+ if (encoding !== undefined) digest = digest.toString(encoding)
+ return digest
+}
+
+HashBase.prototype._digest = function () {
+ throw new Error('_digest is not implemented')
+}
+
+module.exports = HashBase
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..45c06bc
--- /dev/null
+++ b/package.json
@@ -0,0 +1,37 @@
+{
+ "name": "hash-base",
+ "version": "3.0.3",
+ "description": "abstract base class for hash-streams",
+ "keywords": [
+ "hash",
+ "stream"
+ ],
+ "homepage": "https://github.com/crypto-browserify/hash-base",
+ "bugs": {
+ "url": "https://github.com/crypto-browserify/hash-base/issues"
+ },
+ "license": "MIT",
+ "author": "Kirill Fomichev <fanatid at ya.ru> (https://github.com/fanatid)",
+ "files": [
+ "index.js"
+ ],
+ "main": "index.js",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/crypto-browserify/hash-base.git"
+ },
+ "scripts": {
+ "coverage": "nyc node test/*.js",
+ "lint": "standard",
+ "test": "npm run lint && npm run unit",
+ "unit": "node test/*.js"
+ },
+ "dependencies": {
+ "inherits": "^2.0.1"
+ },
+ "devDependencies": {
+ "nyc": "^7.0.0",
+ "standard": "^7.0.0",
+ "tape": "^4.2.0"
+ }
+}
diff --git a/test/index.js b/test/index.js
new file mode 100644
index 0000000..0b260f1
--- /dev/null
+++ b/test/index.js
@@ -0,0 +1,199 @@
+'use strict'
+var test = require('tape')
+var randomBytes = require('crypto').randomBytes
+var HashBase = require('../')
+
+function beforeEach (t) {
+ var _test = t.test
+ t.test = function (name, cb) {
+ _test(name, function (t) {
+ t.base = new HashBase(64)
+ cb(t)
+ })
+ }
+}
+
+test('_transform', function (t) {
+ beforeEach(t)
+
+ t.test('should use update', function (t) {
+ t.plan(2)
+ var buffer = new Buffer(randomBytes(42))
+ t.base.update = function (data) { t.true(data === buffer) }
+ t.base._transform(buffer, 'buffer', function (err) {
+ t.same(err, null)
+ })
+ t.end()
+ })
+
+ t.test('should pass encoding to update', function (t) {
+ t.plan(3)
+ t.base.update = function (data, encoding) {
+ t.same(data, 'УТФ-8 text')
+ t.same(encoding, 'utf8')
+ }
+ t.base._transform('УТФ-8 text', 'utf8', function (err) {
+ t.same(err, null)
+ })
+ t.end()
+ })
+
+ t.test('should handle error in update', function (t) {
+ t.plan(1)
+ var err = new Error('hey')
+ t.base.update = function () { throw err }
+ t.base._transform(new Buffer(randomBytes(42)), 'buffer', function (_err) {
+ t.true(_err === err)
+ })
+ t.end()
+ })
+
+ t.end()
+})
+
+test('_flush', function (t) {
+ beforeEach(t)
+
+ t.test('should use _digest', function (t) {
+ t.plan(2)
+ var buffer = new Buffer(randomBytes(42))
+ t.base._digest = function () { return buffer }
+ t.base.push = function (data) { t.true(data === buffer) }
+ t.base._flush(function (err) { t.same(err, null) })
+ t.end()
+ })
+
+ t.test('should handle errors in _digest', function (t) {
+ t.plan(1)
+ var err = new Error('hey')
+ t.base._digest = function () { throw err }
+ t.base._flush(function (_err) { t.true(_err === err) })
+ t.end()
+ })
+
+ t.test('should throw "Digest already called" on second _flush call', function (t) {
+ var buffer = new Buffer(randomBytes(42))
+ t.base._digest = function () { return buffer }
+ t.base._flush(function (err) { t.error(err) })
+ t.base._flush(function (err) { t.same(err.message, 'Digest already called') })
+ t.same(t.base.read(), buffer)
+ t.end()
+ })
+
+ t.end()
+})
+
+test('update', function (t) {
+ beforeEach(t)
+
+ t.test('should return hash instance', function (t) {
+ t.same(t.base.update(new Buffer(63)), t.base)
+ t.end()
+ })
+
+ t.test('decode string with custom encoding', function (t) {
+ t.plan(1)
+ var buffer = new Buffer('УТФ-8 text', 'utf8')
+ var base = new HashBase(buffer.length)
+ base._update = function () { t.same(this._block, buffer) }
+ base.update(buffer.toString('utf8'), 'utf8')
+ t.end()
+ })
+
+ t.test('decode string with utf8 by default', function (t) {
+ t.plan(1)
+ var buffer = new Buffer(64)
+ buffer.fill(0)
+ new Buffer('УТФ-8', 'utf8').copy(buffer)
+ t.base._update = function () { t.same(this._block, buffer) }
+ t.base.update(buffer.toString('utf8'))
+ t.end()
+ })
+
+ t.test('data length is more than 2^32', function (t) {
+ t.plan(3)
+ var buffer = new Buffer(1048576)
+ var base = new HashBase(1048576)
+ base._length = [ 4286578688, 0, 0, 0 ]
+ base._update = function () { t.same(this._block, buffer) }
+ base.update(buffer)
+ base.update(buffer)
+ t.same(base._length, [ 8388608, 1, 0, 0 ])
+ t.end()
+ })
+
+ t.test('call after digest should throw error', function (t) {
+ t.base._digest = function () {}
+ t.base.digest()
+ t.throws(function () {
+ t.base.update(new Buffer(0))
+ }, /^Error: Digest already called$/)
+ t.end()
+ })
+
+ t.end()
+})
+
+test('_update', function (t) {
+ beforeEach(t)
+
+ t.test('should throw error by default', function (t) {
+ t.throws(function () {
+ t.base._update()
+ }, /^Error: _update is not implemented$/)
+ t.end()
+ })
+
+ t.end()
+})
+
+test('digest', function (t) {
+ beforeEach(t)
+
+ t.test('should return buffer from _digest by default', function (t) {
+ t.plan(2)
+ var buffer = new Buffer(randomBytes(42))
+ t.base._digest = function () {
+ t.pass()
+ return buffer
+ }
+ t.same(t.base.digest(), buffer)
+ t.end()
+ })
+
+ t.test('should return buffer by default', function (t) {
+ t.base._digest = function () { return new Buffer('УТФ-8 text', 'utf8') }
+ t.same(t.base.digest(), new Buffer('УТФ-8 text', 'utf8'))
+ t.end()
+ })
+
+ t.test('should encode result with custom encoding', function (t) {
+ t.base._digest = function () { return new Buffer('УТФ-8 text', 'utf8') }
+ t.same(t.base.digest('utf8'), 'УТФ-8 text')
+ t.end()
+ })
+
+ t.test('second call digest throw error', function (t) {
+ t.base._digest = function () {}
+ t.base.digest()
+ t.throws(function () {
+ t.base.digest()
+ }, /^Error: Digest already called$/)
+ t.end()
+ })
+
+ t.end()
+})
+
+test('_digest', function (t) {
+ beforeEach(t)
+
+ t.test('_digest should throw error by default', function (t) {
+ t.throws(function () {
+ t.base._digest()
+ }, /^Error: _digest is not implemented$/)
+ t.end()
+ })
+
+ t.end()
+})
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-hash-base.git
More information about the Pkg-javascript-commits
mailing list