Browse Source

crypto: remove cryptojs

hk-custom-address
Daniel Cousens 10 years ago
parent
commit
c96b862530
  1. 1
      package.json
  2. 32
      src/convert.js
  3. 16
      src/crypto.js
  4. 1
      src/index.js
  5. 27
      test/convert.js
  6. 25
      test/fixtures/convert.json

1
package.json

@ -75,7 +75,6 @@
"bigi": "1.1.0",
"bs58": "1.2.1",
"bs58check": "1.0.1",
"crypto-js": "3.1.2-3",
"crypto-browserify": "3.0.0",
"ecurve": "1.0.0"
}

32
src/convert.js

@ -1,32 +0,0 @@
var assert = require('assert')
var Crypto = require('crypto-js')
var WordArray = Crypto.lib.WordArray
function bufferToWordArray(buffer) {
assert(Buffer.isBuffer(buffer), 'Expected Buffer, got', buffer)
var words = []
for (var i = 0, b = 0; i < buffer.length; i++, b += 8) {
words[b >>> 5] |= buffer[i] << (24 - b % 32)
}
return new WordArray.init(words, buffer.length)
}
function wordArrayToBuffer(wordArray) {
assert(Array.isArray(wordArray.words), 'Expected WordArray, got' + wordArray)
var words = wordArray.words
var buffer = new Buffer(words.length * 4)
words.forEach(function(value, i) {
buffer.writeInt32BE(value & -1, i * 4)
})
return buffer
}
module.exports = {
bufferToWordArray: bufferToWordArray,
wordArrayToBuffer: wordArrayToBuffer
}

16
src/crypto.js

@ -1,8 +1,4 @@
// Crypto, crypto, where art thou crypto
var assert = require('assert')
var CryptoJS = require('crypto-js')
var crypto = require('crypto')
var convert = require('./convert')
function hash160(buffer) {
return ripemd160(sha256(buffer))
@ -29,16 +25,8 @@ function HmacSHA256(buffer, secret) {
return crypto.createHmac('sha256', secret).update(buffer).digest()
}
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.bufferToWordArray(data)
var secretWords = convert.bufferToWordArray(secret)
var hash = CryptoJS.HmacSHA512(dataWords, secretWords)
return convert.wordArrayToBuffer(hash)
function HmacSHA512(buffer, secret) {
return crypto.createHmac('sha512', secret).update(buffer).digest()
}
module.exports = {

1
src/index.js

@ -1,7 +1,6 @@
module.exports = {
Address: require('./address'),
bufferutils: require('./bufferutils'),
convert: require('./convert'),
crypto: require('./crypto'),
ecdsa: require('./ecdsa'),
ECKey: require('./eckey'),

27
test/convert.js

@ -1,27 +0,0 @@
var assert = require('assert')
var convert = require('../src/convert')
var fixtures = require('./fixtures/convert')
describe('convert', function() {
describe('bufferToWordArray', function() {
fixtures.valid.forEach(function(f) {
it('converts ' + f.hex + ' correctly', function() {
var buffer = new Buffer(f.hex, 'hex')
var result = convert.bufferToWordArray(buffer)
assert.deepEqual(result, f.wordArray)
})
})
})
describe('wordArrayToBuffer', function() {
fixtures.valid.forEach(function(f) {
it('converts to ' + f.hex + ' correctly', function() {
var resultHex = convert.wordArrayToBuffer(f.wordArray).toString('hex')
assert.deepEqual(resultHex, f.hex)
})
})
})
})

25
test/fixtures/convert.json

@ -1,25 +0,0 @@
{
"valid": [
{
"hex": "0000000000000000000000000000000000000000",
"wordArray": {
"words": [0, 0, 0, 0, 0],
"sigBytes": 20
}
},
{
"hex": "62e907b15cbf27d5425399ebf6f0fb50ebb88f18",
"wordArray": {
"words": [1659439025, 1556031445, 1112775147, -151979184, -340226280],
"sigBytes": 20
}
},
{
"hex": "ffffffffffffffffffffffffffffffffffffffff",
"wordArray": {
"words": [-1, -1, -1, -1, -1],
"sigBytes": 20
}
}
]
}
Loading…
Cancel
Save