Add SHA-1 (used for signing)

This commit is contained in:
Kagami Hiiragi 2015-01-16 18:35:40 +03:00
parent 1d8f694c84
commit 8fa539c61b
5 changed files with 37 additions and 12 deletions

View File

@ -15,8 +15,9 @@ API documentation is available [here](https://bitchan.github.io/bitmessage/docs/
## Feature matrix (both Browser and Node) ## Feature matrix (both Browser and Node)
- [x] Crypto - [x] Crypto
- [x] SHA-512 - [x] SHA-1
- [x] SHA-256 - [x] SHA-256
- [x] SHA-512
- [x] RIPEMD-160 - [x] RIPEMD-160
- [x] PRNG - [x] PRNG
- [x] ECC keys manipulation - [x] ECC keys manipulation

View File

@ -10,12 +10,12 @@ var eccrypto = require("eccrypto");
var platform = require("./platform"); var platform = require("./platform");
/** /**
* Calculate SHA-512 hash. * Calculate SHA-1 hash.
* @param {Buffer} buf - Input data * @param {Buffer} buf - Input data
* @return {Buffer} Resulting hash. * @return {Buffer} Resulting hash.
* @function * @function
*/ */
exports.sha512 = platform.sha512; exports.sha1 = platform.sha1;
/** /**
* Calculate SHA-256 hash. * Calculate SHA-256 hash.
@ -25,6 +25,14 @@ exports.sha512 = platform.sha512;
*/ */
exports.sha256 = platform.sha256; exports.sha256 = platform.sha256;
/**
* Calculate SHA-512 hash.
* @param {Buffer} buf - Input data
* @return {Buffer} Resulting hash.
* @function
*/
exports.sha512 = platform.sha512;
/** /**
* Calculate RIPEMD-160 hash. * Calculate RIPEMD-160 hash.
* @param {Buffer} buf - Input data * @param {Buffer} buf - Input data

View File

@ -16,14 +16,18 @@ var BN = require("bn.js");
var work = require("webworkify"); var work = require("webworkify");
var assert = require("./util").assert; var assert = require("./util").assert;
exports.sha512 = function(buf) { exports.sha1 = function(buf) {
return new Sha512().update(buf).digest(); return new Buffer(hash.sha1().update(buf).digest());
}; };
exports.sha256 = function(buf) { exports.sha256 = function(buf) {
return new Buffer(hash.sha256().update(buf).digest()); return new Buffer(hash.sha256().update(buf).digest());
}; };
exports.sha512 = function(buf) {
return new Sha512().update(buf).digest();
};
exports.ripemd160 = function(buf) { exports.ripemd160 = function(buf) {
return new Buffer(hash.ripemd160().update(buf).digest()); return new Buffer(hash.ripemd160().update(buf).digest());
}; };
@ -69,7 +73,7 @@ exports.pow = function(opts) {
poolSize = poolSize || FAILBACK_POOL_SIZE; poolSize = poolSize || FAILBACK_POOL_SIZE;
var cancel; var cancel;
var promise = new Promise(function(resolve, reject) { var powp = new Promise(function(resolve, reject) {
assert(typeof poolSize === "number", "Bad pool size"); assert(typeof poolSize === "number", "Bad pool size");
assert(poolSize >= 1, "Pool size is too low"); assert(poolSize >= 1, "Pool size is too low");
assert(poolSize <= 1024, "Pool size is too high"); assert(poolSize <= 1024, "Pool size is too high");
@ -131,6 +135,8 @@ exports.pow = function(opts) {
}); });
// Allow to stop a POW via custom function added to the Promise // Allow to stop a POW via custom function added to the Promise
// instance. // instance.
promise.cancel = cancel; powp.cancel = cancel;
return promise; return powp;
}; };
exports.promise = Promise;

View File

@ -15,14 +15,18 @@ var worker = require("./worker");
var createHash = crypto.createHash; var createHash = crypto.createHash;
exports.sha512 = function(buf) { exports.sha1 = function(buf) {
return createHash("sha512").update(buf).digest(); return createHash("sha1").update(buf).digest();
}; };
exports.sha256 = function(buf) { exports.sha256 = function(buf) {
return createHash("sha256").update(buf).digest(); return createHash("sha256").update(buf).digest();
}; };
exports.sha512 = function(buf) {
return createHash("sha512").update(buf).digest();
};
exports.ripemd160 = function(buf) { exports.ripemd160 = function(buf) {
return createHash("ripemd160").update(buf).digest(); return createHash("ripemd160").update(buf).digest();
}; };
@ -69,3 +73,5 @@ exports.pow = function(opts) {
); );
}); });
}; };
exports.promise = promise;

View File

@ -29,14 +29,18 @@ var Address = bitmessage.Address;
var UserAgent = bitmessage.UserAgent; var UserAgent = bitmessage.UserAgent;
describe("Crypto", function() { describe("Crypto", function() {
it("should implement SHA-512 hash", function() { it("should implement SHA-1 hash", function() {
expect(bmcrypto.sha512(Buffer("test")).toString("hex")).to.equal("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"); expect(bmcrypto.sha1(Buffer("test")).toString("hex")).to.equal("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3");
}); });
it("should implement SHA-256 hash", function() { it("should implement SHA-256 hash", function() {
expect(bmcrypto.sha256(Buffer("test")).toString("hex")).to.equal("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"); expect(bmcrypto.sha256(Buffer("test")).toString("hex")).to.equal("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08");
}); });
it("should implement SHA-512 hash", function() {
expect(bmcrypto.sha512(Buffer("test")).toString("hex")).to.equal("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff");
});
it("should implement RIPEMD-160 hash", function() { it("should implement RIPEMD-160 hash", function() {
expect(bmcrypto.ripemd160(Buffer("test")).toString("hex")).to.equal("5e52fee47e6b070565f74372468cdc699de89107"); expect(bmcrypto.ripemd160(Buffer("test")).toString("hex")).to.equal("5e52fee47e6b070565f74372468cdc699de89107");
}); });