Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sha512-256 and sha512-224 #67

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ node_js:
- "5"
- "6"
- "7"
- "8"
- "10"
- "12"
- "14"
env:
matrix:
- TEST_SUITE=unit
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ exports.sha224 = require('./sha224')
exports.sha256 = require('./sha256')
exports.sha384 = require('./sha384')
exports.sha512 = require('./sha512')
exports['sha512-224'] = require('./sha512-224')
exports['sha512-256'] = require('./sha512-256')
53 changes: 53 additions & 0 deletions sha512-224.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var inherits = require('inherits')
var Sha512 = require('./sha512')
var Hash = require('./hash')
var Buffer = require('safe-buffer').Buffer

var W = new Array(160)

function Sha512h256 () {
this.init()
this._w = W

Hash.call(this, 128, 112)
}

inherits(Sha512h256, Sha512)

Sha512h256.prototype.init = function () {
this._ah = 0x8c3d37c8
this._bh = 0x73e19966
this._ch = 0x1dfab7ae
this._dh = 0x679dd514
this._eh = 0x0f6d2b69
this._fh = 0x77e36f73
this._gh = 0x3f9d85a8
this._hh = 0x1112e6ad

this._al = 0x19544da2
this._bl = 0x89dcd4d6
this._cl = 0x32ff9c82
this._dl = 0x582f9fcf
this._el = 0x7bd44da8
this._fl = 0x04c48942
this._gl = 0x6a1d36c8
this._hl = 0x91d692a1

return this
}

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

H.writeInt32BE(this._ah, 0)
H.writeInt32BE(this._al, 4)
H.writeInt32BE(this._bh, 8)
H.writeInt32BE(this._bl, 12)
H.writeInt32BE(this._ch, 16)
H.writeInt32BE(this._cl, 20)
H.writeInt32BE(this._dh, 24)

return H
}

module.exports = Sha512h256
54 changes: 54 additions & 0 deletions sha512-256.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var inherits = require('inherits')
var Sha512 = require('./sha512')
var Hash = require('./hash')
var Buffer = require('safe-buffer').Buffer

var W = new Array(160)

function Sha512h256 () {
this.init()
this._w = W

Hash.call(this, 128, 112)
}

inherits(Sha512h256, Sha512)

Sha512h256.prototype.init = function () {
this._ah = 0x22312194
this._bh = 0x9f555fa3
this._ch = 0x2393b86b
this._dh = 0x96387719
this._eh = 0x96283ee2
this._fh = 0xbe5e1e25
this._gh = 0x2b0199fc
this._hh = 0x0eb72ddc

this._al = 0xfc2bf72c
this._bl = 0xc84c64c2
this._cl = 0x6f53b151
this._dl = 0x5940eabd
this._el = 0xa88effe3
this._fl = 0x53863992
this._gl = 0x2c85b8aa
this._hl = 0x81c52ca2

return this
}

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

H.writeInt32BE(this._ah, 0)
H.writeInt32BE(this._al, 4)
H.writeInt32BE(this._bh, 8)
H.writeInt32BE(this._bl, 12)
H.writeInt32BE(this._ch, 16)
H.writeInt32BE(this._cl, 20)
H.writeInt32BE(this._dh, 24)
H.writeInt32BE(this._dl, 28)

return H
}

module.exports = Sha512h256
16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var crypto = require('crypto')
var tape = require('tape')
var Sha1 = require('../').sha1
var sha = require('../')

var inputs = [
['', 'ascii'],
Expand All @@ -25,6 +26,21 @@ tape("hash is the same as node's crypto", function (t) {
t.end()
})

tape("hash is the same as node's crypto for all algos provided by node", function (t) {
var hashes = crypto.getHashes()
Object.keys(sha).forEach(function (alg) {
if (hashes.indexOf(alg) === -1) return // skip unsupported by current Node.js version
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really test newly added sha512-224 / sha512-256?

Copy link
Author

@ChALkeR ChALkeR Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fanatid #73 should fail (introduces 6be33ae)

But it looks like CI is disabled for some reason?

inputs.forEach(function (v) {
var a = new sha[alg]().update(v[0], v[1]).digest('hex')
var e = crypto.createHash(alg).update(v[0], v[1]).digest('hex')
console.log(alg, a, '==', e)
t.equal(a, e)
})
})

t.end()
})

tape('call update multiple times', function (t) {
inputs.forEach(function (v) {
var hash = new Sha1()
Expand Down