Skip to content

Commit 41b4f26

Browse files
committed
Merge branch 'master' of github.com:crypto-browserify/sha.js
2 parents 8eb102b + cdfe4b8 commit 41b4f26

File tree

15 files changed

+265
-285
lines changed

15 files changed

+265
-285
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
language: node_js
22
node_js:
33
- "0.10"
4+
- "0.12"
5+
- "iojs"
6+
env:
7+
- TEST_SUITE=lint
8+
- TEST_SUITE=unit
9+
script: "npm run-script $TEST_SUITE"
410

bin.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
#! /usr/bin/env node
22

33
var createHash = require('./browserify')
4-
54
var argv = process.argv.slice(2)
65

7-
if(/--help|-h/.test(argv[0])) return usage()
8-
9-
function stream (alg, s) {
6+
function pipe (algorithm, s) {
107
var start = Date.now()
11-
var hash = createHash(alg || 'sha1')
12-
s
13-
.on('data', function (data) {
8+
var hash = createHash(algorithm || 'sha1')
9+
10+
s.on('data', function (data) {
1411
hash.update(data)
1512
})
16-
.on('end', function (data) {
17-
if(process.env.DEBUG)
13+
14+
s.on('end', function () {
15+
if (process.env.DEBUG) {
1816
return console.log(hash.digest('hex'), Date.now() - start)
17+
}
18+
1919
console.log(hash.digest('hex'))
2020
})
2121
}
22-
23-
if(!process.stdin.isTTY) {
24-
stream(argv[0], process.stdin)
25-
} else if (argv.length) {
26-
var filename = argv.pop()
27-
var alg = argv.pop()
28-
stream(alg, require('fs').createReadStream(filename))
29-
} else {
30-
usage()
31-
}
3222

3323
function usage () {
3424
console.error('sha.js [algorithm=sha1] [filename] # hash filename with algorithm')
3525
console.error('input | sha.js [algorithm=sha1] # hash stdin with algorithm')
3626
console.error('sha.js --help # display this message')
3727
}
28+
29+
if (!process.stdin.isTTY) {
30+
pipe(argv[0], process.stdin)
31+
32+
} else if (argv.length) {
33+
if (/--help|-h/.test(argv[0])) {
34+
usage()
35+
36+
} else {
37+
var filename = argv.pop()
38+
var algorithm = argv.pop()
39+
pipe(algorithm, require('fs').createReadStream(filename))
40+
}
41+
} else {
42+
usage()
43+
}

hash.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
//prototype class for hash functions
1+
// prototype class for hash functions
22
function Hash (blockSize, finalSize) {
3-
this._block = new Buffer(blockSize) //new Uint32Array(blockSize/4)
3+
this._block = new Buffer(blockSize)
44
this._finalSize = finalSize
55
this._blockSize = blockSize
66
this._len = 0
77
this._s = 0
88
}
99

1010
Hash.prototype.update = function (data, enc) {
11-
if ("string" === typeof data) {
12-
enc = enc || "utf8"
11+
if (typeof data === 'string') {
12+
enc = enc || 'utf8'
1313
data = new Buffer(data, enc)
1414
}
1515

hexpp.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
1-
2-
31
function toHex (buf, group, wrap, LE) {
42
buf = buf.buffer || buf
53
var s = ''
64
var l = buf.byteLength || buf.length
7-
for(var i = 0; i < l ; i++) {
8-
var byte = (i&0xfffffffc)|(!LE ? i%4 : 3 - i%4)
9-
s = s + ((buf[byte]>>4).toString(16))
10-
+ ((buf[byte]&0xf).toString(16))
11-
+ (group-1==i%group ? ' ' : '')
12-
+ (wrap-1==i%wrap ? '\n' : '')
5+
for (var i = 0; i < l ; i++) {
6+
var byteParam = (i & 0xfffffffc) | (!LE ? i % 4 : 3 - i % 4)
7+
s += ((buf[byteParam] >> 4).toString(16)) +
8+
((buf[byteParam] & 0xf).toString(16)) +
9+
(group - 1 === i % group ? ' ' : '') +
10+
(wrap - 1 === i % wrap ? '\n' : '')
1311
}
1412
return s
1513
}
1614

17-
function reverseByteOrder(n) {
18-
return (
19-
((n << 24) & 0xff000000)
20-
| ((n << 8) & 0x00ff0000)
21-
| ((n >> 8) & 0x0000ff00)
22-
| ((n >> 24) & 0x000000ff)
23-
)
24-
}
25-
26-
var hexpp = module.exports = function (buffer, opts) {
15+
var hexpp = module.exports = function hexpp (buffer, opts) {
2716
opts = opts || {}
2817
opts.groups = opts.groups || 4
2918
opts.wrap = opts.wrap || 16
@@ -35,4 +24,3 @@ hexpp.defaults = function (opts) {
3524
return hexpp(b, opts)
3625
}
3726
}
38-

index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
var exports = module.exports = function (alg) {
2-
var Alg = exports[alg.toLowerCase()]
3-
if(!Alg) throw new Error(alg + ' is not supported (we accept pull requests)')
4-
return new Alg()
5-
}
1+
var exports = module.exports = function SHA (algorithm) {
2+
algorithm = algorithm.toLowerCase()
3+
4+
var Algorithm = exports[algorithm]
5+
if (!Algorithm) throw new Error(algorithm + ' is not supported (we accept pull requests)')
66

7+
return new Algorithm()
8+
}
79

810
exports.sha = require('./sha')
911
exports.sha1 = require('./sha1')

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
"devDependencies": {
1414
"buffer": "~2.3.2",
1515
"hash-test-vectors": "^1.3.1",
16+
"standard": "^4.0.0",
1617
"tape": "~2.3.2",
1718
"typedarray": "0.0.6"
1819
},
1920
"bin": "./bin.js",
2021
"scripts": {
21-
"prepublish": "npm ls && npm test",
22-
"test": "set -e; for t in test/*.js; do node $t; done;"
22+
"prepublish": "npm ls && npm run unit",
23+
"lint": "standard",
24+
"test": "npm run lint && npm run unit",
25+
"unit": "set -e; for t in test/*.js; do node $t; done;"
2326
},
2427
"author": "Dominic Tarr <[email protected]> (dominictarr.com)",
2528
"license": "MIT"

sha.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var Hash = require('./hash')
1111

1212
var W = new Array(80)
1313

14-
function Sha() {
14+
function Sha () {
1515
this.init()
1616
this._w = W
1717

@@ -21,20 +21,20 @@ function Sha() {
2121
inherits(Sha, Hash)
2222

2323
Sha.prototype.init = function () {
24-
this._a = 0x67452301
25-
this._b = 0xefcdab89
26-
this._c = 0x98badcfe
27-
this._d = 0x10325476
28-
this._e = 0xc3d2e1f0
24+
this._a = 0x67452301 | 0
25+
this._b = 0xefcdab89 | 0
26+
this._c = 0x98badcfe | 0
27+
this._d = 0x10325476 | 0
28+
this._e = 0xc3d2e1f0 | 0
2929

3030
return this
3131
}
3232

3333
/*
3434
* Bitwise rotate a 32-bit number to the left.
3535
*/
36-
function rol(num, cnt) {
37-
return (num << cnt) | (num >>> (32 - cnt));
36+
function rol (num, cnt) {
37+
return (num << cnt) | (num >>> (32 - cnt))
3838
}
3939

4040
Sha.prototype._update = function (M) {
@@ -52,8 +52,8 @@ Sha.prototype._update = function (M) {
5252
* SHA-1 has a bitwise rotate left operation. But, SHA is not
5353
* function calcW() { return rol(W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16], 1) }
5454
*/
55-
function calcW() { return W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16] }
56-
function loop(w, f) {
55+
function calcW () { return W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16] }
56+
function loop (w, f) {
5757
W[j] = w
5858

5959
var t = rol(a, 5) + f + e + w + k
@@ -86,11 +86,11 @@ Sha.prototype._update = function (M) {
8686
Sha.prototype._hash = function () {
8787
var H = new Buffer(20)
8888

89-
H.writeInt32BE(this._a|0, 0)
90-
H.writeInt32BE(this._b|0, 4)
91-
H.writeInt32BE(this._c|0, 8)
92-
H.writeInt32BE(this._d|0, 12)
93-
H.writeInt32BE(this._e|0, 16)
89+
H.writeInt32BE(this._a | 0, 0)
90+
H.writeInt32BE(this._b | 0, 4)
91+
H.writeInt32BE(this._c | 0, 8)
92+
H.writeInt32BE(this._d | 0, 12)
93+
H.writeInt32BE(this._e | 0, 16)
9494

9595
return H
9696
}

sha1.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var Hash = require('./hash')
1212

1313
var W = new Array(80)
1414

15-
function Sha1() {
15+
function Sha1 () {
1616
this.init()
1717
this._w = W
1818

@@ -22,20 +22,20 @@ function Sha1() {
2222
inherits(Sha1, Hash)
2323

2424
Sha1.prototype.init = function () {
25-
this._a = 0x67452301
26-
this._b = 0xefcdab89
27-
this._c = 0x98badcfe
28-
this._d = 0x10325476
29-
this._e = 0xc3d2e1f0
25+
this._a = 0x67452301 | 0
26+
this._b = 0xefcdab89 | 0
27+
this._c = 0x98badcfe | 0
28+
this._d = 0x10325476 | 0
29+
this._e = 0xc3d2e1f0 | 0
3030

3131
return this
3232
}
3333

3434
/*
3535
* Bitwise rotate a 32-bit number to the left.
3636
*/
37-
function rol(num, cnt) {
38-
return (num << cnt) | (num >>> (32 - cnt));
37+
function rol (num, cnt) {
38+
return (num << cnt) | (num >>> (32 - cnt))
3939
}
4040

4141
Sha1.prototype._update = function (M) {
@@ -49,8 +49,8 @@ Sha1.prototype._update = function (M) {
4949

5050
var j = 0, k
5151

52-
function calcW() { return rol(W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16], 1) }
53-
function loop(w, f) {
52+
function calcW () { return rol(W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16], 1) }
53+
function loop (w, f) {
5454
W[j] = w
5555

5656
var t = rol(a, 5) + f + e + w + k
@@ -83,14 +83,13 @@ Sha1.prototype._update = function (M) {
8383
Sha1.prototype._hash = function () {
8484
var H = new Buffer(20)
8585

86-
H.writeInt32BE(this._a|0, 0)
87-
H.writeInt32BE(this._b|0, 4)
88-
H.writeInt32BE(this._c|0, 8)
89-
H.writeInt32BE(this._d|0, 12)
90-
H.writeInt32BE(this._e|0, 16)
86+
H.writeInt32BE(this._a | 0, 0)
87+
H.writeInt32BE(this._b | 0, 4)
88+
H.writeInt32BE(this._c | 0, 8)
89+
H.writeInt32BE(this._d | 0, 12)
90+
H.writeInt32BE(this._e | 0, 16)
9191

9292
return H
9393
}
9494

9595
module.exports = Sha1
96-

sha224.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,40 @@
77
*/
88

99
var inherits = require('inherits')
10-
var SHA256 = require('./sha256')
10+
var Sha256 = require('./sha256')
1111
var Hash = require('./hash')
1212

1313
var W = new Array(64)
1414

15-
function Sha224() {
15+
function Sha224 () {
1616
this.init()
1717

1818
this._w = W // new Array(64)
1919

2020
Hash.call(this, 64, 56)
2121
}
2222

23-
inherits(Sha224, SHA256)
23+
inherits(Sha224, Sha256)
2424

2525
Sha224.prototype.init = function () {
26-
this._a = 0xc1059ed8|0
27-
this._b = 0x367cd507|0
28-
this._c = 0x3070dd17|0
29-
this._d = 0xf70e5939|0
30-
this._e = 0xffc00b31|0
31-
this._f = 0x68581511|0
32-
this._g = 0x64f98fa7|0
33-
this._h = 0xbefa4fa4|0
26+
this._a = 0xc1059ed8 | 0
27+
this._b = 0x367cd507 | 0
28+
this._c = 0x3070dd17 | 0
29+
this._d = 0xf70e5939 | 0
30+
this._e = 0xffc00b31 | 0
31+
this._f = 0x68581511 | 0
32+
this._g = 0x64f98fa7 | 0
33+
this._h = 0xbefa4fa4 | 0
3434

3535
return this
3636
}
3737

3838
Sha224.prototype._hash = function () {
3939
var H = new Buffer(28)
4040

41-
H.writeInt32BE(this._a, 0)
42-
H.writeInt32BE(this._b, 4)
43-
H.writeInt32BE(this._c, 8)
41+
H.writeInt32BE(this._a, 0)
42+
H.writeInt32BE(this._b, 4)
43+
H.writeInt32BE(this._c, 8)
4444
H.writeInt32BE(this._d, 12)
4545
H.writeInt32BE(this._e, 16)
4646
H.writeInt32BE(this._f, 20)

0 commit comments

Comments
 (0)