* Add bower package.

* Fixed JSHint warnings.
* Add travis.
* Add coveralls.
pull/1/head
Chen Yi-Cyuan 11 years ago
parent 0aa248c0b5
commit 41737fe5ee

@ -0,0 +1 @@
/tests/

@ -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

@ -1,3 +1,10 @@
# v0.1.2 / 2015-01-07
* Add bower package.
* Fixed JSHint warnings.
* Add travis.
* Add coveralls.
# v0.1.1 / 2014-07-27 # v0.1.1 / 2014-07-27
Fixed accents bug Fixed accents bug

@ -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 Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the a copy of this software and associated documentation files (the

@ -1,12 +1,37 @@
# js-sha512 # js-sha512
[![Build Status](https://api.travis-ci.org/emn178/js-sha512.png)](https://travis-ci.org/emn178/js-sha512)
[![Build Status](https://coveralls.io/repos/emn178/js-sha512/badge.png?branch=master)](https://coveralls.io/r/emn178/js-sha512?branch=master)
[![NPM](https://nodei.co/npm/js-sha512.png?stars&downloads)](https://nodei.co/npm/js-sha512/)
A simple SHA-512, SHA-384, SHA-512/224, SHA-512/256 hash functions for JavaScript supports UTF-8 encoding. A simple SHA-512, SHA-384, SHA-512/224, SHA-512/256 hash functions for JavaScript supports UTF-8 encoding.
## Install ## Demo
[SHA512 Online](http://emn178.github.io/online-tools/sha512.html)
[SHA384 Online](http://emn178.github.io/online-tools/sha384.html)
[SHA512/256 Online](http://emn178.github.io/online-tools/sha512_256.html)
[SHA512/224 Online](http://emn178.github.io/online-tools/sha512_224.html)
## Download
[Compress](https://raw.github.com/emn178/js-sha512/master/build/sha512.min.js)
[Uncompress](https://raw.github.com/emn178/js-sha512/master/src/sha512.js)
## Installation
You can also install js-sha512 by using Bower.
bower install js-sha512
For node.js, you can use this command to install: For node.js, you can use this command to install:
npm install js-sha512 npm install js-sha512
## Usage ## Usage
```
You could use like this:
```JavaScript
sha512('Message to hash');
sha384('Message to hash');
sha512_256('Message to hash');
sha512_224('Message to hash');
```
If you use node.js, you should require the module first: If you use node.js, you should require the module first:
```JavaScript ```JavaScript
sha512 = require('js-sha512'); sha512 = require('js-sha512');
@ -17,14 +42,57 @@ sha512 = require('js-sha512').sha512;
sha384 = require('js-sha512').sha384; sha384 = require('js-sha512').sha384;
sha512_256 = require('js-sha512').sha512_256; sha512_256 = require('js-sha512').sha512_256;
sha512_224 = require('js-sha512').sha512_224; sha512_224 = require('js-sha512').sha512_224;
```
And you could use like this: ### Methods
```JavaScript
sha512('Message to hash'); #### sha512(str, asciiOnly)
sha384('Message to hash');
sha512_256('Message to hash'); Hash string to sha512, set asciiOnly to true for better performace if you ensure input is ascii.
sha512_224('Message to hash');
``` ##### *str: `String`*
String to hash.
##### *asciiOnly: `Boolean` (default: `false`)*
Specify the string encoding is ASCII.
#### sha384(str, asciiOnly)
Hash string to sha384, 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.
#### sha512_256(str, asciiOnly)
Hash string to sha512/256, 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.
#### sha512_224(str, asciiOnly)
Hash string to sha512/224, 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 ## Example
Code Code
```JavaScript ```JavaScript
@ -72,15 +140,6 @@ Output
b6dab29c16ec35ab34a5d92ff135b58de96741dda78b1009a2181cf8b45d2f72 b6dab29c16ec35ab34a5d92ff135b58de96741dda78b1009a2181cf8b45d2f72
0f46a0ae7f226517dd66ece0ce1efa29ffb7ced05ac4566fdcaed188 0f46a0ae7f226517dd66ece0ce1efa29ffb7ced05ac4566fdcaed188
## 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 ## Extensions
### jQuery ### jQuery
If you prefer jQuery style, you can add following code to add a jQuery extension. If you prefer jQuery style, you can add following code to add a jQuery extension.

@ -0,0 +1,9 @@
{
"name": "js-sha512",
"version": "0.1.2",
"main": ["build/sha256.min.js"],
"ignore": [
"samples",
"tests"
]
}

@ -1,10 +1,15 @@
{ {
"name": "js-sha512", "name": "js-sha512",
"version": "0.1.1", "version": "0.1.2",
"description": "This is a simple SHA-512, SHA-384, SHA-512/224, SHA-512/256 hash functions for JavaScript supports UTF-8 encoding.", "description": "This is a simple SHA-512, SHA-384, SHA-512/224, SHA-512/256 hash functions for JavaScript supports UTF-8 encoding.",
"main": "src/sha512.js", "main": "src/sha512.js",
"devDependencies": {
"expect.js": "~0.3.1",
"jscoverage": "~0.5.9"
},
"scripts": { "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": { "repository": {
"type": "git", "type": "git",

@ -1,29 +1,85 @@
/* /*
* js-sha512 v0.1.1 * js-sha512 v0.1.2
* https://github.com/emn178/js-sha512 * https://github.com/emn178/js-sha512
* *
* Copyright 2014, emn178@gmail.com * Copyright 2014-2015, emn178@gmail.com
* *
* Licensed under the MIT license: * Licensed under the MIT license:
* http://www.opensource.org/licenses/MIT * http://www.opensource.org/licenses/MIT
*/ */
;(function(root, undefined) {
(function(root, undefined){
'use strict'; 'use strict';
// Class Long // Class Long
var Long = function(high, low){ var Long = function(high, low) {
this.high = high | 0; this.high = high << 0;
this.low = low | 0; this.low = low << 0;
};
Long.prototype.and = function(other) {
return new Long(this.high & other.high, this.low & other.low);
};
Long.prototype.xor = function(other) {
return new Long(this.high ^ other.high, this.low ^ other.low);
};
Long.prototype.not = function() {
return new Long(~this.high, ~this.low);
};
Long.prototype.shiftRightUnsigned = function(numBits) {
numBits &= 63;
if(numBits === 0) {
return new Long(this.high, this.low);
}
if(numBits < 32) {
return new Long(this.high >>> numBits, (this.low >>> numBits) | (this.high << (32 - numBits)));
} else if(numBits == 32) {
return new Long(0, this.high);
} else {
return new Long(0, this.high >>> (numBits - 32));
}
}; };
var HEX_CHARS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; Long.prototype.rightRotate = function(numBits) {
var HEX_TABLE = { numBits &= 63;
'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, if(numBits === 0) {
'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15, return new Long(this.high, this.low);
'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15 }
if(numBits < 32) {
return new Long((this.high >>> numBits) | (this.low << (32 - numBits)), (this.low >>> numBits) | (this.high << (32 - numBits)));
} else if(numBits == 32) {
return new Long(this.low, this.high);
} else {
return new Long((this.low >>> (numBits - 32)) | (this.high << (64 - numBits)), (this.high >>> (numBits - 32)) | (this.low << (64 - numBits)));
}
}; };
Long.prototype.add = function(other) {
var a1 = this.low & 0xFFFF;
var a2 = this.low >>> 16;
var a3 = this.high & 0xFFFF;
var a4 = this.high >>> 16;
var b1 = other.low & 0xFFFF;
var b2 = other.low >>> 16;
var b3 = other.high & 0xFFFF;
var b4 = other.high >>> 16;
var c1 = a1 + b1;
var c2 = a2 + b2 + (c1 >>> 16);
var c3 = a3 + b3 + (c2 >>> 16);
var c4 = a4 + b4 + (c3 >>> 16);
return new Long((c4 << 16) | (c3 & 0xFFFF), (c2 << 16) | (c1 & 0xFFFF));
};
Long.prototype.toHexString = function() {
return toHexString(this.high) + toHexString(this.low);
};
var HEX_CHARS = '0123456789abcdef'.split('');
var K =[new Long(0x428A2F98, 0xD728AE22), new Long(0x71374491, 0x23EF65CD), var K =[new Long(0x428A2F98, 0xD728AE22), new Long(0x71374491, 0x23EF65CD),
new Long(0xB5C0FBCF, 0xEC4D3B2F), new Long(0xE9B5DBA5, 0x8189DBBC), new Long(0xB5C0FBCF, 0xEC4D3B2F), new Long(0xE9B5DBA5, 0x8189DBBC),
new Long(0x3956C25B, 0xF348B538), new Long(0x59F111F1, 0xB605D019), new Long(0x3956C25B, 0xF348B538), new Long(0x59F111F1, 0xB605D019),
@ -65,77 +121,74 @@
new Long(0x4CC5D4BE, 0xCB3E42B6), new Long(0x597F299C, 0xFC657E2A), new Long(0x4CC5D4BE, 0xCB3E42B6), new Long(0x597F299C, 0xFC657E2A),
new Long(0x5FCB6FAB, 0x3AD6FAEC), new Long(0x6C44198C, 0x4A475817)]; new Long(0x5FCB6FAB, 0x3AD6FAEC), new Long(0x6C44198C, 0x4A475817)];
var sha512 = function(message) { var sha512 = function(message, asciiOnly) {
return sha2(message, 512); return sha2(message, 512, asciiOnly);
}; };
var sha384 = function(message) { var sha384 = function(message, asciiOnly) {
return sha2(message, 384); return sha2(message, 384, asciiOnly);
}; };
var sha512_256 = function(message) { var sha512_256 = function(message, asciiOnly) {
return sha2(message, 256); return sha2(message, 256, asciiOnly);
}; };
var sha512_224 = function(message) { var sha512_224 = function(message, asciiOnly) {
return sha2(message, 224); return sha2(message, 224, asciiOnly);
}; };
var sha2 = function(message, tbit) { var sha2 = function(message, tbit, asciiOnly) {
var blocks = hasUTF8(message) ? UTF8toBlocks(message) : ASCIItoBlocks(message); var blocks, h0, h1, h2, h3, h4, h5, h6, h7;
if(!asciiOnly && /[^\x00-\x7F]/.test(message)) {
if(tbit == 512) blocks = getBlocksFromUtf8(message);
{ } else {
var h0 = new Long(0x6A09E667, 0xF3BCC908); blocks = getBlocksFromAscii(message);
var h1 = new Long(0xBB67AE85, 0x84CAA73B);
var h2 = new Long(0x3C6EF372, 0xFE94F82B);
var h3 = new Long(0xA54FF53A, 0x5F1D36F1);
var h4 = new Long(0x510E527F, 0xADE682D1);
var h5 = new Long(0x9B05688C, 0x2B3E6C1F);
var h6 = new Long(0x1F83D9AB, 0xFB41BD6B);
var h7 = new Long(0x5BE0CD19, 0x137E2179);
}
else if(tbit == 384)
{
var h0 = new Long(0xCBBB9D5D, 0xC1059ED8);
var h1 = new Long(0x629A292A, 0x367CD507);
var h2 = new Long(0x9159015A, 0x3070DD17);
var h3 = new Long(0x152FECD8, 0xF70E5939);
var h4 = new Long(0x67332667, 0xFFC00B31);
var h5 = new Long(0x8EB44A87, 0x68581511);
var h6 = new Long(0xDB0C2E0D, 0x64F98FA7);
var h7 = new Long(0x47B5481D, 0xBEFA4FA4);
} }
else if(tbit == 256)
{ if(tbit == 512) {
var h0 = new Long(0x22312194, 0xFC2BF72C); h0 = new Long(0x6A09E667, 0xF3BCC908);
var h1 = new Long(0x9F555FA3, 0xC84C64C2); h1 = new Long(0xBB67AE85, 0x84CAA73B);
var h2 = new Long(0x2393B86B, 0x6F53B151); h2 = new Long(0x3C6EF372, 0xFE94F82B);
var h3 = new Long(0x96387719, 0x5940EABD); h3 = new Long(0xA54FF53A, 0x5F1D36F1);
var h4 = new Long(0x96283EE2, 0xA88EFFE3); h4 = new Long(0x510E527F, 0xADE682D1);
var h5 = new Long(0xBE5E1E25, 0x53863992); h5 = new Long(0x9B05688C, 0x2B3E6C1F);
var h6 = new Long(0x2B0199FC, 0x2C85B8AA); h6 = new Long(0x1F83D9AB, 0xFB41BD6B);
var h7 = new Long(0x0EB72DDC, 0x81C52CA2); h7 = new Long(0x5BE0CD19, 0x137E2179);
} } else if(tbit == 384) {
else if(tbit == 224) h0 = new Long(0xCBBB9D5D, 0xC1059ED8);
{ h1 = new Long(0x629A292A, 0x367CD507);
var h0 = new Long(0x8C3D37C8, 0x19544DA2); h2 = new Long(0x9159015A, 0x3070DD17);
var h1 = new Long(0x73E19966, 0x89DCD4D6); h3 = new Long(0x152FECD8, 0xF70E5939);
var h2 = new Long(0x1DFAB7AE, 0x32FF9C82); h4 = new Long(0x67332667, 0xFFC00B31);
var h3 = new Long(0x679DD514, 0x582F9FCF); h5 = new Long(0x8EB44A87, 0x68581511);
var h4 = new Long(0x0F6D2B69, 0x7BD44DA8); h6 = new Long(0xDB0C2E0D, 0x64F98FA7);
var h5 = new Long(0x77E36F73, 0x04C48942); h7 = new Long(0x47B5481D, 0xBEFA4FA4);
var h6 = new Long(0x3F9D85A8, 0x6A1D36C8); } else if(tbit == 256) {
var h7 = new Long(0x1112E6AD, 0x91D692A1); h0 = new Long(0x22312194, 0xFC2BF72C);
h1 = new Long(0x9F555FA3, 0xC84C64C2);
h2 = new Long(0x2393B86B, 0x6F53B151);
h3 = new Long(0x96387719, 0x5940EABD);
h4 = new Long(0x96283EE2, 0xA88EFFE3);
h5 = new Long(0xBE5E1E25, 0x53863992);
h6 = new Long(0x2B0199FC, 0x2C85B8AA);
h7 = new Long(0x0EB72DDC, 0x81C52CA2);
} else if(tbit == 224) {
h0 = new Long(0x8C3D37C8, 0x19544DA2);
h1 = new Long(0x73E19966, 0x89DCD4D6);
h2 = new Long(0x1DFAB7AE, 0x32FF9C82);
h3 = new Long(0x679DD514, 0x582F9FCF);
h4 = new Long(0x0F6D2B69, 0x7BD44DA8);
h5 = new Long(0x77E36F73, 0x04C48942);
h6 = new Long(0x3F9D85A8, 0x6A1D36C8);
h7 = new Long(0x1112E6AD, 0x91D692A1);
} }
for(var i = 0, length = blocks.length;i < length;i += 16) for(var i = 0, length = blocks.length;i < length;i += 16) {
{ var w = [], s0, s1, j;
var w = [], s0, s1; for(j = 0;j < 16;++j) {
for(var j = 0;j < 16;++j)
w[j] = blocks[i + j]; w[j] = blocks[i + j];
for(var j = 16;j < 80;++j) }
{ for(j = 16;j < 80;++j) {
s0 = w[j - 15].rightRotate(1).xor(w[j - 15].rightRotate(8)).xor(w[j - 15].shiftRightUnsigned(7)); s0 = w[j - 15].rightRotate(1).xor(w[j - 15].rightRotate(8)).xor(w[j - 15].shiftRightUnsigned(7));
s1 = w[j - 2].rightRotate(19).xor(w[j - 2].rightRotate(61)).xor(w[j - 2].shiftRightUnsigned(6)); s1 = w[j - 2].rightRotate(19).xor(w[j - 2].rightRotate(61)).xor(w[j - 2].shiftRightUnsigned(6));
w[j] = w[j - 16].add(s0).add(w[j - 7]).add(s1); w[j] = w[j - 16].add(s0).add(w[j - 7]).add(s1);
@ -151,8 +204,7 @@
var h = h7; var h = h7;
var maj, t1, t2, ch; var maj, t1, t2, ch;
for(var j = 0;j < 80;++j) for(j = 0;j < 80;++j) {
{
s0 = a.rightRotate(28).xor(a.rightRotate(34)).xor(a.rightRotate(39)); s0 = a.rightRotate(28).xor(a.rightRotate(34)).xor(a.rightRotate(39));
maj = a.and(b).xor(a.and(c)).xor(b.and(c)); maj = a.and(b).xor(a.and(c)).xor(b.and(c));
t2 = s0.add(maj); t2 = s0.add(maj);
@ -181,145 +233,100 @@
} }
var hex = h0.toHexString() + h1.toHexString() + h2.toHexString() + h3.toHexString(); var hex = h0.toHexString() + h1.toHexString() + h2.toHexString() + h3.toHexString();
if(tbit == 224) if(tbit == 224) {
return hex.substr(0, hex.length - 8); return hex.substr(0, hex.length - 8);
if(tbit >= 384) }
if(tbit >= 384) {
hex += h4.toHexString() + h5.toHexString(); hex += h4.toHexString() + h5.toHexString();
if(tbit == 512) }
if(tbit == 512) {
hex += h6.toHexString() + h7.toHexString(); hex += h6.toHexString() + h7.toHexString();
}
return hex; return hex;
}; };
var hasUTF8 = function(message) { var getBytesFromUtf8 = function(str) {
var i = message.length; var bytes = [], index = 0;
while(i--) for (var i = 0;i < str.length; i++) {
if(message.charCodeAt(i) > 127) var c = str.charCodeAt(i);
return true; if (c < 0x80) {
return false; 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 1024 bits(128 bytes) // a block is 32 bits(4 bytes), a chunk is 1024 bits(128 bytes)
var length = message.length; var length = message.length;
var chunkCount = ((length + 16) >> 7) + 1; var chunkCount = ((length + 16) >> 7) + 1;
var blockCount = chunkCount << 5; // chunkCount * 32 var blockCount = chunkCount << 5; // chunkCount * 32
var blocks = []; var blocks = [], i;
var i; for(i = 0;i < blockCount;++i) {
for(i = 0;i < blockCount;++i)
blocks[i] = 0; blocks[i] = 0;
for(i = 0;i < length;++i) }
blocks[i >> 2] |= message.charCodeAt(i) << (3 - (i % 4) << 3); for(i = 0;i < length;++i) {
blocks[i >> 2] |= 0x80 << (3 - (i % 4) << 3); 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 blocks[blockCount - 1] = length << 3; // length * 8
var blocks64 = []; var blocks64 = [];
for(i = 0;i < blockCount;i += 2) for(i = 0;i < blockCount;i += 2) {
blocks64[i >> 1] = new Long(blocks[i], blocks[i + 1]); blocks64[i >> 1] = new Long(blocks[i], blocks[i + 1]);
}
return blocks64; return blocks64;
}; };
var UTF8toBlocks = function(message) { var getBlocksFromUtf8 = function(message) {
var uri = encodeURIComponent(message); var bytes = getBytesFromUtf8(message);
var blocks = []; var length = bytes.length;
for(var i = 0, bytes = 0, length = uri.length;i < length;++i) var chunkCount = ((length + 16) >> 7) + 1;
{
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 + 16) >> 7) + 1;
var blockCount = chunkCount << 5; // chunkCount * 32 var blockCount = chunkCount << 5; // chunkCount * 32
var index = bytes >> 2; var blocks = [], i;
blocks[index] |= 0x80 << (3 - (bytes % 4) << 3); for(i = 0;i < blockCount;++i) {
for(var i = index + 1;i < blockCount;++i)
blocks[i] = 0; 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
var blocks64 = []; var blocks64 = [];
for(i = 0;i < blockCount;i += 2) for(i = 0;i < blockCount;i += 2) {
blocks64[i >> 1] = new Long(blocks[i], blocks[i + 1]); blocks64[i >> 1] = new Long(blocks[i], blocks[i + 1]);
}
return blocks64; return blocks64;
}; };
var toHexString = function(num) { var toHexString = function(num) {
var hex = ""; var hex = '';
for(var i = 0; i < 4; i++) for(var i = 0; i < 4; i++) {
{
var offset = 3 - i << 3; var offset = 3 - i << 3;
hex += HEX_CHARS[(num >> (offset + 4)) & 0x0F] + HEX_CHARS[(num >> offset) & 0x0F]; hex += HEX_CHARS[(num >> (offset + 4)) & 0x0F] + HEX_CHARS[(num >> offset) & 0x0F];
} }
return hex; return hex;
}; };
Long.prototype.and = function(other){ if(typeof(module) != 'undefined') {
return new Long(this.high & other.high, this.low & other.low);
};
Long.prototype.xor = function(other){
return new Long(this.high ^ other.high, this.low ^ other.low);
};
Long.prototype.not = function(){
return new Long(~this.high, ~this.low);
};
Long.prototype.shiftRightUnsigned = function(numBits){
numBits &= 63;
if(numBits == 0)
return new Long(this.high, this.low);
if(numBits < 32)
return new Long(this.high >>> numBits, (this.low >>> numBits) | (this.high << (32 - numBits)));
else if(numBits == 32)
return new Long(0, this.high);
else
return new Long(0, this.high >>> (numBits - 32));
};
Long.prototype.rightRotate = function(numBits){
numBits &= 63;
if(numBits == 0)
return new Long(this.high, this.low);
if(numBits < 32)
return new Long((this.high >>> numBits) | (this.low << (32 - numBits)), (this.low >>> numBits) | (this.high << (32 - numBits)));
else if(numBits == 32)
return new Long(this.low, this.high);
else
return new Long((this.low >>> (numBits - 32)) | (this.high << (64 - numBits)), (this.high >>> (numBits - 32)) | (this.low << (64 - numBits)));
};
Long.prototype.add = function(other){
var a1 = this.low & 0xFFFF;
var a2 = this.low >>> 16;
var a3 = this.high & 0xFFFF;
var a4 = this.high >>> 16;
var b1 = other.low & 0xFFFF;
var b2 = other.low >>> 16;
var b3 = other.high & 0xFFFF;
var b4 = other.high >>> 16;
var c1 = a1 + b1;
var c2 = a2 + b2 + (c1 >>> 16);
var c3 = a3 + b3 + (c2 >>> 16);
var c4 = a4 + b4 + (c3 >>> 16);
return new Long((c4 << 16) | (c3 & 0xFFFF), (c2 << 16) | (c1 & 0xFFFF));
};
Long.prototype.toHexString = function() {
return toHexString(this.high) + toHexString(this.low);
};
if(typeof(module) != 'undefined')
{
sha512.sha512 = sha512; sha512.sha512 = sha512;
sha512.sha384 = sha384; sha512.sha384 = sha384;
sha512.sha512_256 = sha512_256; sha512.sha512_256 = sha512_256;
sha512.sha512_224 = sha512_224; sha512.sha512_224 = sha512_224;
module.exports = sha512; module.exports = sha512;
} } else if(root) {
else if(root)
{
root.sha512 = sha512; root.sha512 = sha512;
root.sha384 = sha384; root.sha384 = sha384;
root.sha512_256 = sha512_256; root.sha512_256 = sha512_256;

@ -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);

@ -3,10 +3,20 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>SHA512</title> <title>SHA512</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.1.0/mocha.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.1.0/mocha.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect.js/0.2.0/expect.min.js"></script>
<script src="../src/sha512.js"></script> <script src="../src/sha512.js"></script>
<script src="debug.js"></script>
<script src="test.js"></script>
</head> </head>
<body> <body>
<div id="mocha"></div>
<script>
mocha.setup('bdd');
</script>
<script src="test.js"></script>
<script>
mocha.checkLeaks();
mocha.run();
</script>
</body> </body>
</html> </html>

@ -1,9 +1,6 @@
// this also works:
// sha512 = require('../src/sha512.js');
sha512 = require('../src/sha512.js').sha512; sha512 = require('../src/sha512.js').sha512;
sha384 = require('../src/sha512.js').sha384; sha384 = require('../src/sha512.js').sha384;
sha512_256 = require('../src/sha512.js').sha512_256; sha512_256 = require('../src/sha512.js').sha512_256;
sha512_224 = require('../src/sha512.js').sha512_224; sha512_224 = require('../src/sha512.js').sha512_224;
require('./debug.js'); expect = require('expect.js');
require('./test.js'); require('./test.js');

@ -1,20 +1,131 @@
assert('sha512 1', 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e', sha512('')); describe('sha512', function() {
assert('sha512 2', '07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6', sha512('The quick brown fox jumps over the lazy dog')); describe('ascii', function() {
assert('sha512 3', '91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed', sha512('The quick brown fox jumps over the lazy dog.')); describe('less than 64 bytes', function() {
assert('sha512 4', '8b88efc2ebbcbdad5ac2d65af05bec57bda25e71fd5fb25bbd892057a2755fbd05d8d8491cb2946febd5b0f124ffdfbaecf7e34946353c4f1b5ab29545895468', sha512('中文')); it('should be successful', function() {
assert('sha512 5', 'e1c6925243db76985abacaf9fa85e22697f549e67f65a36c88e4046a2260990ff9eefc3402396ea8dcbe8c592d8d5671bea612156eda38d3708d394bbd17d493', sha512('aécio')); expect(sha512('')).to.be('cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e');
assert('sha384 1', '38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b', sha384('')); expect(sha512('The quick brown fox jumps over the lazy dog')).to.be('07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6');
assert('sha384 2', 'ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1', sha384('The quick brown fox jumps over the lazy dog')); expect(sha512('The quick brown fox jumps over the lazy dog.', true)).to.be('91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed');
assert('sha384 3', 'ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7', sha384('The quick brown fox jumps over the lazy dog.')); });
assert('sha384 4', '93422ceb8291a69b22f02dc1114c39a287493ad525dcebc77e4019a44eaee2633a85d0f29cd298ee6799048c33a4be0c', sha384('中文')); });
assert('sha384 5', 'a2146805faafc45b0055b49386768811c803ef9fa8a85b648e114276c1bf49ef1092ec1bc2d3f7e036238a97eace2087', sha384('aécio'));
assert('sha512/256 1', 'c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a', sha512_256('')); describe('more than 64 bytes', function() {
assert('sha512/256 2', 'dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d', sha512_256('The quick brown fox jumps over the lazy dog')); it('should be successful', function() {
assert('sha512/256 3', '1546741840f8a492b959d9b8b2344b9b0eb51b004bba35c0aebaac86d45264c3', sha512_256('The quick brown fox jumps over the lazy dog.')); expect(sha512('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('a8dedff31e3be9df6413ef5b4ecb93d62d3fbcb04297552eab5370e04afd45927854a4373037e81a50186e678d818c9ba824f4c850f3d0f02764af0252076979');
assert('sha512/256 4', 'b6dab29c16ec35ab34a5d92ff135b58de96741dda78b1009a2181cf8b45d2f72', sha512_256('中文')); });
assert('sha512/256 5', '122802ca08e39c2ef46f6a81379dc5683bd8aa074dfb54259f0add4d8b5504bc', sha512_256('aécio')); });
assert('sha512/224 1', '6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4', sha512_224('')); });
assert('sha512/224 2', '944cd2847fb54558d4775db0485a50003111c8e5daa63fe722c6aa37', sha512_224('The quick brown fox jumps over the lazy dog'));
assert('sha512/224 3', '6d6a9279495ec4061769752e7ff9c68b6b0b3c5a281b7917ce0572de', sha512_224('The quick brown fox jumps over the lazy dog.')); describe('UTF8', function() {
assert('sha512/224 4', '0f46a0ae7f226517dd66ece0ce1efa29ffb7ced05ac4566fdcaed188', sha512_224('中文')); describe('less than 64 bytes', function() {
assert('sha512/224 5', '562f2e4ee7f7451d20dcc6a0ac1a1e1c4a75f09baaf1cf19af3e15f4', sha512_224('aécio')); it('should be successful', function() {
expect(sha512('中文')).to.be('8b88efc2ebbcbdad5ac2d65af05bec57bda25e71fd5fb25bbd892057a2755fbd05d8d8491cb2946febd5b0f124ffdfbaecf7e34946353c4f1b5ab29545895468');
expect(sha512('aécio')).to.be('e1c6925243db76985abacaf9fa85e22697f549e67f65a36c88e4046a2260990ff9eefc3402396ea8dcbe8c592d8d5671bea612156eda38d3708d394bbd17d493');
});
});
describe('more than 64 bytes', function() {
it('should be successful', function() {
expect(sha512('訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一又譯雜湊演算法、摘要演算法等主流程式語言普遍已有MD5的實作。')).to.be('d24af1901aaf1458f089a6eddf784ce61c3012aee0df98bdb67ad2dc6b41a3b4051d40caac524373930ae396a2dde99a9204871b40892eea3e5f3c8d46da0c3c');
});
});
});
});
describe('sha384', function() {
describe('ascii', function() {
describe('less than 64 bytes', function() {
it('should be successful', function() {
expect(sha384('')).to.be('38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b');
expect(sha384('The quick brown fox jumps over the lazy dog')).to.be('ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1');
expect(sha384('The quick brown fox jumps over the lazy dog.', true)).to.be('ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7');
});
});
describe('more than 64 bytes', function() {
it('should be successful', function() {
expect(sha384('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('3ab853dee5785c19346e8578d9cdb3b7258a8212c15fdf25d3055b4fbbc892a529e552788992fdae68dfa34f13f0af4e');
});
});
});
describe('UTF8', function() {
describe('less than 64 bytes', function() {
it('should be successful', function() {
expect(sha384('中文')).to.be('93422ceb8291a69b22f02dc1114c39a287493ad525dcebc77e4019a44eaee2633a85d0f29cd298ee6799048c33a4be0c');
expect(sha384('aécio')).to.be('a2146805faafc45b0055b49386768811c803ef9fa8a85b648e114276c1bf49ef1092ec1bc2d3f7e036238a97eace2087');
});
});
describe('more than 64 bytes', function() {
it('should be successful', function() {
expect(sha384('訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一又譯雜湊演算法、摘要演算法等主流程式語言普遍已有MD5的實作。')).to.be('432a8d25efb1a8d463acc7bbbed57365cddc72de3cc19d42e1d260b1479770ecea9dd403fead2949d4fabb36939f263a');
});
});
});
});
describe('sha512/256', function() {
describe('ascii', function() {
describe('less than 64 bytes', function() {
it('should be successful', function() {
expect(sha512_256('')).to.be('c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a');
expect(sha512_256('The quick brown fox jumps over the lazy dog')).to.be('dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d');
expect(sha512_256('The quick brown fox jumps over the lazy dog.', true)).to.be('1546741840f8a492b959d9b8b2344b9b0eb51b004bba35c0aebaac86d45264c3');
});
});
describe('more than 64 bytes', function() {
it('should be successful', function() {
expect(sha512_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('21e2e940930b23f1de6377086d07e22033c6bbf3fd9fbf4b62ec66e6c08c25be');
});
});
});
describe('UTF8', function() {
describe('less than 64 bytes', function() {
it('should be successful', function() {
expect(sha512_256('中文')).to.be('b6dab29c16ec35ab34a5d92ff135b58de96741dda78b1009a2181cf8b45d2f72');
expect(sha512_256('aécio')).to.be('122802ca08e39c2ef46f6a81379dc5683bd8aa074dfb54259f0add4d8b5504bc');
});
});
describe('more than 64 bytes', function() {
it('should be successful', function() {
expect(sha512_256('訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一又譯雜湊演算法、摘要演算法等主流程式語言普遍已有MD5的實作。')).to.be('bd1abad59e6b8ad69bc17b6e05aa13f0cb725467fbeb45b83d3e4094332d1367');
});
});
});
});
describe('sha512/224', function() {
describe('ascii', function() {
describe('less than 64 bytes', function() {
it('should be successful', function() {
expect(sha512_224('')).to.be('6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4');
expect(sha512_224('The quick brown fox jumps over the lazy dog')).to.be('944cd2847fb54558d4775db0485a50003111c8e5daa63fe722c6aa37');
expect(sha512_224('The quick brown fox jumps over the lazy dog.', true)).to.be('6d6a9279495ec4061769752e7ff9c68b6b0b3c5a281b7917ce0572de');
});
});
describe('more than 64 bytes', function() {
it('should be successful', function() {
expect(sha512_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('2e962464977b198ee758d615bbc92251ad2e3c0960068e279fd21d2f');
});
});
});
describe('UTF8', function() {
describe('less than 64 bytes', function() {
it('should be successful', function() {
expect(sha512_224('中文')).to.be('0f46a0ae7f226517dd66ece0ce1efa29ffb7ced05ac4566fdcaed188');
expect(sha512_224('aécio')).to.be('562f2e4ee7f7451d20dcc6a0ac1a1e1c4a75f09baaf1cf19af3e15f4');
});
});
describe('more than 64 bytes', function() {
it('should be successful', function() {
expect(sha512_224('訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一又譯雜湊演算法、摘要演算法等主流程式語言普遍已有MD5的實作。')).to.be('009c3d1e3172d6df71344982eada855421592aea28acbf660ada7569');
});
});
});
});

Loading…
Cancel
Save