Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sha.js

Package Overview
Dependencies
Maintainers
2
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sha.js - npm Package Compare versions

Comparing version 2.4.8 to 2.4.9

2

bin.js

@@ -31,7 +31,5 @@ #! /usr/bin/env node

pipe(argv[0], process.stdin)
} else if (argv.length) {
if (/--help|-h/.test(argv[0])) {
usage()
} else {

@@ -38,0 +36,0 @@ var filename = argv.pop()

68

hash.js

@@ -0,8 +1,9 @@

var Buffer = require('safe-buffer').Buffer
// prototype class for hash functions
function Hash (blockSize, finalSize) {
this._block = new Buffer(blockSize)
this._block = Buffer.alloc(blockSize)
this._finalSize = finalSize
this._blockSize = blockSize
this._len = 0
this._s = 0
}

@@ -13,27 +14,27 @@

enc = enc || 'utf8'
data = new Buffer(data, enc)
data = Buffer.from(data, enc)
}
var l = this._len += data.length
var s = this._s || 0
var f = 0
var buffer = this._block
var block = this._block
var blockSize = this._blockSize
var length = data.length
var accum = this._len
while (s < l) {
var t = Math.min(data.length, f + this._blockSize - (s % this._blockSize))
var ch = (t - f)
for (var offset = 0; offset < length;) {
var assigned = accum % blockSize
var remainder = Math.min(length - offset, blockSize - assigned)
for (var i = 0; i < ch; i++) {
buffer[(s % this._blockSize) + i] = data[i + f]
for (var i = 0; i < remainder; i++) {
block[assigned + i] = data[offset + i]
}
s += ch
f += ch
accum += remainder
offset += remainder
if ((s % this._blockSize) === 0) {
this._update(buffer)
if ((accum % blockSize) === 0) {
this._update(block)
}
}
this._s = s
this._len += length
return this

@@ -43,12 +44,11 @@ }

Hash.prototype.digest = function (enc) {
// Suppose the length of the message M, in bits, is l
var l = this._len * 8
var rem = this._len % this._blockSize
// Append the bit 1 to the end of the message
this._block[this._len % this._blockSize] = 0x80
this._block[rem] = 0x80
// and then k zero bits, where k is the smallest non-negative solution to the equation (l + 1 + k) === finalSize mod blockSize
this._block.fill(0, this._len % this._blockSize + 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 (l % (this._blockSize * 8) >= this._finalSize * 8) {
if (rem >= this._finalSize) {
this._update(this._block)

@@ -58,8 +58,20 @@ this._block.fill(0)

// to this append the block which is equal to the number l written in binary
// TODO: handle case where l is > Math.pow(2, 29)
this._block.writeInt32BE(l, this._blockSize - 4)
var bits = this._len * 8
var hash = this._update(this._block) || this._hash()
// uint32
if (bits <= 0xffffffff) {
this._block.writeUInt32BE(bits, this._blockSize - 4)
// uint64
} else {
var lowBits = bits & 0xffffffff
var highBits = (bits - lowBits) / 0x100000000
this._block.writeUInt32BE(highBits, this._blockSize - 8)
this._block.writeUInt32BE(lowBits, this._blockSize - 4)
}
this._update(this._block)
var hash = this._hash()
return enc ? hash.toString(enc) : hash

@@ -66,0 +78,0 @@ }

{
"name": "sha.js",
"description": "Streamable SHA hashes in pure javascript",
"version": "2.4.8",
"version": "2.4.9",
"homepage": "https://github.com/crypto-browserify/sha.js",

@@ -11,3 +11,4 @@ "repository": {

"dependencies": {
"inherits": "^2.0.1"
"inherits": "^2.0.1",
"safe-buffer": "^5.0.1"
},

@@ -17,3 +18,3 @@ "devDependencies": {

"hash-test-vectors": "^1.3.1",
"standard": "^4.0.0",
"standard": "^10.0.2",
"tape": "~2.3.2",

@@ -20,0 +21,0 @@ "typedarray": "0.0.6"

# sha.js
[![NPM Package](https://img.shields.io/npm/v/sha.js.svg?style=flat-square)](https://www.npmjs.org/package/sha.js)
[![Build Status](https://img.shields.io/travis/crypto-browserify/sha.js.svg?branch=master&style=flat-square)](https://travis-ci.org/crypto-browserify/sha.js)
[![Dependency status](https://img.shields.io/david/crypto-browserify/sha.js.svg?style=flat-square)](https://david-dm.org/crypto-browserify/sha.js#info=dependencies)
Streamable SHA hashes in pure javascript.
[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
[![build status](https://secure.travis-ci.org/crypto-browserify/sha.js.png)](http://travis-ci.org/crypto-browserify/sha.js)
[![NPM](http://img.shields.io/npm/v/sha.js.svg)](https://www.npmjs.org/package/sha.js)
Node style `SHA` on pure JavaScript.
```js
var shajs = require('sha.js')
## Example
console.log(shajs('sha256').update('42').digest('hex'))
// => 73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049
console.log(new shajs.sha256().update('42').digest('hex'))
// => 73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049
``` js
var createHash = require('sha.js')
var sha256 = createHash('sha256')
var sha512 = createHash('sha512')
var h = sha256.update('abc', 'utf8').digest('hex')
console.log(h) //ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
//LEGACY, do not use in new systems:
var sha0 = createHash('sha')
var sha1 = createHash('sha1')
var sha256stream = shajs('sha256')
sha256stream.end('42')
console.log(sha256stream.read().toString('hex'))
// => 73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049
```
## supported hashes
`sha.js` currently implements:
sha.js currently implements:
- SHA (SHA-0) -- **legacy, do not use in new systems**
- SHA-1 -- **legacy, do not use in new systems**
- SHA-224
- SHA-256
- SHA-384
- SHA-512
* sha256
* sha512
* sha1 (legacy, no not use in new systems)
* sha (legacy, no not use in new systems)
## Note
## Not an actual stream
Note, this doesn't actually implement a stream, but wrapping this in a stream is trivial.
but is does update incrementally, so you can hash things larger than ram, and also, since it reuses
the typedarrays, it uses a constant amount of memory (except when using base64 or utf8 encoding,
see code comments)
It does update incrementally, so you can hash things larger than RAM, as it uses a constant amount of memory (except when using base64 or utf8 encoding, see code comments).
## Acknowledgements
This work is derived from Paul Johnston's [A JavaScript implementation of the Secure Hash Algorithm](http://pajhome.org.uk/crypt/md5/sha1.html).
This work is derived from Paul Johnston's ["A JavaScript implementation of the Secure Hash Algorithm"]
(http://pajhome.org.uk/crypt/md5/sha1.html)
## License
MIT
## LICENSE [MIT](LICENSE)

@@ -11,2 +11,3 @@ /*

var Hash = require('./hash')
var Buffer = require('safe-buffer').Buffer

@@ -83,3 +84,3 @@ var K = [

Sha.prototype._hash = function () {
var H = new Buffer(20)
var H = Buffer.allocUnsafe(20)

@@ -86,0 +87,0 @@ H.writeInt32BE(this._a | 0, 0)

@@ -12,2 +12,3 @@ /*

var Hash = require('./hash')
var Buffer = require('safe-buffer').Buffer

@@ -88,3 +89,3 @@ var K = [

Sha1.prototype._hash = function () {
var H = new Buffer(20)
var H = Buffer.allocUnsafe(20)

@@ -91,0 +92,0 @@ H.writeInt32BE(this._a | 0, 0)

@@ -12,2 +12,3 @@ /**

var Hash = require('./hash')
var Buffer = require('safe-buffer').Buffer

@@ -40,3 +41,3 @@ var W = new Array(64)

Sha224.prototype._hash = function () {
var H = new Buffer(28)
var H = Buffer.allocUnsafe(28)

@@ -43,0 +44,0 @@ H.writeInt32BE(this._a, 0)

@@ -11,2 +11,3 @@ /**

var Hash = require('./hash')
var Buffer = require('safe-buffer').Buffer

@@ -121,3 +122,3 @@ var K = [

Sha256.prototype._hash = function () {
var H = new Buffer(32)
var H = Buffer.allocUnsafe(32)

@@ -124,0 +125,0 @@ H.writeInt32BE(this._a, 0)

var inherits = require('inherits')
var SHA512 = require('./sha512')
var Hash = require('./hash')
var Buffer = require('safe-buffer').Buffer

@@ -39,3 +40,3 @@ var W = new Array(160)

Sha384.prototype._hash = function () {
var H = new Buffer(48)
var H = Buffer.allocUnsafe(48)

@@ -42,0 +43,0 @@ function writeInt64BE (h, l, offset) {

var inherits = require('inherits')
var Hash = require('./hash')
var Buffer = require('safe-buffer').Buffer

@@ -240,3 +241,3 @@ var K = [

Sha512.prototype._hash = function () {
var H = new Buffer(64)
var H = Buffer.allocUnsafe(64)

@@ -243,0 +244,0 @@ function writeInt64BE (h, l, offset) {

@@ -1,5 +0,3 @@

var hexpp = require('../hexpp').defaults({ bigendian: false })
var tape = require('tape')
var Hash = require('../hash')
var hex = '0A1B2C3D4E5F6G7H'

@@ -9,9 +7,6 @@

t.equal(a.length, b.length)
for (var i = 0; i < a.length; i++) {
t.equal(a[i], b[i])
}
t.equal(a.toString('hex'), b.toString('hex'))
}
var hexBuf = new Buffer([48, 65, 49, 66, 50, 67, 51, 68, 52, 69, 53, 70, 54, 71, 55, 72])
var hexBuf = Buffer.from('0A1B2C3D4E5F6G7H', 'utf8')
var count16 = {

@@ -21,3 +16,3 @@ strings: ['0A1B2C3D4E5F6G7H'],

hexBuf,
new Buffer([ 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128])
Buffer.from('80000000000000000000000000000080', 'hex')
]

@@ -29,3 +24,3 @@ }

buffers: [
new Buffer([ 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ])
Buffer.from('80000000000000000000000000000000', 'hex')
]

@@ -35,8 +30,8 @@ }

var multi = {
strings: ['abcd', 'efhijk', 'lmnopq'],
buffers: [
new Buffer('abcdefhijklmnopq', 'ascii'),
new Buffer([128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128])
]
}
strings: ['abcd', 'efhijk', 'lmnopq'],
buffers: [
Buffer.from('abcdefhijklmnopq', 'ascii'),
Buffer.from('80000000000000000000000000000080', 'hex')
]
}

@@ -48,3 +43,3 @@ var long = {

hexBuf,
new Buffer([128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0])
Buffer.from('80000000000000000000000000000100', 'hex')
]

@@ -55,5 +50,4 @@ }

tape(name, function (t) {
var h = new Hash(16, 8)
var hash = new Buffer(20)
var hash = Buffer.alloc(20)
var n = 2

@@ -65,8 +59,2 @@ var expected = data.buffers.slice()

var e = expected.shift()
console.log('---block---')
console.log(hexpp(block), block.length)
console.log('---e---')
console.log(hexpp(e), block.length)
console.log(block)
equal(t, block, e)

@@ -77,3 +65,4 @@

}
}
h._hash = function () {
return hash

@@ -80,0 +69,0 @@ }

@@ -74,4 +74,4 @@ var crypto = require('crypto')

var s = v[0].substring(i, (i + 1) * 2)
hash.update(new Buffer(s, 'ascii').toString('hex'), 'hex')
_hash.update(new Buffer(s, 'ascii').toString('hex'), 'hex')
hash.update(Buffer.from(s, 'ascii').toString('hex'), 'hex')
_hash.update(Buffer.from(s, 'ascii').toString('hex'), 'hex')
}

@@ -87,1 +87,16 @@ var a = hash.digest('hex')

})
tape('call digest for more than MAX_UINT32 bits of data', function (t) {
var _hash = crypto.createHash('sha1')
var hash = new Sha1()
var bigData = Buffer.alloc(Math.pow(2, 32) / 8)
hash.update(bigData)
_hash.update(bigData)
var a = hash.digest('hex')
var e = _hash.digest('hex')
t.equal(a, e)
t.end()
})
var tape = require('tape')
var vectors = require('hash-test-vectors')
// var from = require('bops/typedarray/from')
var Buffer = require('buffer').Buffer
var hexpp = require('../hexpp')
var Buffer = require('safe-buffer').Buffer

@@ -17,7 +16,6 @@ var createHash = require('../')

console.log('INPUT', v.input)
console.log(hexpp(new Buffer(v.input, 'base64')))
console.log(new Buffer(v.input, 'base64').toString('hex'))
console.log(Buffer.from(v.input, 'base64').toString('hex'))
}
var buf = new Buffer(v.input, 'base64')
var buf = Buffer.from(v.input, 'base64')
t.equal(createHash(alg).update(buf).digest('hex'), v[alg])

@@ -62,3 +60,2 @@

})
}

@@ -68,3 +65,2 @@

makeTest(process.argv[2], parseInt(process.argv[3], 10), true)
} else {

@@ -71,0 +67,0 @@ vectors.forEach(function (v, i) {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc