Skip to content

Commit

Permalink
[eslint] switch to eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 1, 2024
1 parent 242b695 commit 88e9366
Show file tree
Hide file tree
Showing 14 changed files with 1,117 additions and 832 deletions.
64 changes: 64 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"root": true,

"extends": "@ljharb",

"rules": {
"func-style": "off",
"no-magic-numbers": "off",
},

"overrides": [
{
"files": "bin.js",
"extends": "@ljharb/eslint-config/node/0.4",
"rules": {
"func-style": "off",
},
},
{
"files": [
"hash.js",
"sha.js",
"sha1.js",
"sha224.js",
"sha256.js",
"sha384.js",
"sha512.js",
"test/vectors.js",
],
"rules": {
"no-underscore-dangle": "off",
},
},
{
"files": [
"sha.js",
"sha1.js",
"sha224.js",
],
"rules": {
"max-params": "off",
},
},
{
"files": [
"sha256.js",
"sha512.js",
],
"rules": {
"max-statements": "off",
},
},
{
"files": [
"sha512.js",
],
"rules": {
"new-cap": "warn",
"max-lines": "off",
"max-lines-per-function": "off",
},
},
],
}
57 changes: 30 additions & 27 deletions bin.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
#! /usr/bin/env node

var createHash = require('./browserify')
var argv = process.argv.slice(2)
'use strict';

function pipe (algorithm, s) {
var start = Date.now()
var hash = createHash(algorithm || 'sha1')
var createHash = require('./browserify');
var argv = process.argv.slice(2);

s.on('data', function (data) {
hash.update(data)
})
function pipe(algorithm, s) {
var start = Date.now();
var hash = createHash(algorithm || 'sha1');

s.on('end', function () {
if (process.env.DEBUG) {
return console.log(hash.digest('hex'), Date.now() - start)
}
s.on('data', function (data) {
hash.update(data);
});

console.log(hash.digest('hex'))
})
s.on('end', function () {
if (process.env.DEBUG) {
console.log(hash.digest('hex'), Date.now() - start);
} else {
console.log(hash.digest('hex'));
}
});
}

function usage () {
console.error('sha.js [algorithm=sha1] [filename] # hash filename with algorithm')
console.error('input | sha.js [algorithm=sha1] # hash stdin with algorithm')
console.error('sha.js --help # display this message')
function usage() {
console.error('sha.js [algorithm=sha1] [filename] # hash filename with algorithm');
console.error('input | sha.js [algorithm=sha1] # hash stdin with algorithm');
console.error('sha.js --help # display this message');
}

if (!process.stdin.isTTY) {
pipe(argv[0], process.stdin)
pipe(argv[0], process.stdin);
} else if (argv.length) {
if (/--help|-h/.test(argv[0])) {
usage()
} else {
var filename = argv.pop()
var algorithm = argv.pop()
pipe(algorithm, require('fs').createReadStream(filename))
}
if ((/--help|-h/).test(argv[0])) {
usage();
} else {
var filename = argv.pop();
var algorithm = argv.pop();
// eslint-disable-next-line global-require
pipe(algorithm, require('fs').createReadStream(filename));
}
} else {
usage()
usage();
}
129 changes: 67 additions & 62 deletions hash.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,86 @@
var Buffer = require('safe-buffer').Buffer
'use strict';

var Buffer = require('safe-buffer').Buffer;

// prototype class for hash functions
function Hash (blockSize, finalSize) {
this._block = Buffer.alloc(blockSize)
this._finalSize = finalSize
this._blockSize = blockSize
this._len = 0
function Hash(blockSize, finalSize) {
this._block = Buffer.alloc(blockSize);
this._finalSize = finalSize;
this._blockSize = blockSize;
this._len = 0;
}

Hash.prototype.update = function (data, enc) {
if (typeof data === 'string') {
enc = enc || 'utf8'
data = Buffer.from(data, enc)
}

var block = this._block
var blockSize = this._blockSize
var length = data.length
var accum = this._len

for (var offset = 0; offset < length;) {
var assigned = accum % blockSize
var remainder = Math.min(length - offset, blockSize - assigned)

for (var i = 0; i < remainder; i++) {
block[assigned + i] = data[offset + i]
}

accum += remainder
offset += remainder

if ((accum % blockSize) === 0) {
this._update(block)
}
}

this._len += length
return this
}
/* eslint no-param-reassign: 0 */
if (typeof data === 'string') {
enc = enc || 'utf8';
data = Buffer.from(data, enc);
}

var block = this._block;
var blockSize = this._blockSize;
var length = data.length;
var accum = this._len;

for (var offset = 0; offset < length;) {
var assigned = accum % blockSize;
var remainder = Math.min(length - offset, blockSize - assigned);

for (var i = 0; i < remainder; i++) {
block[assigned + i] = data[offset + i];
}

accum += remainder;
offset += remainder;

if ((accum % blockSize) === 0) {
this._update(block);
}
}

this._len += length;
return this;
};

Hash.prototype.digest = function (enc) {
var rem = this._len % this._blockSize
var rem = this._len % this._blockSize;

this._block[rem] = 0x80
this._block[rem] = 0x80;

// zero (rem + 1) trailing bits, where (rem + 1) is the smallest
// non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize
this._block.fill(0, rem + 1)
/*
* zero (rem + 1) trailing bits, where (rem + 1) is the smallest
* non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize
*/
this._block.fill(0, rem + 1);

if (rem >= this._finalSize) {
this._update(this._block)
this._block.fill(0)
}
if (rem >= this._finalSize) {
this._update(this._block);
this._block.fill(0);
}

var bits = this._len * 8
var bits = this._len * 8;

// uint32
if (bits <= 0xffffffff) {
this._block.writeUInt32BE(bits, this._blockSize - 4)
// uint32
if (bits <= 0xffffffff) {
this._block.writeUInt32BE(bits, this._blockSize - 4);

// uint64
} else {
var lowBits = (bits & 0xffffffff) >>> 0
var highBits = (bits - lowBits) / 0x100000000
// uint64
} else {
var lowBits = (bits & 0xffffffff) >>> 0;
var highBits = (bits - lowBits) / 0x100000000;

this._block.writeUInt32BE(highBits, this._blockSize - 8)
this._block.writeUInt32BE(lowBits, this._blockSize - 4)
}
this._block.writeUInt32BE(highBits, this._blockSize - 8);
this._block.writeUInt32BE(lowBits, this._blockSize - 4);
}

this._update(this._block)
var hash = this._hash()
this._update(this._block);
var hash = this._hash();

return enc ? hash.toString(enc) : hash
}
return enc ? hash.toString(enc) : hash;
};

Hash.prototype._update = function () {
throw new Error('_update must be implemented by subclass')
}
throw new Error('_update must be implemented by subclass');
};

module.exports = Hash
module.exports = Hash;
28 changes: 16 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
var exports = module.exports = function SHA (algorithm) {
algorithm = algorithm.toLowerCase()
'use strict';

var Algorithm = exports[algorithm]
if (!Algorithm) throw new Error(algorithm + ' is not supported (we accept pull requests)')
module.exports = function SHA(algorithm) {
var alg = algorithm.toLowerCase();

return new Algorithm()
}
var Algorithm = module.exports[alg];
if (!Algorithm) {
throw new Error(alg + ' is not supported (we accept pull requests)');
}

exports.sha = require('./sha')
exports.sha1 = require('./sha1')
exports.sha224 = require('./sha224')
exports.sha256 = require('./sha256')
exports.sha384 = require('./sha384')
exports.sha512 = require('./sha512')
return new Algorithm();
};

module.exports.sha = require('./sha');
module.exports.sha1 = require('./sha1');
module.exports.sha224 = require('./sha224');
module.exports.sha256 = require('./sha256');
module.exports.sha384 = require('./sha384');
module.exports.sha512 = require('./sha512');
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
"safe-buffer": "^5.2.1"
},
"devDependencies": {
"@ljharb/eslint-config": "^21.1.1",
"buffer": "^2.8.3",
"eslint": "=8.8.0",
"hash-test-vectors": "^1.3.2",
"standard": "^10.0.3",
"tape": "^5.9.0",
"typedarray": "^0.0.7"
},
"bin": "./bin.js",
"scripts": {
"prepublish": "npm ls && npm run unit",
"lint": "standard",
"lint": "eslint --ext=js,mjs .",
"pretest": "npm run lint",
"test": "npm run tests-only",
"tests-only": "tape 'test/**/*.js'",
Expand Down
Loading

0 comments on commit 88e9366

Please sign in to comment.