diff --git a/.covignore b/.covignore new file mode 100644 index 0000000..016f806 --- /dev/null +++ b/.covignore @@ -0,0 +1 @@ +/tests/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f5cd45c --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +language: node_js +node_js: + - "0.11" + - "0.10" + - "0.8" +install: + - npm install mocha -g + - npm install coveralls -g + - npm install mocha-lcov-reporter -g + - npm install +script: + - npm run-script coveralls diff --git a/CHANGELOG.md b/CHANGELOG.md index 678a8fe..d7c0c5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# v0.1.3 / 2015-01-06 + +* Add bower package. +* Fixed JSHint warnings. +* Add travis. +* Add coveralls. + # v0.1.1 / 2014-07-27 Fixed accents bug diff --git a/LICENSE.txt b/LICENSE.txt index dff2ebd..58074d3 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,4 +1,4 @@ -Copyright 2014 emn178@gmail.com +Copyright 2014-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 diff --git a/README.md b/README.md index 3bd9344..27f5f8e 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,32 @@ # js-sha256 +[![Build Status](https://api.travis-ci.org/emn178/js-sha256.png)](https://travis-ci.org/emn178/js-sha256) +[![Build Status](https://coveralls.io/repos/emn178/js-sha256/badge.png?branch=master)](https://coveralls.io/r/emn178/js-sha256?branch=master) +[![NPM](https://nodei.co/npm/js-sha256.png?stars&downloads)](https://nodei.co/npm/js-sha256/) A simple SHA-256 / SHA-224 hash function for JavaScript supports UTF-8 encoding. -## Install +## Demo +[SHA256 Online](http://emn178.github.io/online-tools/sha256.html) +[SHA224 Online](http://emn178.github.io/online-tools/sha224.html) + +## Download +[Compress](https://raw.github.com/emn178/js-sha256/master/build/sha256.min.js) +[Uncompress](https://raw.github.com/emn178/js-sha256/master/src/sha256.js) + +## Installation +You can also install js-sha256 by using Bower. + + bower install js-sha256 + For node.js, you can use this command to install: npm install js-sha256 ## Usage +You could use like this: +```JavaScript +sha256('Message to hash'); +sha224('Message to hash'); +``` If you use node.js, you should require the module first: ```JavaScript sha256 = require('js-sha256'); @@ -16,20 +36,42 @@ or sha256 = require('js-sha256').sha256; sha224 = require('js-sha256').sha224; ``` -And you could use like this: -```JavaScript -sha256('Message to hash'); -sha224('Message to hash'); -``` + +### Methods + +#### sha256(str, asciiOnly) + +Hash string to sha256, set asciiOnly to true for better performace if you ensure input is ascii. + +##### *str: `String`* + +String to hash. + +##### *asciiOnly: `Boolean` (default: `false`)* + +Specify the string encoding is ASCII. + +#### sha225(str, asciiOnly) + +Hash string to sha225, set asciiOnly to true for better performace if you ensure input is ascii. + +##### *str: `String`* + +String to hash. + +##### *asciiOnly: `Boolean` (default: `false`)* + +Specify the string encoding is ASCII. + ## Example Code ```JavaScript sha256(''); -sha256('The quick brown fox jumps over the lazy dog'); -sha256('The quick brown fox jumps over the lazy dog.'); +sha256('The quick brown fox jumps over the lazy dog', true); +sha256('The quick brown fox jumps over the lazy dog.', true); sha224(''); -sha224('The quick brown fox jumps over the lazy dog'); -sha224('The quick brown fox jumps over the lazy dog.'); +sha224('The quick brown fox jumps over the lazy dog', true); +sha224('The quick brown fox jumps over the lazy dog.', true); ``` Output @@ -52,15 +94,6 @@ Output 72726d8818f693066ceb69afa364218b692e62ea92b385782363780f47529c21 dfbab71afdf54388af4d55f8bd3de8c9b15e0eb916bf9125f4a959d4 -## Tests -You can open `tests/index.html` in browser or use node.js to run test - - node tests/node-test.js - -or - - npm test - ## Extensions ### jQuery If you prefer jQuery style, you can add following code to add a jQuery extension. diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..9c534b8 --- /dev/null +++ b/bower.json @@ -0,0 +1,9 @@ +{ + "name": "js-sha256", + "version": "0.1.2", + "main": ["build/sha256.min.js"], + "ignore": [ + "samples", + "tests" + ] +} diff --git a/build/sha256.min.js b/build/sha256.min.js index 0eac253..c8e07c9 100644 --- a/build/sha256.min.js +++ b/build/sha256.min.js @@ -1,5 +1,5 @@ -(function(t,I){var u="0123456789abcdef".split(""),F={0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,a:10,b:11,c:12,d:13,e:14,f:15,A:10,B:11,C:12,D:13,E:14,F:15},J=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895, -666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298],f=function(g){return G(g,!0)},H=function(g){return G(g,!1)},G=function(g,z){z===I&&(z=!0);var a;a:{for(a=g.length;a--;)if(127>2]=37==m?a[e>>2]|(F[b.charAt(++c)]<<4|F[b.charAt(++c)])<<(3-e%4<<3):a[e>>2]|m<<(3-e%4<<3);++e}b=(e+8>>6)+1<<4;c=e>>2;a[c]|=128<<(3-e%4<<3);for(c+=1;c>6)+1<<4;c=[];for(b=0;b>2]|=g.charCodeAt(b)<<(3-b%4<<3);c[b>>2]|=128<<(3-b%4<<3);c[e-1]=a<<3;a=c}e=a;if(z){var c=1779033703,b=3144134277,r=1013904242,m=2773480762,f= -1359893119,v=2600822924,w=528734635;a=1541459225}else c=3238371032,b=914150663,r=812702999,m=4144912697,f=4290775857,v=1750603025,w=1694076839,a=3204075428;for(var A=0,t=e.length;Ad;++d)h[d]=e[A+d];for(d=16;64>d;++d)s=k(h[d-15],7)^k(h[d-15],18)^h[d-15]>>>3,l=k(h[d-2],17)^k(h[d-2],19)^h[d-2]>>>10,h[d]=h[d-16]+s+h[d-7]+l;for(var n=c,x=b,y=r,D=m,p=f,B=v,C=w,E=a,u,d=0;64>d;++d)s=k(n,2)^k(n,13)^k(n,22),l=n&x^n&y^x&y,s+=l,l=k(p,6)^k(p,11)^k(p,25),u=p&B^~p&C,l=E+l+u+J[d]+ -h[d]&4294967295,E=C,C=B,B=p,p=D+l,D=y,y=x,x=n,n=l+s;c+=n;b+=x;r+=y;m+=D;f+=p;v+=B;w+=C;a+=E}e=q(c)+q(b)+q(r)+q(m)+q(f)+q(v)+q(w);z&&(e+=q(a));return e},k=function(g,f){return g>>>f|g<<32-f},q=function(g){for(var f="",a=0;4>a;a++)var b=3-a<<3,f=f+(u[g>>b+4&15]+u[g>>b&15]);return f};"undefined"!=typeof module?(f.sha256=f,f.sha224=H,module.exports=f):t&&(t.sha256=f,t.sha224=H)})(this); +(function(x,H){var y="0123456789abcdef".split(""),I=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411, +3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298],k=function(b,g){return F(b,!0,g)},G=function(b,g){return F(b,!1,g)},F=function(b,g,c){g===H&&(g=!0);var e,d,a,r,t,u,v;if(!c&&/[^\x00-\x7F]/.test(b)){c=[];for(d=e=0;da?c[e++]=a:(2048>a?c[e++]=192|a>>6:(55296>a||57344<=a?c[e++]=224| +a>>12:(a=65536+((a&1023)<<10|b.charCodeAt(++d)&1023),c[e++]=240|a>>18,c[e++]=128|a>>12&63),c[e++]=128|a>>6&63),c[e++]=128|a&63);b=c.length;e=(b+8>>6)+1<<4;d=[];for(a=0;a>2]|=c[a]<<(3-(a&3)<<3);d[a>>2]|=128<<(3-(a&3)<<3);d[e-1]=b<<3}else{c=b.length;e=(c+8>>6)+1<<4;d=[];for(a=0;a>2]|=b.charCodeAt(a)<<(3-(a&3)<<3);d[a>>2]|=128<<(3-(a&3)<<3);d[e-1]=c<<3}c=d;g?(e=1779033703,d=3144134277,a=1013904242,r=2773480762,t=1359893119,u=2600822924, +v=528734635,b=1541459225):(e=3238371032,d=914150663,a=812702999,r=4144912697,t=4290775857,u=1750603025,v=1694076839,b=3204075428);for(var k=0,x=c.length;kf;++f)h[f]=c[k+f];for(f=16;64>f;++f)w=l(h[f-15],7)^l(h[f-15],18)^h[f-15]>>>3,m=l(h[f-2],17)^l(h[f-2],19)^h[f-2]>>>10,h[f]=h[f-16]+w+h[f-7]+m;var n=e,z=d,A=a,D=r,p=t,B=u,C=v,E=b,y;for(f=0;64>f;++f)w=l(n,2)^l(n,13)^l(n,22),m=n&z^n&A^z&A,w+=m,m=l(p,6)^l(p,11)^l(p,25),y=p&B^~p&C,m=E+m+y+I[f]+h[f]&4294967295,E=C,C= +B,B=p,p=D+m,D=A,A=z,z=n,n=m+w;e+=n;d+=z;a+=A;r+=D;t+=p;u+=B;v+=C;b+=E}c=q(e)+q(d)+q(a)+q(r)+q(t)+q(u)+q(v);g&&(c+=q(b));return c},l=function(b,g){return b>>>g|b<<32-g},q=function(b){for(var g="",c=0;4>c;c++)var e=3-c<<3,g=g+(y[b>>e+4&15]+y[b>>e&15]);return g};"undefined"!=typeof module?(k.sha256=k,k.sha224=G,module.exports=k):x&&(x.sha256=k,x.sha224=G)})(this); diff --git a/package.json b/package.json index 50a66b4..938b1e2 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,15 @@ { "name": "js-sha256", - "version": "0.1.1", + "version": "0.1.2", "description": "A simple SHA-256 / SHA-224 hash function for JavaScript supports UTF-8 encoding.", "main": "src/sha256.js", + "devDependencies": { + "expect.js": "~0.3.1", + "jscoverage": "~0.5.9" + }, "scripts": { - "test": "node tests/node-test.js" + "test": "mocha tests/node-test.js -r jscoverage", + "coveralls": "mocha tests/node-test.js -R mocha-lcov-reporter -r jscoverage | coveralls" }, "repository": { "type": "git", diff --git a/src/sha256.js b/src/sha256.js index 0a047e2..5047018 100644 --- a/src/sha256.js +++ b/src/sha256.js @@ -1,22 +1,16 @@ /* - * js-sha256 v0.1.1 + * js-sha256 v0.1.2 * https://github.com/emn178/js-sha256 * - * Copyright 2014, emn178@gmail.com + * Copyright 2014-2015, emn178@gmail.com * * Licensed under the MIT license: * http://www.opensource.org/licenses/MIT */ - -(function(root, undefined){ +;(function(root, undefined) { 'use strict'; - var HEX_CHARS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; - var HEX_TABLE = { - '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, - 'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15, - 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15 - }; + var HEX_CHARS = '0123456789abcdef'.split(''); var K =[0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, @@ -27,49 +21,51 @@ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]; - var sha256 = function(message) { - return sha2(message, true); + var sha256 = function(message, asciiOnly) { + return sha2(message, true, asciiOnly); }; - var sha224 = function(message) { - return sha2(message, false); + var sha224 = function(message, asciiOnly) { + return sha2(message, false, asciiOnly); }; - var sha2 = function(message, is256) { - if(is256 === undefined) + var sha2 = function(message, is256, asciiOnly) { + if(is256 === undefined) { is256 = true; + } - var blocks = hasUTF8(message) ? UTF8toBlocks(message) : ASCIItoBlocks(message); - if(is256) - { - var h0 = 0x6a09e667; - var h1 = 0xbb67ae85; - var h2 = 0x3c6ef372; - var h3 = 0xa54ff53a; - var h4 = 0x510e527f; - var h5 = 0x9b05688c; - var h6 = 0x1f83d9ab; - var h7 = 0x5be0cd19; + var blocks, h0, h1, h2, h3, h4, h5, h6, h7; + if(!asciiOnly && /[^\x00-\x7F]/.test(message)) { + blocks = getBlocksFromUtf8(message); + } else { + blocks = getBlocksFromAscii(message); } - else // 224 - { - var h0 = 0xc1059ed8; - var h1 = 0x367cd507; - var h2 = 0x3070dd17; - var h3 = 0xf70e5939; - var h4 = 0xffc00b31; - var h5 = 0x68581511; - var h6 = 0x64f98fa7; - var h7 = 0xbefa4fa4; + if(is256) { + h0 = 0x6a09e667; + h1 = 0xbb67ae85; + h2 = 0x3c6ef372; + h3 = 0xa54ff53a; + h4 = 0x510e527f; + h5 = 0x9b05688c; + h6 = 0x1f83d9ab; + h7 = 0x5be0cd19; + } else { // 224 + h0 = 0xc1059ed8; + h1 = 0x367cd507; + h2 = 0x3070dd17; + h3 = 0xf70e5939; + h4 = 0xffc00b31; + h5 = 0x68581511; + h6 = 0x64f98fa7; + h7 = 0xbefa4fa4; } - for(var i = 0, length = blocks.length;i < length;i += 16) - { - var w = [], s0, s1; - for(var j = 0;j < 16;++j) + for(var i = 0, length = blocks.length;i < length;i += 16) { + var w = [], s0, s1, j; + for(j = 0;j < 16;++j) { w[j] = blocks[i + j]; - for(var j = 16;j < 64;++j) - { + } + for(j = 16;j < 64;++j) { s0 = rightrotate(w[j - 15], 7) ^ rightrotate(w[j - 15], 18) ^ (w[j - 15] >>> 3); s1 = rightrotate(w[j - 2], 17) ^ rightrotate(w[j - 2], 19) ^ (w[j - 2] >>> 10); w[j] = w[j - 16] + s0 + w[j - 7] + s1; @@ -85,8 +81,7 @@ var h = h7; var maj, t1, t2, ch; - for(var j = 0;j < 64;++j) - { + for(j = 0;j < 64;++j) { s0 = rightrotate(a, 2) ^ rightrotate(a, 13) ^ rightrotate(a, 22); maj = (a & b) ^ (a & c) ^ (b & c); t2 = s0 + maj; @@ -115,8 +110,9 @@ } var hex = toHexString(h0) + toHexString(h1)+ toHexString(h2) + toHexString(h3) + toHexString(h4) + toHexString(h5) + toHexString(h6); - if(is256) + if(is256) { hex += toHexString(h7); + } return hex; }; @@ -125,69 +121,77 @@ }; var toHexString = function(num) { - var hex = ""; - for(var i = 0; i < 4; i++) - { + var hex = ''; + for(var i = 0; i < 4; i++) { var offset = 3 - i << 3; hex += HEX_CHARS[(num >> (offset + 4)) & 0x0F] + HEX_CHARS[(num >> offset) & 0x0F]; } return hex; }; - var hasUTF8 = function(message) { - var i = message.length; - while(i--) - if(message.charCodeAt(i) > 127) - return true; - return false; + var getBytesFromUtf8 = function(str) { + var bytes = [], index = 0; + for (var i = 0;i < str.length; i++) { + var c = str.charCodeAt(i); + if (c < 0x80) { + bytes[index++] = c; + } else if (c < 0x800) { + bytes[index++] = 0xc0 | (c >> 6); + bytes[index++] = 0x80 | (c & 0x3f); + } else if (c < 0xd800 || c >= 0xe000) { + bytes[index++] = 0xe0 | (c >> 12); + bytes[index++] = 0x80 | ((c >> 6) & 0x3f); + bytes[index++] = 0x80 | (c & 0x3f); + } else { + c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff)); + bytes[index++] = 0xf0 | (c >> 18); + bytes[index++] = 0x80 | ((c >> 12) & 0x3f); + bytes[index++] = 0x80 | ((c >> 6) & 0x3f); + bytes[index++] = 0x80 | (c & 0x3f); + } + } + return bytes; }; - var ASCIItoBlocks = function(message) { + var getBlocksFromAscii = function(message) { // a block is 32 bits(4 bytes), a chunk is 512 bits(64 bytes) var length = message.length; var chunkCount = ((length + 8) >> 6) + 1; var blockCount = chunkCount << 4; // chunkCount * 16 - var blocks = []; - var i; - for(i = 0;i < blockCount;++i) + var blocks = [], i; + for(i = 0;i < blockCount;++i) { blocks[i] = 0; - for(i = 0;i < length;++i) - blocks[i >> 2] |= message.charCodeAt(i) << (3 - (i % 4) << 3); - blocks[i >> 2] |= 0x80 << (3 - (i % 4) << 3); + } + for(i = 0;i < length;++i) { + blocks[i >> 2] |= message.charCodeAt(i) << (3 - (i & 3) << 3); + } + blocks[i >> 2] |= 0x80 << (3 - (i & 3) << 3); blocks[blockCount - 1] = length << 3; // length * 8 return blocks; }; - - var UTF8toBlocks = function(message) { - var uri = encodeURIComponent(message); - var blocks = []; - for(var i = 0, bytes = 0, length = uri.length;i < length;++i) - { - var c = uri.charCodeAt(i); - if(c == 37) // % - blocks[bytes >> 2] |= ((HEX_TABLE[uri.charAt(++i)] << 4) | HEX_TABLE[uri.charAt(++i)]) << (3 - (bytes % 4) << 3); - else - blocks[bytes >> 2] |= c << (3 - (bytes % 4) << 3); - ++bytes; - } - var chunkCount = ((bytes + 8) >> 6) + 1; + + var getBlocksFromUtf8 = function(message) { + var bytes = getBytesFromUtf8(message); + var length = bytes.length; + var chunkCount = ((length + 8) >> 6) + 1; var blockCount = chunkCount << 4; // chunkCount * 16 - var index = bytes >> 2; - blocks[index] |= 0x80 << (3 - (bytes % 4) << 3); - for(var i = index + 1;i < blockCount;++i) + var blocks = [], i; + for(i = 0;i < blockCount;++i) { blocks[i] = 0; - blocks[blockCount - 1] = bytes << 3; // bytes * 8 + } + for(i = 0;i < length;++i) { + blocks[i >> 2] |= bytes[i] << (3 - (i & 3) << 3); + } + blocks[i >> 2] |= 0x80 << (3 - (i & 3) << 3); + blocks[blockCount - 1] = length << 3; // length * 8 return blocks; }; - if(typeof(module) != 'undefined') - { + if(typeof(module) != 'undefined') { sha256.sha256 = sha256; sha256.sha224 = sha224; module.exports = sha256; - } - else if(root) - { + } else if(root) { root.sha256 = sha256; root.sha224 = sha224; } diff --git a/tests/debug.js b/tests/debug.js deleted file mode 100644 index 23900bf..0000000 --- a/tests/debug.js +++ /dev/null @@ -1,12 +0,0 @@ -(function(root) { - var assert = function (title, expect, actual) { - if(expect == actual) - console.log(title + ': true'); - else - console.log(title + ': false', 'Except:' + expect, 'Actual: ' + actual); - }; - if(typeof(module) != 'undefined') - global.assert = assert; - else if(root) - root.assert = assert; -})(this); diff --git a/tests/index.html b/tests/index.html index 43797c4..e3592ba 100644 --- a/tests/index.html +++ b/tests/index.html @@ -3,10 +3,20 @@ SHA256 + + + - - +
+ + + diff --git a/tests/node-test.js b/tests/node-test.js index 133ff5a..997701a 100644 --- a/tests/node-test.js +++ b/tests/node-test.js @@ -1,7 +1,4 @@ -// this also works: -// sha256 = require('../src/sha256.js'); - sha256 = require('../src/sha256.js').sha256; sha224 = require('../src/sha256.js').sha224; -require('./debug.js'); +expect = require('expect.js'); require('./test.js'); diff --git a/tests/test.js b/tests/test.js index a48c06e..5bf854b 100644 --- a/tests/test.js +++ b/tests/test.js @@ -1,10 +1,65 @@ -assert('sha256 1', 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', sha256('')); -assert('sha256 2', 'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592', sha256('The quick brown fox jumps over the lazy dog')); -assert('sha256 3', 'ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c', sha256('The quick brown fox jumps over the lazy dog.')); -assert('sha256 4', '72726d8818f693066ceb69afa364218b692e62ea92b385782363780f47529c21', sha256('中文')); -assert('sha256 5', '53196d1acfce0c4b264e01e8018c989d571351f59e33f055f76ff15b4f0516c6', sha256('aécio')); -assert('sha224 1', 'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f', sha224('')); -assert('sha224 2', '730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525', sha224('The quick brown fox jumps over the lazy dog')); -assert('sha224 3', '619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4c', sha224('The quick brown fox jumps over the lazy dog.')); -assert('sha224 4', 'dfbab71afdf54388af4d55f8bd3de8c9b15e0eb916bf9125f4a959d4', sha224('中文')); -assert('sha224 5', 'd12841cafd89c534924a839e62bf35a2b5f3717b7802eb19bd8d8e15', sha224('aécio')); +describe('sha256', function() { + describe('ascii', function() { + describe('less than 64 bytes', function() { + it('should be successful', function() { + expect(sha256('')).to.be('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'); + expect(sha256('The quick brown fox jumps over the lazy dog')).to.be('d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592'); + expect(sha256('The quick brown fox jumps over the lazy dog.')).to.be('ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c'); + }); + }); + + describe('more than 64 bytes', function() { + it('should be successful', function() { + expect(sha256('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('54e73d89e1924fdcd056390266a983924b6d6d461e9470b6cd50bbaf69b5c54c'); + }); + }); + }); + + describe('UTF8', function() { + describe('less than 64 bytes', function() { + it('should be successful', function() { + expect(sha256('中文')).to.be('72726d8818f693066ceb69afa364218b692e62ea92b385782363780f47529c21'); + expect(sha256('aécio')).to.be('53196d1acfce0c4b264e01e8018c989d571351f59e33f055f76ff15b4f0516c6'); + }); + }); + + describe('more than 64 bytes', function() { + it('should be successful', function() { + expect(sha256('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('81a1472ebdeb09406a783d607ff49ee2fde3e9f44ac1cd158ad8d6ad3c4e69fa'); + }); + }); + }); +}); + +describe('sha224', function() { + describe('ascii', function() { + describe('less than 64 bytes', function() { + it('should be successful', function() { + expect(sha224('')).to.be('d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f'); + expect(sha224('The quick brown fox jumps over the lazy dog')).to.be('730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525'); + expect(sha224('The quick brown fox jumps over the lazy dog.')).to.be('619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4c'); + }); + }); + + describe('more than 64 bytes', function() { + it('should be successful', function() { + expect(sha224('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('4d97e15967391d2e846ea7d21bb480efadbae5868b731e7cc6267006'); + }); + }); + }); + + describe('UTF8', function() { + describe('less than 64 bytes', function() { + it('should be successful', function() { + expect(sha224('中文')).to.be('dfbab71afdf54388af4d55f8bd3de8c9b15e0eb916bf9125f4a959d4'); + expect(sha224('aécio')).to.be('d12841cafd89c534924a839e62bf35a2b5f3717b7802eb19bd8d8e15'); + }); + }); + + describe('more than 64 bytes', function() { + it('should be successful', function() { + expect(sha224('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('a8cb74a54e6dc6ab6110db3915ba08ffe5e1abafaea78538fa12a626'); + }); + }); + }); +});