Comparing version 0.4.0 to 0.5.0
@@ -6,12 +6,12 @@ | ||
var REGEXP = { | ||
'md5': /^[0-9a-f]{32}$/i, | ||
'sha1': /^[0-9a-f]{40}$/i, | ||
'ripemd160': /^[0-9a-f]{40}$/i, | ||
'sha256': /^[0-9a-f]{64}$/i, | ||
'sha512': /^[0-9a-f]{128}$/i, | ||
'whirlpool': /^[0-9a-f]{128}$/i, | ||
'DEFAULT': /^$/ | ||
'md5': "^[0-9a-f]{32}$", | ||
'sha1': "^[0-9a-f]{40}$", | ||
'ripemd160': "^[0-9a-f]{40}$", | ||
'sha256': "^[0-9a-f]{64}$", | ||
'sha512': "^[0-9a-f]{128}$", | ||
'whirlpool': "^[0-9a-f]{128}$", | ||
'DEFAULT': "^$" | ||
}; | ||
function Merkle (hashFunc, hashFuncName) { | ||
function Merkle (hashFunc, hashFuncName, useUpperCaseForHash) { | ||
@@ -24,3 +24,8 @@ var that = this; | ||
that.hashResultRegexp = REGEXP[hashFuncName] || REGEXP.DEFAULT; | ||
var regexpStr = REGEXP[hashFuncName] || REGEXP.DEFAULT; | ||
if (useUpperCaseForHash) { | ||
// Use only capital letters if upper case is enabled | ||
regexpStr = regexpStr.replace('a', 'A').replace('f', 'F'); | ||
} | ||
that.hashResultRegexp = new RegExp(regexpStr); | ||
that.leaves = []; | ||
@@ -34,6 +39,10 @@ that.treeDepth = 0; | ||
// Push leaf without hashing it since it is already a hash | ||
that.leaves.push(anyData.toUpperCase()); | ||
that.leaves.push(anyData); | ||
} | ||
else{ | ||
that.leaves.push(hashFunc(anyData).toUpperCase()); | ||
var hash = hashFunc(anyData); | ||
if (useUpperCaseForHash) { | ||
hash = hash.toUpperCase(); | ||
} | ||
that.leaves.push(hash); | ||
} | ||
@@ -89,4 +98,9 @@ return that; | ||
var nodes = []; | ||
var hash; | ||
for (var i = 0; i < leaves.length - 1; i = i + 2) { | ||
nodes[i/2] = hashFunc(leaves[i] + leaves[i+1]).toUpperCase(); | ||
hash = hashFunc(leaves[i] + leaves[i+1]); | ||
if (useUpperCaseForHash) { | ||
hash = hash.toUpperCase(); | ||
} | ||
nodes[i/2] = hash; | ||
} | ||
@@ -173,3 +187,3 @@ if(remainder === 1){ | ||
module.exports = function (hashFuncName) { | ||
module.exports = function (hashFuncName, useUpperCaseForHash) { | ||
return new Merkle(function (input) { | ||
@@ -182,3 +196,6 @@ if (hashFuncName === 'none') { | ||
} | ||
}, hashFuncName); | ||
}, hashFuncName, | ||
// Use upper case y default | ||
useUpperCaseForHash !== false); | ||
}; |
{ | ||
"name": "merkle", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Javascript implementation of merkle trees", | ||
@@ -5,0 +5,0 @@ "main": "merkle.js", |
@@ -330,2 +330,51 @@ var should = require('should'); | ||
}); | ||
}); | ||
}); | ||
describe('using lowercase', function() { | ||
var USE_LOWERCASE = false; // This is a constant, false value is coherent with the name | ||
describe("merkle stream ['a', 'b', 'c', 'd', 'e'] with 'md5')", function(){ | ||
var root = null; | ||
before(function (done) { | ||
var m = merkle('md5', USE_LOWERCASE); | ||
m.pipe(es.mapSync(function (data) { | ||
root = data; | ||
done(); | ||
})); | ||
abcde.forEach(function(c){ | ||
m.write(c); | ||
}); | ||
m.end(); | ||
}); | ||
it("should have root '14bb879020adb0cd3bf3935576f58f25'", function(){ | ||
assert.equal(root, "14bb879020adb0cd3bf3935576f58f25"); | ||
}); | ||
}); | ||
describe("merkle stream ['a', 'b', 'c', 'd', 'e'] with 'none')", function(){ | ||
var root = null; | ||
before(function (done) { | ||
var m = merkle('none', USE_LOWERCASE); | ||
m.pipe(es.mapSync(function (data) { | ||
root = data; | ||
done(); | ||
})); | ||
abcde.forEach(function(c){ | ||
m.write(c); | ||
}); | ||
m.end(); | ||
}); | ||
it("should have root 'abcde'", function(){ | ||
assert.equal(root, "abcde"); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
25116
452