Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependencies #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
sudo: false
language: node_js
node_js:
- "8"
- "10"
- "12"
- "14"
- "16"
- "18"
- lts/*
- current
env:
Expand Down
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@ Abstract base class to inherit from if you want to create streams implementing t

```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

class MyHash extends HashBase {
this._sum = 0x00
}

inherits(MyHash, HashBase)
constructor() {
super(1) // in bytes
}

MyHash.prototype._update = function () {
for (let i = 0; i < this._block.length; ++i) this._sum ^= this._block[i]
}
_update () {
for (let i = 0; i < this._block.length; ++i) this._sum ^= this._block[i]
}

MyHash.prototype._digest = function () {
return this._sum
_digest () {
return this._sum
}
}

const data = Buffer.from([ 0x00, 0x42, 0x01 ])
Expand Down
136 changes: 69 additions & 67 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,95 +1,97 @@
'use strict'
var Buffer = require('safe-buffer').Buffer
var Transform = require('readable-stream').Transform
var inherits = require('inherits')

const { Buffer } = require('node:buffer')
const { Transform } = require('node:stream')

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
}
class HashBase extends Transform {
constructor (blockSize) {
super()

inherits(HashBase, Transform)
this._block = Buffer.allocUnsafe(blockSize)
this._blockSize = blockSize
this._blockOffset = 0
this._length = [0, 0, 0, 0]

HashBase.prototype._transform = function (chunk, encoding, callback) {
var error = null
try {
this.update(chunk, encoding)
} catch (err) {
error = err
this._finalized = false
}

callback(error)
}
_transform (chunk, encoding, callback) {
let error = null
try {
this.update(chunk, encoding)
} catch (err) {
error = err
}

HashBase.prototype._flush = function (callback) {
var error = null
try {
this.push(this.digest())
} catch (err) {
error = err
callback(error)
}

callback(error)
}
_flush (callback) {
let error = null
try {
this.push(this.digest())
} catch (err) {
error = err
}

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
callback(error)
}
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
update (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
const block = this._block
let offset = 0
while (this._blockOffset + data.length - offset >= this._blockSize) {
for (let 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 (let 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
}

return this
}

HashBase.prototype._update = function () {
throw new Error('_update is not implemented')
}
_update () {
throw new Error('_update is not implemented')
}

HashBase.prototype.digest = function (encoding) {
if (this._finalized) throw new Error('Digest already called')
this._finalized = true
digest (encoding) {
if (this._finalized) throw new Error('Digest already called')
this._finalized = true

var digest = this._digest()
if (encoding !== undefined) digest = digest.toString(encoding)
let 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
// reset state
this._block.fill(0)
this._blockOffset = 0
for (let i = 0; i < 4; ++i) this._length[i] = 0

return digest
}
return digest
}

HashBase.prototype._digest = function () {
throw new Error('_digest is not implemented')
_digest () {
throw new Error('_digest is not implemented')
}
}

module.exports = HashBase
Loading