pax_global_header00006660000000000000000000000064131112463770014517gustar00rootroot0000000000000052 comment=d36440ed0448a654734ae05464d55d34b189a17a hash-base-3.0.4/000077500000000000000000000000001311124637700133565ustar00rootroot00000000000000hash-base-3.0.4/.gitignore000066400000000000000000000000611311124637700153430ustar00rootroot00000000000000.nyc_output coverage node_modules npm-debug.log hash-base-3.0.4/.travis.yml000066400000000000000000000003201311124637700154620ustar00rootroot00000000000000sudo: false language: node_js node_js: - "4" - "5" - "6" - "7" env: matrix: - TEST_SUITE=unit matrix: include: - node_js: "6" env: TEST_SUITE=lint script: npm run-script $TEST_SUITE hash-base-3.0.4/LICENSE000066400000000000000000000020721311124637700143640ustar00rootroot00000000000000The 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. hash-base-3.0.4/README.md000066400000000000000000000032611311124637700146370ustar00rootroot00000000000000# 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 const HashBase = require('hash-base') const inherits = require('inherits') // our hash function is XOR sum of all bytes function MyHash () { HashBase.call(this, 1) // in bytes this._sum = 0x00 } inherits(MyHash, HashBase) MyHash.prototype._update = function () { for (let i = 0; i < this._block.length; ++i) this._sum ^= this._block[i] } MyHash.prototype._digest = function () { return this._sum } const data = Buffer.from([ 0x00, 0x42, 0x01 ]) const hash = new MyHash().update(data).digest() console.log(hash) // => 67 ``` 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 hash-base-3.0.4/index.js000066400000000000000000000044121311124637700150240ustar00rootroot00000000000000'use strict' var Buffer = require('safe-buffer').Buffer var Transform = require('stream').Transform var inherits = require('inherits') function throwIfNotStringOrBuffer (val, prefix) { if (!Buffer.isBuffer(val) && typeof val !== 'string') { throw new TypeError(prefix + ' must be a string or a buffer') } } function HashBase (blockSize) { Transform.call(this) this._block = Buffer.allocUnsafe(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) { throwIfNotStringOrBuffer(data, 'Data') if (this._finalized) throw new Error('Digest already called') if (!Buffer.isBuffer(data)) data = Buffer.from(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 () { 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) // reset state this._block.fill(0) this._blockOffset = 0 for (var i = 0; i < 4; ++i) this._length[i] = 0 return digest } HashBase.prototype._digest = function () { throw new Error('_digest is not implemented') } module.exports = HashBase hash-base-3.0.4/package.json000066400000000000000000000016271311124637700156520ustar00rootroot00000000000000{ "name": "hash-base", "version": "3.0.4", "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 (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", "safe-buffer": "^5.0.1" }, "devDependencies": { "nyc": "^8.3.2", "standard": "*", "tape": "^4.2.0" }, "engines": { "node": ">=4" } } hash-base-3.0.4/test/000077500000000000000000000000001311124637700143355ustar00rootroot00000000000000hash-base-3.0.4/test/index.js000066400000000000000000000107341311124637700160070ustar00rootroot00000000000000'use strict' var test = require('tape') var HashBase = require('../') var utf8text = 'УТФ-8 text' var utf8buf = Buffer.from(utf8text, 'utf8') function noop () {} function createHashBase (t) { t.base = new HashBase(64) } function beforeEach (t) { var fns = Array.prototype.slice.call(arguments, 1) var _test = t.test t.test = function (name, callback) { _test(name, function (t) { for (var i = 0; i < fns.length; ++i) t = fns[i](t) || t callback(t) }) } } test('HashBase#_transform', function (t) { beforeEach(t, createHashBase) t.test('should use HashBase#update', function (t) { t.plan(3) t.base.update = function () { t.same(arguments[0], utf8text) t.same(arguments[1], 'utf8') } t.base._transform(utf8text, 'utf8', function (err) { t.same(err, null) }) t.end() }) t.test('should handle error in HashBase#update', function (t) { t.plan(1) var err = new Error('hey') t.base.update = function () { throw err } t.base._transform(Buffer.allocUnsafe(0), 'buffer', function (_err) { t.true(_err === err) }) t.end() }) t.end() }) test('HashBase#_flush', function (t) { beforeEach(t, createHashBase) t.test('should use HashBase#digest', function (t) { t.plan(2) var buffer = Buffer.allocUnsafe(0) t.base.push = function (data) { t.true(data === buffer) } t.base.digest = function () { return buffer } t.base._flush(function (err) { t.same(err, null) }) t.end() }) t.test('should handle errors in HashBase#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.end() }) test('HashBase#update', function (t) { beforeEach(t, createHashBase) t.test('only string or buffer is allowed', function (t) { t.throws(function () { t.base.update(null) }, /^TypeError: Data must be a string or a buffer$/) t.end() }) t.test('should throw error after HashBase#digest', function (t) { t.base._digest = noop t.base.digest() t.throws(function () { t.base.update('') }, /^Error: Digest already called$/) t.end() }) t.test('should use HashBase#_update', function (t) { t.plan(1) t.base._update = t.pass t.base.update(Buffer.allocUnsafe(64)) t.end() }) t.test('default encoding is utf8', function (t) { t.plan(1) var buffer = Buffer.allocUnsafe(64) buffer.fill(0) utf8buf.copy(buffer) t.base._update = function () { t.same(this._block, buffer) } t.base.update(buffer.toString('utf8')) t.end() }) t.test('decode string with custom encoding', function (t) { t.plan(1) var buffer = Buffer.allocUnsafe(64).fill(0x42) t.base._update = function () { t.same(this._block, buffer) } t.base.update(buffer.toString('hex'), 'hex') t.end() }) t.test('data length is more than 2^32 bits', function (t) { t.base._length = [ Math.pow(2, 32) - 1, 0, 0, 0 ] t.base.update(Buffer.allocUnsafe(1)) t.same(t.base._length, [ 7, 1, 0, 0 ]) t.end() }) t.test('should return `this`', function (t) { t.same(t.base.update(Buffer.allocUnsafe(0)), t.base) t.end() }) t.end() }) test('HashBase#_update', function (t) { beforeEach(t, createHashBase) t.test('is not implemented', function (t) { t.throws(function () { t.base._update() }, /^Error: _update is not implemented$/) t.end() }) t.end() }) test('HashBase#digest', function (t) { beforeEach(t, createHashBase) t.test('should throw error on second call', function (t) { t.base._digest = noop t.base.digest() t.throws(function () { t.base.digest() }, /^Error: Digest already called$/) t.end() }) t.test('should use HashBase#_digest', function (t) { t.plan(1) t.base._digest = t.pass t.base.digest() t.end() }) t.test('should return buffer by default', function (t) { t.base._digest = function () { return utf8buf } t.same(t.base.digest(), utf8buf) t.end() }) t.test('should encode result with custom encoding', function (t) { t.base._digest = function () { return utf8buf } t.same(t.base.digest('utf8'), utf8text) t.end() }) t.end() }) test('HashBase#_digest', function (t) { beforeEach(t, createHashBase) t.test('is not implemented', function (t) { t.throws(function () { t.base._digest() }, /^Error: _digest is not implemented$/) t.end() }) t.end() })