-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,117 additions
and
832 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
}, | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.