2014-04-08 14:13:03 +02:00
|
|
|
// Crypto, crypto, where art thou crypto
|
2014-05-10 01:55:55 +02:00
|
|
|
var assert = require('assert')
|
2014-04-08 14:13:03 +02:00
|
|
|
var CryptoJS = require('crypto-js')
|
|
|
|
var crypto = require('crypto')
|
|
|
|
var convert = require('./convert')
|
|
|
|
|
|
|
|
function hash160(buffer) {
|
|
|
|
|
|
|
|
var step1 = sha256(buffer)
|
|
|
|
|
|
|
|
var step2a = convert.bytesToWordArray(step1)
|
|
|
|
var step2b = CryptoJS.RIPEMD160(step2a)
|
|
|
|
|
2014-04-08 15:44:55 +02:00
|
|
|
return new Buffer(convert.wordArrayToBytes(step2b))
|
2014-04-08 14:13:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function hash256(buffer) {
|
|
|
|
return sha256(sha256(buffer))
|
|
|
|
}
|
|
|
|
|
|
|
|
function sha1(buffer) {
|
|
|
|
return crypto.createHash('sha1').update(buffer).digest()
|
|
|
|
}
|
|
|
|
|
|
|
|
function sha256(buffer) {
|
|
|
|
return crypto.createHash('sha256').update(buffer).digest()
|
|
|
|
}
|
|
|
|
|
2014-05-10 01:55:55 +02:00
|
|
|
// FIXME: Name not consistent with others
|
2014-05-16 04:36:09 +02:00
|
|
|
function HmacSHA256(buffer, secret) {
|
|
|
|
return crypto.createHmac('sha256', secret).update(buffer).digest()
|
|
|
|
}
|
|
|
|
|
2014-05-10 01:55:55 +02:00
|
|
|
function HmacSHA512(data, secret) {
|
|
|
|
assert(Buffer.isBuffer(data), 'Expected Buffer for data, got ' + data)
|
|
|
|
assert(Buffer.isBuffer(secret), 'Expected Buffer for secret, got ' + secret)
|
|
|
|
|
|
|
|
var dataWords = convert.bytesToWordArray(data)
|
|
|
|
var secretWords = convert.bytesToWordArray(secret)
|
|
|
|
|
|
|
|
var hash = CryptoJS.HmacSHA512(dataWords, secretWords)
|
|
|
|
|
|
|
|
return new Buffer(convert.wordArrayToBytes(hash))
|
|
|
|
}
|
|
|
|
|
2014-04-08 14:13:03 +02:00
|
|
|
module.exports = {
|
|
|
|
sha1: sha1,
|
|
|
|
sha256: sha256,
|
|
|
|
hash160: hash160,
|
2014-05-10 01:55:55 +02:00
|
|
|
hash256: hash256,
|
2014-05-16 04:36:09 +02:00
|
|
|
HmacSHA256: HmacSHA256,
|
2014-05-10 01:55:55 +02:00
|
|
|
HmacSHA512: HmacSHA512
|
2014-03-08 06:02:40 +01:00
|
|
|
}
|