commit b249e417a75b47199caaaf94239b2b0d2f073485 Author: Chen Yi-Cyuan Date: Mon Feb 23 18:23:14 2015 +0800 Initial release diff --git a/.covignore b/.covignore new file mode 100644 index 0000000..016f806 --- /dev/null +++ b/.covignore @@ -0,0 +1 @@ +/tests/ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2187be0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +covreporter diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..aabe614 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +language: node_js +node_js: + - "0.11" + - "0.10" + - "0.8" +before_install: + - npm install mocha -g + - npm install coveralls -g + - npm install mocha-lcov-reporter -g +script: npm run-script coveralls +branches: + only: + - master diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..bbbf7d2 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +# v0.1.0 / 2015-02-23 + +* Initial release diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..df6ab18 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,20 @@ +Copyright 2015 emn178@gmail.com + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..6a6476e --- /dev/null +++ b/README.md @@ -0,0 +1,131 @@ +# js-sha3 +[![Build Status](https://travis-ci.org/emn178/js-sha3.svg?branch=master)](https://travis-ci.org/emn178/js-sha3) +[![Coverage Status](https://coveralls.io/repos/emn178/js-sha3/badge.svg?branch=master)](https://coveralls.io/r/emn178/js-sha3?branch=master) +[![NPM](https://nodei.co/npm/js-sha3.png?stars&downloads)](https://nodei.co/npm/js-sha3/) +A simple SHA-3 / Keccak hash function for JavaScript supports UTF-8 encoding. + +## Download +[Compress](https://raw.github.com/emn178/js-sha3/master/build/sha3.min.js) +[Uncompress](https://raw.github.com/emn178/js-sha3/master/src/sha3.js) + +## Installation +You can also install js-sha3 by using Bower. + + bower install js-sha3 + +For node.js, you can use this command to install: + + npm install js-sha3 + +## Usage +You could use like this: +```JavaScript +sha3_512('Message to hash'); +sha3_384('Message to hash'); +sha3_256('Message to hash'); +sha3_224('Message to hash'); +``` +If you use node.js, you should require the module first: +```JavaScript +sha3_512 = require('js-sha3').sha3_512; +sha3_384 = require('js-sha3').sha3_384; +sha3_256 = require('js-sha3').sha3_256; +sha3_224 = require('js-sha3').sha3_224; +``` + +## Example +Code +```JavaScript +sha3_512(''); +sha3_512('The quick brown fox jumps over the lazy dog'); +sha3_512('The quick brown fox jumps over the lazy dog.'); +sha3_384(''); +sha3_384('The quick brown fox jumps over the lazy dog'); +sha3_384('The quick brown fox jumps over the lazy dog.'); +sha3_256(''); +sha3_256('The quick brown fox jumps over the lazy dog'); +sha3_256('The quick brown fox jumps over the lazy dog.'); +sha3_224(''); +sha3_224('The quick brown fox jumps over the lazy dog'); +sha3_224('The quick brown fox jumps over the lazy dog.'); +``` +Output + + 0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e + d135bb84d0439dbac432247ee573a23ea7d3c9deb2a968eb31d47c4fb45f1ef4422d6c531b5b9bd6f449ebcc449ea94d0a8f05f62130fda612da53c79659f609 + ab7192d2b11f51c7dd744e7b3441febf397ca07bf812cceae122ca4ded6387889064f8db9230f173f6d1ab6e24b6e50f065b039f799f5592360a6558eb52d760 + 2c23146a63a29acf99e73b88f8c24eaa7dc60aa771780ccc006afbfa8fe2479b2dd2b21362337441ac12b515911957ff + 283990fa9d5fb731d786c5bbee94ea4db4910f18c62c03d173fc0a5e494422e8a0b3da7574dae7fa0baf005e504063b3 + 9ad8e17325408eddb6edee6147f13856ad819bb7532668b605a24a2d958f88bd5c169e56dc4b2f89ffd325f6006d820b + c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 + 4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15 + 578951e24efd62a3d63a86f7cd19aaa53c898fe287d2552133220370240b572d + f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd + 310aee6b30c47350576ac2873fa89fd190cdc488442f3ef654cf23fe + c59d4eaeac728671c635ff645014e2afa935bebffdb5fbd207ffdeab + +It also supports UTF-8 encoding: + +Code +```JavaScript +sha3_512('中文'); +sha3_384('中文'); +sha3_256('中文'); +sha3_224('中文'); +``` +Output + + 2f6a1bd50562230229af34b0ccf46b8754b89d23ae2c5bf7840b4acfcef86f87395edc0a00b2bfef53bafebe3b79de2e3e01cbd8169ddbb08bde888dcc893524 + 743f64bb7544c6ed923be4741b738dde18b7cee384a3a09c4e01acaaac9f19222cdee137702bd3aa05dc198373d87d6c + 70a2b6579047f0a977fcb5e9120a4e07067bea9abb6916fbc2d13ffb9a4e4eee + f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd + +## Extensions +### jQuery +If you prefer jQuery style, you can add following code to add a jQuery extension. + +Code +```JavaScript +jQuery.sha3_512 = sha3_512; +jQuery.sha3_384 = sha3_384; +jQuery.sha3_256 = sha3_256; +jQuery.sha3_224 = sha3_224; +``` +And then you could use like this: +```JavaScript +$.sha3_512('message'); +$.sha3_384('message'); +$.sha3_256('message'); +$.sha3_224('message'); +``` +### Prototype +If you prefer prototype style, you can add following code to add a prototype extension. + +Code +```JavaScript +String.prototype.sha3_512 = function() { + return sha3_512(this); +}; +String.prototype.sha3_384 = function() { + return sha3_384(this); +}; +String.prototype.sha3_256 = function() { + return sha3_256(this); +}; +String.prototype.sha3_224 = function() { + return sha3_224(this); +}; +``` +And then you could use like this: +```JavaScript +'message'.sha3_512(); +'message'.sha3_384(); +'message'.sha3_256(); +'message'.sha3_224(); +``` +## License +The project is released under the [MIT license](http://www.opensource.org/licenses/MIT). + +## Contact +The project's website is located at https://github.com/emn178/js-sha3 +Author: emn178@gmail.com diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..8471670 --- /dev/null +++ b/bower.json @@ -0,0 +1,9 @@ +{ + "name": "js-sha3", + "version": "0.1.0", + "main": ["build/sha3.min.js"], + "ignore": [ + "samples", + "tests" + ] +} diff --git a/build/sha3.min.js b/build/sha3.min.js new file mode 100644 index 0000000..bc59ea6 --- /dev/null +++ b/build/sha3.min.js @@ -0,0 +1,5 @@ +(function(m,E){var w="undefined"!=typeof module;w&&(m=global);var p="0123456789abcdef".split(""),F=[1,256,65536,16777216],r=[0,8,16,24],G=[0,1,62,28,27,36,44,6,55,20,3,10,43,25,39,41,45,15,21,8,18,2,61,56,14],A=[1,0,32898,0,32906,2147483648,2147516416,2147483648,32907,0,2147483649,0,2147516545,2147483648,32777,2147483648,138,0,136,0,2147516425,0,2147483658,0,2147516555,0,139,2147483648,32905,2147483648,32771,2147483648,32770,2147483648,128,2147483648,32778,0,2147483658,2147483648,2147516545,2147483648, +32896,2147483648,2147483649,0,2147516424,2147483648],c=[],b=[],l=[],q=[],B=function(b){return n(b,224)},C=function(b){return n(b,256)},D=function(b){return n(b,384)},n=function(m,n){n===E&&(n=512);var x,d,u=!1,t=0,w=0,y=m.length,e,g,f,k,a,h,v=(1600-2*n)/32,z=4*v;for(a=0;50>a;++a)b[a]=0;x=0;do{c[0]=x;for(a=1;ad?c[a>>2]|=d<d?c[a>>2]|=(192|d>>6)<d||57344<=d?c[a>>2]|=(224|d>>12)<>2]|=(240|d>>18)<>2]|=(128|d>>12&63)<>2]|=(128|d>>6&63)<>2]|=(128|d&63)<>2]|=F[a&3],++t);x=c[v];t>y&&ad;d++){for(e=0;5>e;e++)f=2*e,q[f]=b[f]^b[f+10]^b[f+20]^b[f+30]^b[f+40],q[f+1]=b[f+1]^b[f+11]^b[f+21]^b[f+31]^b[f+41];for(e=0;5>e;e++)for(f=2*e,a=(e+4)%5*2,h=(e+1)%5*2,k=q[a]^(q[h]<<1|q[h+1]>>>31),h=q[a+1]^(q[h+1]<<1|q[h]>>>31),g=0;5> +g;g++)a=f+10*g,b[a]^=k,b[a+1]^=h;for(e=0;5>e;e++)for(f=2*e,g=0;5>g;g++)a=f+10*g,h=2*g+(f+3*g)%5*10,k=G[e+5*g],0===k?(l[h]=b[a],l[h+1]=b[a+1]):32>k?(l[h]=b[a]<>>32-k,l[h+1]=b[a+1]<>>32-k):(l[h]=b[a+1]<>>64-k,l[h+1]=b[a]<>>64-k);for(e=0;5>e;e++)for(f=2*e,g=0;5>g;g++)a=f+10*g,h=2*((e+1)%5+5*g),k=2*((e+2)%5+5*g),b[a]=l[a]^~l[h]&l[k],b[a+1]=l[a+1]^~l[h+1]&l[k+1];b[0]^=A[2*d];b[1]^=A[2*d+1]}}while(!u);u="";a=0;for(d=n/32;a>4&15]+p[b[a]&15]+p[b[a]>> +12&15]+p[b[a]>>8&15]+p[b[a]>>20&15]+p[b[a]>>16&15]+p[b[a]>>28&15]+p[b[a]>>24&15];return u};!m.JS_SHA3_TEST&&w?module.exports={sha3_512:n,sha3_384:D,sha3_256:C,sha3_224:B}:m&&(m.sha3_512=n,m.sha3_384=D,m.sha3_256=C,m.sha3_224=B)})(this); diff --git a/package.json b/package.json new file mode 100644 index 0000000..b570874 --- /dev/null +++ b/package.json @@ -0,0 +1,32 @@ +{ + "name": "js-sha3", + "version": "0.1.0", + "description": "A simple SHA-3 / Keccak hash function for JavaScript supports UTF-8 encoding.", + "main": "src/sha3.js", + "devDependencies": { + "expect.js": "~0.3.1", + "jscoverage": "~0.5.9" + }, + "scripts": { + "test": "mocha tests/node-test.js -r jscoverage", + "coveralls": "mocha tests/node-test.js -R mocha-lcov-reporter -r jscoverage | coveralls" + }, + "repository": { + "type": "git", + "url": "https://github.com/emn178/js-sha3.git" + }, + "keywords": [ + "sha3", + "keccak", + "hash", + "encryption", + "cryptography", + "HMAC" + ], + "license": "MIT", + "author": "emn178 ", + "homepage": "https://github.com/emn178/js-sha3", + "bugs": { + "url": "https://github.com/emn178/js-sha3/issues" + } +} diff --git a/src/sha3.js b/src/sha3.js new file mode 100644 index 0000000..a144103 --- /dev/null +++ b/src/sha3.js @@ -0,0 +1,172 @@ +/* + * js-sha3 v0.1.0 + * https://github.com/emn178/js-sha3 + * + * Copyright 2015, emn178@gmail.com + * + * Licensed under the MIT license: + * http://www.opensource.org/licenses/MIT + */ +;(function(root, undefined) { + 'use strict'; + + var NODE_JS = typeof(module) != 'undefined'; + if(NODE_JS) { + root = global; + } + var HEX_CHARS = '0123456789abcdef'.split(''); + var EXTRA = [1, 256, 65536, 16777216]; + var SHIFT = [0, 8, 16, 24]; + var R = [0, 1, 62, 28, 27, 36, 44, 6, 55, 20, 3, 10, 43, 25, 39, 41, 45, 15, 21, 8, 18, 2, 61, 56, 14]; + var RC = [1, 0, 32898, 0, 32906, 2147483648, 2147516416, 2147483648, 32907, 0, 2147483649, + 0, 2147516545, 2147483648, 32777, 2147483648, 138, 0, 136, 0, 2147516425, 0, + 2147483658, 0, 2147516555, 0, 139, 2147483648, 32905, 2147483648, 32771, + 2147483648, 32770, 2147483648, 128, 2147483648, 32778, 0, 2147483658, 2147483648, + 2147516545, 2147483648, 32896, 2147483648, 2147483649, 0, 2147516424, 2147483648]; + + var blocks = [], s = [], b = [], c = []; + + var sha3_224 = function(message) { + return sha3(message, 224); + }; + + var sha3_256 = function(message) { + return sha3(message, 256); + }; + + var sha3_384 = function(message) { + return sha3(message, 384); + }; + + var sha3 = function(message, bits) { + if(bits === undefined) { + bits = 512; + } + + var block, code, end = false, index = 0, start = 0, length = message.length, + n, x, y, x2, r, i, j, k, h, l; + var blockCount = (1600 - bits * 2) / 32; + var byteCount = blockCount * 4; + + for(i = 0;i < 50;++i) { + s[i] = 0; + } + + block = 0; + do { + blocks[0] = block; + for(i = 1;i < blockCount + 1;++i) { + blocks[i] = 0; + } + for (i = start;index < length && i < byteCount; ++index) { + code = message.charCodeAt(index); + if (code < 0x80) { + blocks[i >> 2] |= code << SHIFT[i++ & 3]; + } else if (code < 0x800) { + blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } else if (code < 0xd800 || code >= 0xe000) { + blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } else { + code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); + blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } + } + start = i - byteCount; + if(index == length) { + blocks[i >> 2] |= EXTRA[i & 3]; + ++index; + } + block = blocks[blockCount]; + if(index > length && i < byteCount) { + blocks[blockCount - 1] |= 0x80000000; + end = true; + } + + for(i = 0;i < blockCount;++i) { + s[i] ^= blocks[i]; + } + + for(n = 0; n < 24; n++) { + for (x = 0; x < 5; x++) { + x2 = x * 2; + c[x2] = s[x2] ^ s[x2 + 10] ^ s[x2 + 20] ^ s[x2 + 30] ^ s[x2 + 40]; + c[x2 + 1] = s[x2 + 1] ^ s[x2 + 11] ^ s[x2 + 21] ^ s[x2 + 31] ^ s[x2 + 41]; + } + + for (x = 0; x < 5; x++) { + x2 = x * 2; + i = ((x + 4) % 5) * 2; + j = ((x + 1) % 5) * 2; + h = c[i] ^ ((c[j] << 1) | (c[j + 1] >>> 31)); + l = c[i + 1] ^ ((c[j + 1] << 1) | (c[j] >>> 31)); + for (y = 0; y < 5; y++) { + i = x2 + y * 10; + s[i] ^= h; + s[i + 1] ^= l; + } + } + + for (x = 0; x < 5; x++) { + x2 = x * 2; + for (y = 0; y < 5; y++) { + i = x2 + y * 10; + j = y * 2 + ((x2 + 3 * y) % 5) * 10; + r = R[x + y * 5]; + if(r === 0) { + b[j] = s[i]; + b[j + 1] = s[i + 1]; + } else if (r < 32) { + b[j] = (s[i] << r) | (s[i + 1] >>> (32 - r)); + b[j + 1] = (s[i + 1] << r) | (s[i] >>> (32 - r)); + } else { + b[j] = (s[i + 1] << (r - 32)) | (s[i] >>> (64 - r)); + b[j + 1] = (s[i] << (r - 32)) | (s[i + 1] >>> (64 - r)); + } + } + } + + for (x = 0; x < 5; x++) { + x2 = x * 2; + for (y = 0; y < 5; y++) { + i = x2 + y * 10; + j = (((x + 1) % 5) + 5 * y) * 2; + k = (((x + 2) % 5) + 5 * y) * 2; + s[i] = b[i] ^ (~b[j] & b[k]); + s[i + 1] = b[i + 1] ^ (~b[j + 1] & b[k + 1]); + } + } + s[0] ^= RC[n * 2]; + s[1] ^= RC[n * 2 + 1]; + } + } while(!end); + + var hex = ''; + for(i = 0, n = bits / 32;i < n;++i) { + hex += HEX_CHARS[(s[i] >> 4) & 0x0F] + HEX_CHARS[s[i] & 0x0F] + + HEX_CHARS[(s[i] >> 12) & 0x0F] + HEX_CHARS[(s[i] >> 8) & 0x0F] + + HEX_CHARS[(s[i] >> 20) & 0x0F] + HEX_CHARS[(s[i] >> 16) & 0x0F] + + HEX_CHARS[(s[i] >> 28) & 0x0F] + HEX_CHARS[(s[i] >> 24) & 0x0F]; + } + return hex; + }; + + if(!root.JS_SHA3_TEST && NODE_JS) { + module.exports = { + sha3_512: sha3, + sha3_384: sha3_384, + sha3_256: sha3_256, + sha3_224: sha3_224 + }; + } else if(root) { + root.sha3_512 = sha3; + root.sha3_384 = sha3_384; + root.sha3_256 = sha3_256; + root.sha3_224 = sha3_224; + } +}(this)); diff --git a/tests/index.html b/tests/index.html new file mode 100644 index 0000000..6f4462f --- /dev/null +++ b/tests/index.html @@ -0,0 +1,22 @@ + + + + + SHA3 + + + + + + +
+ + + + + diff --git a/tests/node-test.js b/tests/node-test.js new file mode 100644 index 0000000..b70bb78 --- /dev/null +++ b/tests/node-test.js @@ -0,0 +1,17 @@ +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; +expect = require('expect.js'); +require('./test.js'); + +delete require.cache[require.resolve('../src/sha3.js')] +delete require.cache[require.resolve('./test.js')] +sha3_512 = null; +sha3_384 = null; +sha3_256 = null; +sha3_224 = null; + +JS_SHA3_TEST = true; +require('../src/sha3.js'); +require('./test.js'); diff --git a/tests/test.js b/tests/test.js new file mode 100644 index 0000000..78b4576 --- /dev/null +++ b/tests/test.js @@ -0,0 +1,148 @@ +(function(sha3_512, sha3_384, sha3_256, sha3_224) { + describe('sha3_512', function() { + describe('ascii', function() { + describe('less than 128 bytes', function() { + it('should be successful', function() { + expect(sha3_512('')).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e'); + expect(sha3_512('The quick brown fox jumps over the lazy dog')).to.be('d135bb84d0439dbac432247ee573a23ea7d3c9deb2a968eb31d47c4fb45f1ef4422d6c531b5b9bd6f449ebcc449ea94d0a8f05f62130fda612da53c79659f609'); + expect(sha3_512('The quick brown fox jumps over the lazy dog.')).to.be('ab7192d2b11f51c7dd744e7b3441febf397ca07bf812cceae122ca4ded6387889064f8db9230f173f6d1ab6e24b6e50f065b039f799f5592360a6558eb52d760'); + }); + }); + + describe('more than 128 bytes', function() { + it('should be successful', function() { + expect(sha3_512('The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.')).to.be('10dcbf6389980ce3594547939bbc685363d28adbd6a05bc4abd7fc62e7693a1f6e33569fed5a380bfecb56ae811d25939b95823f39bb0f16a08740629d066d43'); + }); + }); + }); + + describe('UTF8', function() { + describe('less than 128 bytes', function() { + it('should be successful', function() { + expect(sha3_512('中文')).to.be('2f6a1bd50562230229af34b0ccf46b8754b89d23ae2c5bf7840b4acfcef86f87395edc0a00b2bfef53bafebe3b79de2e3e01cbd8169ddbb08bde888dcc893524'); + expect(sha3_512('aécio')).to.be('c452ec93e83d4795fcab62a76eed0d88f2231a995ce108ac8f130246f87c4a11cb18a2c1a688a5695906a6f863e71bbe8997c6610319ab97f12d2e5bf0afe458'); + expect(sha3_512('𠜎')).to.be('8a2d72022ce19d989dbe6a0017faccbf5dc2e22c162d1c5eb168864d32dd1a71e1b4782652c148cf6ca47b77a72c96fff682e72bdfef0566d4b7cca3c9ccc59d'); + }); + }); + + describe('more than 128 bytes', function() { + it('should be successful', function() { + expect(sha3_512('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('6a67c28aa1946ca1be8382b861aac4aaf20052f495db9b6902d13adfa603eaba5d169f8896b86d461b2949283eb98e503c3f0640188ea7d6731526fc06568d37'); + expect(sha3_512('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('d04ff5b0e85e9968be2a4d4e133c15c7ccee7497198bb651599a97d11d00bca6048d329ab75aa454566cd532648fa1cb4551985d9d645de9fa43a311a9ee8e4d'); + }); + }); + }); + }); + + describe('sha3_384', function() { + describe('ascii', function() { + describe('less than 128 bytes', function() { + it('should be successful', function() { + expect(sha3_384('')).to.be('2c23146a63a29acf99e73b88f8c24eaa7dc60aa771780ccc006afbfa8fe2479b2dd2b21362337441ac12b515911957ff'); + expect(sha3_384('The quick brown fox jumps over the lazy dog')).to.be('283990fa9d5fb731d786c5bbee94ea4db4910f18c62c03d173fc0a5e494422e8a0b3da7574dae7fa0baf005e504063b3'); + expect(sha3_384('The quick brown fox jumps over the lazy dog.')).to.be('9ad8e17325408eddb6edee6147f13856ad819bb7532668b605a24a2d958f88bd5c169e56dc4b2f89ffd325f6006d820b'); + }); + }); + + describe('more than 128 bytes', function() { + it('should be successful', function() { + expect(sha3_384('The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.')).to.be('e7ec8976b4d96e43f50ae8ecdcf2d97a56236e6406e8dd00efd0d9abe885659db58a2f4b138a4ecfb1bd0052f6569516'); + }); + }); + }); + + describe('UTF8', function() { + describe('less than 128 bytes', function() { + it('should be successful', function() { + expect(sha3_384('中文')).to.be('743f64bb7544c6ed923be4741b738dde18b7cee384a3a09c4e01acaaac9f19222cdee137702bd3aa05dc198373d87d6c'); + expect(sha3_384('aécio')).to.be('08990555e131af8597687614309da4c5053ce866f348544da0a0c2c78c2cc79680ebb57cfbe238286e78ea133a037897'); + expect(sha3_384('𠜎')).to.be('2a80f59abf3111f38a35a3daa25123b495f90e9736bd300e35911d19abdd8806498c581333f198ccbbf2252b57c2925d'); + }); + }); + + describe('more than 128 bytes', function() { + it('should be successful', function() { + expect(sha3_384('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('a3b043a2f69e4326a05d478fa4c8aa2bd7612453d775af37665a0b96ef2207cdc74c50cdba1629796a5136fe77300b05'); + expect(sha3_384('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('66414c090cc3fe9c396d313cbaa100aefd335e851838b29382568b7f57357ada7c54b8fa8c17f859945bba88b2c2e332'); + }); + }); + }); + }); + + describe('sha3_256', function() { + describe('ascii', function() { + describe('less than 128 bytes', function() { + it('should be successful', function() { + expect(sha3_256('')).to.be('c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'); + expect(sha3_256('The quick brown fox jumps over the lazy dog')).to.be('4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15'); + expect(sha3_256('The quick brown fox jumps over the lazy dog.')).to.be('578951e24efd62a3d63a86f7cd19aaa53c898fe287d2552133220370240b572d'); + }); + }); + + describe('more than 128 bytes', function() { + it('should be successful', function() { + expect(sha3_256('The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.')).to.be('af20018353ffb50d507f1555580f5272eca7fdab4f8295db4b1a9ad832c93f6d'); + }); + }); + }); + + describe('UTF8', function() { + describe('less than 128 bytes', function() { + it('should be successful', function() { + expect(sha3_256('中文')).to.be('70a2b6579047f0a977fcb5e9120a4e07067bea9abb6916fbc2d13ffb9a4e4eee'); + expect(sha3_256('aécio')).to.be('d7d569202f04daf90432810d6163112b2695d7820da979327ebd894efb0276dc'); + expect(sha3_256('𠜎')).to.be('16a7cc7a58444cbf7e939611910ddc82e7cba65a99d3e8e08cfcda53180a2180'); + }); + }); + + describe('more than 128 bytes', function() { + it('should be successful', function() { + expect(sha3_256('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('d1021d2d4c5c7e88098c40f422af68493b4b64c913cbd68220bf5e6127c37a88'); + expect(sha3_256('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('ffabf9bba2127c4928d360c9905cb4911f0ec21b9c3b89f3b242bccc68389e36'); + }); + }); + }); + }); + + describe('sha3_224', function() { + describe('ascii', function() { + describe('less than 128 bytes', function() { + it('should be successful', function() { + expect(sha3_224('')).to.be('f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd'); + expect(sha3_224('The quick brown fox jumps over the lazy dog')).to.be('310aee6b30c47350576ac2873fa89fd190cdc488442f3ef654cf23fe'); + expect(sha3_224('The quick brown fox jumps over the lazy dog.')).to.be('c59d4eaeac728671c635ff645014e2afa935bebffdb5fbd207ffdeab'); + }); + }); + + describe('more than 128 bytes', function() { + it('should be successful', function() { + expect(sha3_224('The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.')).to.be('8dd58b706e3a08ec4f1f202af39295b38c355a39b23308ade7218a21'); + }); + }); + }); + + describe('UTF8', function() { + describe('less than 128 bytes', function() { + it('should be successful', function() { + expect(sha3_224('中文')).to.be('7bc2a0b6e7e0a055a61e4f731e2944b560f41ff98967dcbf4bbf77a5'); + expect(sha3_224('aécio')).to.be('66f3db76bf8cb35726cb278bac412d187c3484ab2083dc50ef5ffb55'); + expect(sha3_224('𠜎')).to.be('3bfa94845726f4cd5cf17d19b5eacac17b3694790e13a76d5c81c7c2'); + }); + }); + + describe('more than 128 bytes', function() { + it('should be successful', function() { + expect(sha3_224('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('d59eef8f394ef7d96967bb0bde578785c033f7f0a21913d6ba41ed1b'); + expect(sha3_224('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('27123a2a3860d1041d4769778c4b078732bf4300f7e1c56536ab2644'); + }); + }); + }); + }); + + describe('special length', function() { + it('should be successful', function() { + expect(sha3_512('012345678901234567890123456789012345678901234567890123456789012345678901')).to.be('90b1d032c3bf06dcc78a46fe52054bab1250600224bfc6dfbfb40a7877c55e89bb982799a2edf198568a4166f6736678b45e76b12fac813cfdf0a76714e5eae8'); + expect(sha3_512('01234567890123456789012345678901234567890123456789012345678901234567890')).to.be('3173e7abc754a0b2909410d78986428a9183e996864af02f421d273d9fa1b4e4a5b14e2998b20767712f53a01ff8f6ae2c3e71e51e2c0f24257b03e6da09eb77'); + }); + }); +})(sha3_512, sha3_384, sha3_256, sha3_224);