Skip to content

Commit 88e9366

Browse files
committed
[eslint] switch to eslint
1 parent 242b695 commit 88e9366

14 files changed

+1117
-832
lines changed

Diff for: .eslintrc

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"root": true,
3+
4+
"extends": "@ljharb",
5+
6+
"rules": {
7+
"func-style": "off",
8+
"no-magic-numbers": "off",
9+
},
10+
11+
"overrides": [
12+
{
13+
"files": "bin.js",
14+
"extends": "@ljharb/eslint-config/node/0.4",
15+
"rules": {
16+
"func-style": "off",
17+
},
18+
},
19+
{
20+
"files": [
21+
"hash.js",
22+
"sha.js",
23+
"sha1.js",
24+
"sha224.js",
25+
"sha256.js",
26+
"sha384.js",
27+
"sha512.js",
28+
"test/vectors.js",
29+
],
30+
"rules": {
31+
"no-underscore-dangle": "off",
32+
},
33+
},
34+
{
35+
"files": [
36+
"sha.js",
37+
"sha1.js",
38+
"sha224.js",
39+
],
40+
"rules": {
41+
"max-params": "off",
42+
},
43+
},
44+
{
45+
"files": [
46+
"sha256.js",
47+
"sha512.js",
48+
],
49+
"rules": {
50+
"max-statements": "off",
51+
},
52+
},
53+
{
54+
"files": [
55+
"sha512.js",
56+
],
57+
"rules": {
58+
"new-cap": "warn",
59+
"max-lines": "off",
60+
"max-lines-per-function": "off",
61+
},
62+
},
63+
],
64+
}

Diff for: bin.js

+30-27
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
11
#! /usr/bin/env node
22

3-
var createHash = require('./browserify')
4-
var argv = process.argv.slice(2)
3+
'use strict';
54

6-
function pipe (algorithm, s) {
7-
var start = Date.now()
8-
var hash = createHash(algorithm || 'sha1')
5+
var createHash = require('./browserify');
6+
var argv = process.argv.slice(2);
97

10-
s.on('data', function (data) {
11-
hash.update(data)
12-
})
8+
function pipe(algorithm, s) {
9+
var start = Date.now();
10+
var hash = createHash(algorithm || 'sha1');
1311

14-
s.on('end', function () {
15-
if (process.env.DEBUG) {
16-
return console.log(hash.digest('hex'), Date.now() - start)
17-
}
12+
s.on('data', function (data) {
13+
hash.update(data);
14+
});
1815

19-
console.log(hash.digest('hex'))
20-
})
16+
s.on('end', function () {
17+
if (process.env.DEBUG) {
18+
console.log(hash.digest('hex'), Date.now() - start);
19+
} else {
20+
console.log(hash.digest('hex'));
21+
}
22+
});
2123
}
2224

23-
function usage () {
24-
console.error('sha.js [algorithm=sha1] [filename] # hash filename with algorithm')
25-
console.error('input | sha.js [algorithm=sha1] # hash stdin with algorithm')
26-
console.error('sha.js --help # display this message')
25+
function usage() {
26+
console.error('sha.js [algorithm=sha1] [filename] # hash filename with algorithm');
27+
console.error('input | sha.js [algorithm=sha1] # hash stdin with algorithm');
28+
console.error('sha.js --help # display this message');
2729
}
2830

2931
if (!process.stdin.isTTY) {
30-
pipe(argv[0], process.stdin)
32+
pipe(argv[0], process.stdin);
3133
} else if (argv.length) {
32-
if (/--help|-h/.test(argv[0])) {
33-
usage()
34-
} else {
35-
var filename = argv.pop()
36-
var algorithm = argv.pop()
37-
pipe(algorithm, require('fs').createReadStream(filename))
38-
}
34+
if ((/--help|-h/).test(argv[0])) {
35+
usage();
36+
} else {
37+
var filename = argv.pop();
38+
var algorithm = argv.pop();
39+
// eslint-disable-next-line global-require
40+
pipe(algorithm, require('fs').createReadStream(filename));
41+
}
3942
} else {
40-
usage()
43+
usage();
4144
}

Diff for: hash.js

+67-62
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,86 @@
1-
var Buffer = require('safe-buffer').Buffer
1+
'use strict';
2+
3+
var Buffer = require('safe-buffer').Buffer;
24

35
// prototype class for hash functions
4-
function Hash (blockSize, finalSize) {
5-
this._block = Buffer.alloc(blockSize)
6-
this._finalSize = finalSize
7-
this._blockSize = blockSize
8-
this._len = 0
6+
function Hash(blockSize, finalSize) {
7+
this._block = Buffer.alloc(blockSize);
8+
this._finalSize = finalSize;
9+
this._blockSize = blockSize;
10+
this._len = 0;
911
}
1012

1113
Hash.prototype.update = function (data, enc) {
12-
if (typeof data === 'string') {
13-
enc = enc || 'utf8'
14-
data = Buffer.from(data, enc)
15-
}
16-
17-
var block = this._block
18-
var blockSize = this._blockSize
19-
var length = data.length
20-
var accum = this._len
21-
22-
for (var offset = 0; offset < length;) {
23-
var assigned = accum % blockSize
24-
var remainder = Math.min(length - offset, blockSize - assigned)
25-
26-
for (var i = 0; i < remainder; i++) {
27-
block[assigned + i] = data[offset + i]
28-
}
29-
30-
accum += remainder
31-
offset += remainder
32-
33-
if ((accum % blockSize) === 0) {
34-
this._update(block)
35-
}
36-
}
37-
38-
this._len += length
39-
return this
40-
}
14+
/* eslint no-param-reassign: 0 */
15+
if (typeof data === 'string') {
16+
enc = enc || 'utf8';
17+
data = Buffer.from(data, enc);
18+
}
19+
20+
var block = this._block;
21+
var blockSize = this._blockSize;
22+
var length = data.length;
23+
var accum = this._len;
24+
25+
for (var offset = 0; offset < length;) {
26+
var assigned = accum % blockSize;
27+
var remainder = Math.min(length - offset, blockSize - assigned);
28+
29+
for (var i = 0; i < remainder; i++) {
30+
block[assigned + i] = data[offset + i];
31+
}
32+
33+
accum += remainder;
34+
offset += remainder;
35+
36+
if ((accum % blockSize) === 0) {
37+
this._update(block);
38+
}
39+
}
40+
41+
this._len += length;
42+
return this;
43+
};
4144

4245
Hash.prototype.digest = function (enc) {
43-
var rem = this._len % this._blockSize
46+
var rem = this._len % this._blockSize;
4447

45-
this._block[rem] = 0x80
48+
this._block[rem] = 0x80;
4649

47-
// zero (rem + 1) trailing bits, where (rem + 1) is the smallest
48-
// non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize
49-
this._block.fill(0, rem + 1)
50+
/*
51+
* zero (rem + 1) trailing bits, where (rem + 1) is the smallest
52+
* non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize
53+
*/
54+
this._block.fill(0, rem + 1);
5055

51-
if (rem >= this._finalSize) {
52-
this._update(this._block)
53-
this._block.fill(0)
54-
}
56+
if (rem >= this._finalSize) {
57+
this._update(this._block);
58+
this._block.fill(0);
59+
}
5560

56-
var bits = this._len * 8
61+
var bits = this._len * 8;
5762

58-
// uint32
59-
if (bits <= 0xffffffff) {
60-
this._block.writeUInt32BE(bits, this._blockSize - 4)
63+
// uint32
64+
if (bits <= 0xffffffff) {
65+
this._block.writeUInt32BE(bits, this._blockSize - 4);
6166

62-
// uint64
63-
} else {
64-
var lowBits = (bits & 0xffffffff) >>> 0
65-
var highBits = (bits - lowBits) / 0x100000000
67+
// uint64
68+
} else {
69+
var lowBits = (bits & 0xffffffff) >>> 0;
70+
var highBits = (bits - lowBits) / 0x100000000;
6671

67-
this._block.writeUInt32BE(highBits, this._blockSize - 8)
68-
this._block.writeUInt32BE(lowBits, this._blockSize - 4)
69-
}
72+
this._block.writeUInt32BE(highBits, this._blockSize - 8);
73+
this._block.writeUInt32BE(lowBits, this._blockSize - 4);
74+
}
7075

71-
this._update(this._block)
72-
var hash = this._hash()
76+
this._update(this._block);
77+
var hash = this._hash();
7378

74-
return enc ? hash.toString(enc) : hash
75-
}
79+
return enc ? hash.toString(enc) : hash;
80+
};
7681

7782
Hash.prototype._update = function () {
78-
throw new Error('_update must be implemented by subclass')
79-
}
83+
throw new Error('_update must be implemented by subclass');
84+
};
8085

81-
module.exports = Hash
86+
module.exports = Hash;

Diff for: index.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
var exports = module.exports = function SHA (algorithm) {
2-
algorithm = algorithm.toLowerCase()
1+
'use strict';
32

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

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

10-
exports.sha = require('./sha')
11-
exports.sha1 = require('./sha1')
12-
exports.sha224 = require('./sha224')
13-
exports.sha256 = require('./sha256')
14-
exports.sha384 = require('./sha384')
15-
exports.sha512 = require('./sha512')
11+
return new Algorithm();
12+
};
13+
14+
module.exports.sha = require('./sha');
15+
module.exports.sha1 = require('./sha1');
16+
module.exports.sha224 = require('./sha224');
17+
module.exports.sha256 = require('./sha256');
18+
module.exports.sha384 = require('./sha384');
19+
module.exports.sha512 = require('./sha512');

Diff for: package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212
"safe-buffer": "^5.2.1"
1313
},
1414
"devDependencies": {
15+
"@ljharb/eslint-config": "^21.1.1",
1516
"buffer": "^2.8.3",
17+
"eslint": "=8.8.0",
1618
"hash-test-vectors": "^1.3.2",
17-
"standard": "^10.0.3",
1819
"tape": "^5.9.0",
1920
"typedarray": "^0.0.7"
2021
},
2122
"bin": "./bin.js",
2223
"scripts": {
2324
"prepublish": "npm ls && npm run unit",
24-
"lint": "standard",
25+
"lint": "eslint --ext=js,mjs .",
2526
"pretest": "npm run lint",
2627
"test": "npm run tests-only",
2728
"tests-only": "tape 'test/**/*.js'",

0 commit comments

Comments
 (0)