add shake algorithm

This commit is contained in:
Yi-Cyuan
2015-09-17 14:57:12 +08:00
parent 61300d2d34
commit 7423f9ef37
11 changed files with 160 additions and 139 deletions
+1
View File
@@ -15,6 +15,7 @@
</script>
<script src="test.js"></script>
<script src="test-keccak.js"></script>
<script src="test-shake.js"></script>
<script>
mocha.checkLeaks();
mocha.run();
+6
View File
@@ -7,12 +7,15 @@ sha3_512 = require('../src/sha3.js').sha3_512;
sha3_384 = require('../src/sha3.js').sha3_384;
sha3_256 = require('../src/sha3.js').sha3_256;
sha3_224 = require('../src/sha3.js').sha3_224;
shake_128 = require('../src/sha3.js').shake_128;
shake_256 = require('../src/sha3.js').shake_256;
require('./test-keccak.js');
require('./test.js');
delete require.cache[require.resolve('../src/sha3.js')]
delete require.cache[require.resolve('./test.js')]
delete require.cache[require.resolve('./test-keccak.js')]
delete require.cache[require.resolve('./test-shake.js')]
sha3_512 = null;
sha3_384 = null;
sha3_256 = null;
@@ -21,8 +24,11 @@ keccak_512 = null;
keccak_384 = null;
keccak_256 = null;
keccak_224 = null;
shake_128 = null;
shake_256 = null;
JS_SHA3_TEST = true;
require('../src/sha3.js');
require('./test-keccak.js');
require('./test-shake.js');
require('./test.js');
+47
View File
@@ -0,0 +1,47 @@
(function(shake_256, shake_128) {
describe('shake_128', function() {
context('with 256 output', function() {
it('should be equal', function() {
expect(shake_128('', 256)).to.be('7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26');
expect(shake_128('The quick brown fox jumps over the lazy dog', 256)).to.be('f4202e3c5852f9182a0430fd8144f0a74b95e7417ecae17db0f8cfeed0e3e66e');
expect(shake_128('The quick brown fox jumps over the lazy dof', 256)).to.be('853f4538be0db9621a6cea659a06c1107b1f83f02b13d18297bd39d7411cf10c');
});
});
context('with 8 output', function() {
it('should be equal', function() {
expect(shake_128('', 8)).to.be('7f');
expect(shake_128('The quick brown fox jumps over the lazy dog', 8)).to.be('f4');
expect(shake_128('The quick brown fox jumps over the lazy dof', 8)).to.be('85');
});
});
context('with more output', function() {
it('should be equal', function() {
expect(shake_128('', 16)).to.be('7f9c');
expect(shake_128('', 24)).to.be('7f9c2b');
});
});
context('with 8 output ArrayBuffer', function() {
it('should be equal', function() {
expect(shake_128.buffer('', 8).toHexString()).to.be('7f');
expect(shake_128.buffer('The quick brown fox jumps over the lazy dog', 8).toHexString()).to.be('f4');
expect(shake_128.buffer('The quick brown fox jumps over the lazy dof', 8).toHexString()).to.be('85');
});
});
});
describe('shake_256', function() {
context('with 512 output', function() {
it('should be equal', function() {
expect(shake_256('', 512)).to.be('46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762fd75dc4ddd8c0f200cb05019d67b592f6fc821c49479ab48640292eacb3b7c4be');
});
});
context('with 8 output', function() {
it('should be equal', function() {
expect(shake_256('', 8)).to.be('46');
});
});
});
})(shake_256, shake_128);
+15 -9
View File
@@ -1,13 +1,13 @@
ArrayBuffer.prototype.toHexString = function (argument) {
var array = new Uint8Array(this);
var hex = '';
for(var i = 0;i < array.length;++i) {
var c = array[i].toString('16');
hex += c.length == 1 ? '0' + c : c;
}
return hex;
};
(function(sha3_512, sha3_384, sha3_256, sha3_224) {
ArrayBuffer.prototype.toHexString = function (argument) {
var array = new Uint8Array(this);
var hex = '';
for(var i = 0;i < array.length;++i) {
var c = array[i].toString('16');
hex += c.length == 1 ? '0' + c : c;
}
return hex;
};
describe('sha3_512', function() {
context('when ascii', function() {
context('and less than 128 bytes', function() {
@@ -74,6 +74,12 @@ ArrayBuffer.prototype.toHexString = function (argument) {
expect(sha3_512.buffer('').toHexString()).to.be('a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26');
});
});
context('#hex', function() {
it('should be equal', function() {
expect(sha3_512.hex('')).to.be(sha3_512(''));
});
});
});
describe('sha3_384', function() {