Compare commits

...

8 Commits

Author SHA1 Message Date
Yi-Cyuan Chen 6def8ef942 ### Fixed
- dependencies and security issues.
- don't modify global Array and ArrayBuffer prototypes.
- refactor: simplify formatMessage internal logic.
- Generates incorrect hash in some cases.

### Changed
- use unsigned right shift.
2 years ago
Yi-Cyuan Chen b6c443143f Added
- TypeScript definitions.
- clone method.

Changed
- throw error if update after finalize
7 years ago
Yi-Cyuan Chen 241405d4fd Fixed incorrect result when first bit is 1 of bytes. 8 years ago
Yi-Cyuan Chen 6aed3e39c4 upgrade packages 8 years ago
Yi-Cyuan Chen 8a281c3bcb Added support HMAC. #8 8 years ago
Yi-Cyuan Chen 094ce76c5a Fixed incorrect result when file size >= 512M. 8 years ago
Yi-Cyuan Chen 05de50a022 * Added support for web worker. #5
* Changed throw error if input type is incorrect.
8 years ago
Chen Yi-Cyuan 310922df3b Added
- Streaming support(update).
- support for ArrayBuffer input/output.
- support for byte array input/output.
8 years ago

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

3
.gitignore vendored

@ -1,2 +1,3 @@
/node_modules/
/covreporter/
/coverage/
/.nyc_output/

@ -0,0 +1,7 @@
/node_modules/
/coverage/
/.nyc_output/
/tests/
.travis.yml
.npmignore
bower.json

@ -1,12 +1,10 @@
language: node_js
node_js:
- "0.12.15"
- "4.5"
- "6.5.0"
- "6.11.4"
- "8.6.0"
before_install:
- npm install coveralls
- npm install mocha-lcov-reporter
script: npm run-script coveralls
after_success: npm run coveralls
branches:
only:
- master

@ -1,8 +1,52 @@
# Change Log
## v0.9.0 / 2024-01-24
### Fixed
- dependencies and security issues.
- don't modify global Array and ArrayBuffer prototypes.
- refactor: simplify formatMessage internal logic.
- Generates incorrect hash in some cases.
### Changed
- use unsigned right shift.
## v0.8.0 / 2018-08-05
### Added
- TypeScript definitions.
- clone method.
### Changed
- throw error if update after finalize
## v0.7.1 / 2017-12-21
### Fixed
- incorrect result when first bit is 1 of bytes.
## v0.7.0 / 2017-11-19
### Added
- support HMAC. #8
## v0.6.0 / 2017-10-31
### Fixed
- incorrect result when file size >= 512M.
## v0.5.0 / 2017-10-07
### Added
- support for web worker. #5
### Changed
- throw error if input type is incorrect.
## v0.4.0 / 2017-07-08
### Added
- Streaming support(update).
- support for ArrayBuffer input/output.
- support for byte array input/output.
## v0.3.0 / 2017-01-23
### Added
- AMD support.
### Fixed
- ArrayBuffer dosen't work in Webpack.
@ -13,20 +57,24 @@
## v0.2.1 / 2015-02-08
### Added
- test cases.
### Removed
- ascii parameter.
### Improved
- performance.
## v0.2.0 / 2015-02-02
### Added
- test cases.
### Improved
- performance.
## v0.1.3 / 2015-01-26
### Added
- test cases.
### Improved
- performance.
@ -35,6 +83,7 @@
- bower package.
- travis.
- coveralls.
### Fixed
- JSHint warnings.

@ -1,4 +1,4 @@
Copyright 2014-2017 Chen, Yi-Cyuan
Copyright 2014-2024 Chen, Yi-Cyuan
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

@ -5,6 +5,9 @@
[![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.
## Notice
* v0.8.0+ will throw an error if try to update hash after finalize.
## Demo
[SHA512 Online](http://emn178.github.io/online-tools/sha512.html)
[SHA384 Online](http://emn178.github.io/online-tools/sha384.html)
@ -25,32 +28,79 @@ For node.js, you can use this command to install:
npm install js-sha512
## 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');
```
### Node.js
If you use node.js, you should require the module first:
```JavaScript
var sha512 = require('js-sha512');
const { sha512, sha384, sha512_256, sha512_224 } = require('js-sha512');
```
or
```JavaScript
var sha512 = require('js-sha512').sha512;
var sha384 = require('js-sha512').sha384;
var sha512_256 = require('js-sha512').sha512_256;
var sha512_224 = require('js-sha512').sha512_224;
### TypeScript
If you use TypeScript, you can import like this:
```TypeScript
import { sha512, sha384, sha512_256, sha512_224 } from 'js-sha512';
```
### RequireJS
It supports AMD:
```JavaScript
require(['your/path/sha512.js'], function(sha512) {
// ...
require(['your/path/sha512.js'], function(jsSha512) {
const { sha512, sha384, sha512_256, sha512_224 } = jsSha512;
});
```
### Classic Browser
If you use browser script directly, functions will be global:
```JavaScript
sha512('Message to hash');
sha384('Message to hash');
sha512_256('Message to hash');
sha512_224('Message to hash');
```
## Example
You could use like this:
```JavaScript
sha512('Message to hash');
sha384('Message to hash');
sha512_256('Message to hash');
sha512_224('Message to hash');
// Support ArrayBuffer output
var arrayBuffer = sha512.arrayBuffer('Message to hash');
// Support Array output
var bytes = sha512.digest('Message to hash');
var bytes = sha512.array('Message to hash');
// update hash
var hash = sha512.update('Message to hash');
hash.update('Message2 to hash');
hash.hex();
// or to use create
var hash = sha512.create();
hash.update('Message to hash');
hash.hex();
// clone
var hash = sha512.update('Message to hash');
var hash2 = hash.clone();
hash2.hex();
hash.update('Message2 to hash');
hash.hex();
// HMAC
sha512.hmac('key', 'Message to hash');
sha384.hmac('key', 'Message to hash');
var hash = sha512.hmac.create('key');
hash.update('Message to hash');
hash.hex();
var hash2 = sha512.hmac.update('key', 'Message to hash');
hash2.update('Message2 to hash');
hash2.array();
sha512(''); // cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
sha512('The quick brown fox jumps over the lazy dog'); // 07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6
sha512('The quick brown fox jumps over the lazy dog.'); // 91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed
@ -69,6 +119,17 @@ sha512('中文'); // 8b88efc2ebbcbdad5ac2d65af05bec57bda25e71fd5fb25bbd892057a27
sha384('中文'); // 93422ceb8291a69b22f02dc1114c39a287493ad525dcebc77e4019a44eaee2633a85d0f29cd298ee6799048c33a4be0c
sha512_256('中文'); // b6dab29c16ec35ab34a5d92ff135b58de96741dda78b1009a2181cf8b45d2f72
sha512_224('中文'); // 0f46a0ae7f226517dd66ece0ce1efa29ffb7ced05ac4566fdcaed188
// Different output
sha512(''); // cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
sha512.hex(''); // cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
sha512.array(''); // [207, 131, 225, 53, 126, 239, 184, 189, 241, 84, 40, 80, 214, 109, 128, 7, 214, 32, 228, 5, 11, 87, 21, 220, 131, 244, 169, 33, 211, 108, 233, 206, 71, 208, 209, 60, 93, 133, 242, 176, 255, 131, 24, 210, 135, 126, 236, 47, 99, 185, 49, 189, 71, 65, 122, 129, 165, 56, 50, 122, 249, 39, 218, 62]
sha512.digest(''); // [207, 131, 225, 53, 126, 239, 184, 189, 241, 84, 40, 80, 214, 109, 128, 7, 214, 32, 228, 5, 11, 87, 21, 220, 131, 244, 169, 33, 211, 108, 233, 206, 71, 208, 209, 60, 93, 133, 242, 176, 255, 131, 24, 210, 135, 126, 236, 47, 99, 185, 49, 189, 71, 65, 122, 129, 165, 56, 50, 122, 249, 39, 218, 62]
sha512.arrayBuffer(''); // ArrayBuffer
// HMAC
sha512.hmac.hex('key', 'Message to hash');
sha512.hmac.array('key', 'Message to hash');
```
## License

@ -1,6 +1,6 @@
{
"name": "js-sha512",
"version": "0.3.0",
"version": "0.9.0",
"main": ["src/sha512.js"],
"ignore": [
"samples",

File diff suppressed because one or more lines are too long

151
index.d.ts vendored

@ -0,0 +1,151 @@
type Message = string | number[] | ArrayBuffer | Uint8Array;
interface Hasher {
/**
* Update hash
*
* @param message The message you want to hash.
*/
update(message: Message): Hasher;
/**
* Return hash in hex string.
*/
hex(): string;
/**
* Return hash in hex string.
*/
toString(): string;
/**
* Return hash in ArrayBuffer.
*/
arrayBuffer(): ArrayBuffer;
/**
* Return hash in integer array.
*/
digest(): number[];
/**
* Return hash in integer array.
*/
array(): number[];
}
interface Hmac {
/**
* Computes a Hash-based message authentication code (HMAC) using a secret key
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
(secretKey: string, message: Message): string;
/**
* Create a hash object using a secret key.
*
* @param secretKey The Secret Key
*/
create(secretKey: string): Hasher;
/**
* Create a hash object and hash message using a secret key
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
update(secretKey: string, message: Message): Hasher;
/**
* Return hash in hex string.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
hex(secretKey: string, message: Message): string;
/**
* Return hash in ArrayBuffer.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
arrayBuffer(secretKey: string, message: Message): ArrayBuffer;
/**
* Return hash in integer array.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
digest(secretKey: string, message: Message): number[];
/**
* Return hash in integer array.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
array(secretKey: string, message: Message): number[];
}
interface Hash {
/**
* Hash and return hex string.
*
* @param message The message you want to hash.
*/
(message: Message): string;
/**
* Hash and return hex string.
*
* @param message The message you want to hash.
*/
hex(message: Message): string;
/**
* Hash and return ArrayBuffer.
*
* @param message The message you want to hash.
*/
arrayBuffer(message: Message): ArrayBuffer;
/**
* Hash and return integer array.
*
* @param message The message you want to hash.
*/
digest(message: Message): number[];
/**
* Hash and return integer array.
*
* @param message The message you want to hash.
*/
array(message: Message): number[];
/**
* Create a hash object.
*/
create(): Hasher;
/**
* Create a hash object and hash message.
*
* @param message The message you want to hash.
*/
update(message: Message): Hasher;
/**
* HMAC interface
*/
hmac: Hmac;
}
export var sha512: Hash;
export var sha384: Hash;
export var sha512_256: Hash;
export var sha512_224: Hash;

1979
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -1,19 +1,21 @@
{
"name": "js-sha512",
"version": "0.3.0",
"version": "0.9.0",
"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",
"devDependencies": {
"expect.js": "~0.3.1",
"jscoverage": "~0.5.9",
"mocha": "~2.3.4",
"uglifyjs": "~2.4.10"
"mocha": "~10.2.0",
"nyc": "^15.1.0",
"requirejs": "^2.1.22",
"tiny-worker": "^2.3.0",
"uglify-js": "^3.1.9"
},
"scripts": {
"test": "mocha tests/node-test.js -r jscoverage",
"report": "mocha tests/node-test.js -r jscoverage --covout=html",
"coveralls": "mocha tests/node-test.js -R mocha-lcov-reporter -r jscoverage | coveralls",
"build": "uglifyjs src/sha512.js --compress --mangle --comments --output build/sha512.min.js"
"test": "nyc mocha tests/node-test.js",
"report": "nyc --reporter=html --reporter=text mocha tests/node-test.js --exit",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"build": "uglifyjs src/sha512.js -c -m eval --comments -o build/sha512.min.js"
},
"repository": {
"type": "git",
@ -36,5 +38,10 @@
"homepage": "https://github.com/emn178/js-sha512",
"bugs": {
"url": "https://github.com/emn178/js-sha512/issues"
},
"nyc": {
"exclude": [
"tests"
]
}
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,290 @@
(function (sha512, sha384, sha512_256, sha512_224) {
Array.prototype.toHexString = ArrayBuffer.prototype.toHexString = function () {
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;
};
var testCases = {
sha512_hmac: {
'Test Vectors': {
'87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854': [
[0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b],
'Hi There'
],
'164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737': [
'Jefe',
'what do ya want for nothing?'
],
'fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb': [
[0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa],
[0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd]
],
'b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd': [
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19],
[0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd]
],
'80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598': [
[0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa],
'Test Using Larger Than Block-Size Key - Hash Key First'
],
'e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58': [
[0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa],
'This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm.'
]
},
'UTF8': {
'8a2f8205e690f84981727a6c49f8131d6a76030e39ba69e08adff7ab344c58ae1c20c22532b61d1f0410d3174e2b3f3dd08e9d1d82e86c960683eddf279a8239': ['中文', '中文'],
'bb31bfef7be475a69f612741728c72237106053752a2804618c0b2d68cfe78cec6df18f4ae1fc0830f1153c1c497d817315c0129d84329b7f8c1d818306111d2': ['aécio', 'aécio'],
'cba25df64cf81b67d2476bb812be931263561f219f423f723aa7be2e5c3cd3566a8adbc77663b6b6a69742f8f866f8afbff9b52769e2a53baae6d6a77d010c8a': ['𠜎', '𠜎']
}
},
sha384_hmac: {
'Test Vectors': {
'afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59cfaea9ea9076ede7f4af152e8b2fa9cb6': [
[0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b],
'Hi There'
],
'af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649': [
'Jefe',
'what do ya want for nothing?'
],
'88062608d3e6ad8a0aa2ace014c8a86f0aa635d947ac9febe83ef4e55966144b2a5ab39dc13814b94e3ab6e101a34f27': [
[0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa],
[0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd]
],
'3e8a69b7783c25851933ab6290af6ca77a9981480850009cc5577c6e1f573b4e6801dd23c4a7d679ccf8a386c674cffb': [
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19],
[0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd]
],
'4ece084485813e9088d2c63a041bc5b44f9ef1012a2b588f3cd11f05033ac4c60c2ef6ab4030fe8296248df163f44952': [
[0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa],
'Test Using Larger Than Block-Size Key - Hash Key First'
],
'6617178e941f020d351e2f254e8fd32c602420feb0b8fb9adccebb82461e99c5a678cc31e799176d3860e6110c46523e': [
[0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa],
'This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm.'
]
},
'UTF8': {
'dd85fd0f7ae32037040ef2cfc344430addacf80f46f52c2ee066ab89ce6dedabb7d0a08cdb8765a7cfa616c7f4ddd377': ['中文', '中文'],
'9f7abdeda88240304f987ecf11ceea9bd6571de782a2c73505f9dcaf9a58ec08cf69d078fc80d13ceb18e366f9537b0d': ['aécio', 'aécio'],
'371f68d3377c583a29a38157348e4cc76d09aad3d86a5f4fdac661b2feefd7533338ffaf987267ac0ce3bb85e7787fce': ['𠜎', '𠜎']
}
},
sha512_256_hmac: {
'Test Vectors': {
'9f9126c3d9c3c330d760425ca8a217e31feae31bfe70196ff81642b868402eab': [
[0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b],
'Hi There'
],
'6df7b24630d5ccb2ee335407081a87188c221489768fa2020513b2d593359456': [
'Jefe',
'what do ya want for nothing?'
]
}
},
sha512_224_hmac: {
'Test Vectors': {
'b244ba01307c0e7a8ccaad13b1067a4cf6b961fe0c6a20bda3d92039': [
[0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b],
'Hi There'
],
'4a530b31a79ebcce36916546317c45f247d83241dfb818fd37254bde': [
'Jefe',
'what do ya want for nothing?'
]
}
}
};
if (!(typeof JS_SHA512_NO_ARRAY_BUFFER === 'boolean' && JS_SHA512_NO_ARRAY_BUFFER)) {
testCases.sha512_hmac['ArrayBuffer'] = {
'f7688a104326d36c1940f6d28d746c0661d383e0d14fe8a04649444777610f5dd9565a36846ab9e9e734cf380d3a070d8ef021b5f3a50c481710a464968e3419': [
new ArrayBuffer(0),
'Hi There'
]
};
testCases.sha512_hmac['Uint8Array'] = {
'f7688a104326d36c1940f6d28d746c0661d383e0d14fe8a04649444777610f5dd9565a36846ab9e9e734cf380d3a070d8ef021b5f3a50c481710a464968e3419': [
new Uint8Array(0),
'Hi There'
]
};
testCases.sha384_hmac['ArrayBuffer'] = {
'da5393cef424a670d6db42c6ed6e7920779dfa4cbb98bf1c2e9c12ae10d10905d0c9e9d576c2a613be54b8daea246d4b': [
new ArrayBuffer(0),
'Hi There'
]
};
testCases.sha384_hmac['Uint8Array'] = {
'da5393cef424a670d6db42c6ed6e7920779dfa4cbb98bf1c2e9c12ae10d10905d0c9e9d576c2a613be54b8daea246d4b': [
new Uint8Array(0),
'Hi There'
]
};
}
var errorTestCases = [null, undefined, { length: 0 }, 0, 1, false, true, NaN, Infinity, function () {}];
function runTestCases(name, algorithm) {
var methods = [
{
name: name,
call: algorithm,
},
{
name: name + '.hex',
call: algorithm.hex
},
{
name: name + '.array',
call: function (key, message) {
return algorithm.array(key, message).toHexString();
}
},
{
name: name + '.digest',
call: function (key, message) {
return algorithm.digest(key, message).toHexString();
}
},
{
name: name + '.arrayBuffer',
call: function (key, message) {
return algorithm.arrayBuffer(key, message).toHexString();
}
}
];
var classMethods = [
{
name: 'create',
call: function (key, message) {
return algorithm.create(key).update(message).toString();
}
},
{
name: 'update',
call: function (key, message) {
return algorithm.update(key, message).toString();
}
},
{
name: 'hex',
call: function (key, message) {
return algorithm.update(key, message).hex();
}
},
{
name: 'array',
call: function (key, message) {
return algorithm.update(key, message).array().toHexString();
}
},
{
name: 'digest',
call: function (key, message) {
return algorithm.update(key, message).digest().toHexString();
}
},
{
name: 'arrayBuffer',
call: function (key, message) {
return algorithm.update(key, message).arrayBuffer().toHexString();
}
},
{
name: 'finalize',
call: function (key, message) {
var hash = algorithm.update(key, message);
hash.hex();
hash.finalize();
return hash.hex();
}
},
{
name: 'clone',
call: function (key, message) {
var hash = algorithm.update(key, message);
var hash2 = hash.clone();
hash.update('any');
return hash2.hex();
}
}
];
var subTestCases = testCases[name];
describe(name, function () {
methods.forEach(function (method) {
describe('#' + method.name, function () {
for (var testCaseName in subTestCases) {
(function (testCaseName) {
var testCase = subTestCases[testCaseName];
context('when ' + testCaseName, function () {
for (var hash in testCase) {
(function (message, hash) {
it('should be equal', function () {
expect(method.call(message[0], message[1])).to.be(hash);
});
})(testCase[hash], hash);
}
});
})(testCaseName);
}
});
});
classMethods.forEach(function (method) {
describe('#' + method.name, function () {
for (var testCaseName in subTestCases) {
(function (testCaseName) {
var testCase = subTestCases[testCaseName];
context('when ' + testCaseName, function () {
for (var hash in testCase) {
(function (message, hash) {
it('should be equal', function () {
expect(method.call(message[0], message[1])).to.be(hash);
});
})(testCase[hash], hash);
}
});
})(testCaseName);
}
});
});
context('when update after finalize', function () {
it('should throw error', function () {
expect(function () {
var hash = algorithm.update('key', 'any');
hash.hex();
hash.update('any');
}).to.throwError(/finalize already called/);
});
});
describe('#' + name, function () {
errorTestCases.forEach(function (testCase) {
context('when ' + testCase, function () {
it('should throw error', function () {
expect(function () {
algorithm(testCase, '');
}).to.throwError(/input is invalid type/);
});
});
});
});
});
}
runTestCases('sha512_hmac', sha512.hmac);
runTestCases('sha384_hmac', sha384.hmac);
runTestCases('sha512_256_hmac', sha512_256.hmac);
runTestCases('sha512_224_hmac', sha512_224.hmac);
})(sha512, sha384, sha512_256, sha512_224);

@ -3,9 +3,9 @@
<head>
<meta charset="UTF-8">
<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>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/expect.js/index.js"></script>
<script src="../src/sha512.js"></script>
</head>
<body>
@ -14,6 +14,7 @@
mocha.setup('bdd');
</script>
<script src="test.js"></script>
<script src="hmac-test.js"></script>
<script>
mocha.checkLeaks();
mocha.run();

@ -1,51 +1,75 @@
// Node.js env
expect = require('expect.js');
sha512 = require('../src/sha512.js').sha512;
sha384 = require('../src/sha512.js').sha384;
sha512_256 = require('../src/sha512.js').sha512_256;
sha512_224 = require('../src/sha512.js').sha512_224;
require('./test.js');
delete require.cache[require.resolve('../src/sha512.js')];
delete require.cache[require.resolve('./test.js')];
sha512 = null;
sha384 = null;
sha512_256 = null;
sha512_224 = null;
Worker = require("tiny-worker");
function unset() {
delete require.cache[require.resolve('../src/sha512.js')];
delete require.cache[require.resolve('./test.js')];
delete require.cache[require.resolve('./hmac-test.js')];
sha512 = null;
sha384 = null;
sha512_256 = null;
sha512_224 = null;
BUFFER = undefined;
JS_SHA512_NO_WINDOW = undefined;
JS_SHA512_NO_NODE_JS = undefined;
JS_SHA512_NO_COMMON_JS = undefined;
JS_SHA512_NO_ARRAY_BUFFER = undefined;
JS_SHA512_NO_ARRAY_BUFFER_IS_VIEW = undefined;
window = undefined;
}
function requireToGlobal() {
sha512 = require('../src/sha512.js').sha512;
sha384 = require('../src/sha512.js').sha384;
sha512_256 = require('../src/sha512.js').sha512_256;
sha512_224 = require('../src/sha512.js').sha512_224;
}
function runCommonJsTest() {
requireToGlobal();
require('./test.js');
require('./hmac-test.js');
unset();
}
function runWindowTest() {
window = global;
require('../src/sha512.js');
require('./test.js');
require('./hmac-test.js');
unset();
}
// Node.js env
BUFFER = true;
runCommonJsTest();
// Webpack browser env
JS_SHA512_NO_NODE_JS = true;
window = global;
sha512 = require('../src/sha512.js').sha512;
sha384 = require('../src/sha512.js').sha384;
sha512_256 = require('../src/sha512.js').sha512_256;
sha512_224 = require('../src/sha512.js').sha512_224;
require('./test.js');
delete require.cache[require.resolve('../src/sha512.js')];
delete require.cache[require.resolve('./test.js')];
sha512 = null;
sha384 = null;
sha512_256 = null;
sha512_224 = null;
runCommonJsTest();
// browser env
JS_SHA512_NO_NODE_JS = true;
JS_SHA512_NO_COMMON_JS = true;
window = global;
require('../src/sha512.js');
require('./test.js');
runWindowTest();
delete require.cache[require.resolve('../src/sha512.js')];
delete require.cache[require.resolve('./test.js')];
sha512 = null;
sha384 = null;
sha512_256 = null;
sha512_224 = null;
// browser env and no array buffer
JS_SHA512_NO_NODE_JS = true;
JS_SHA512_NO_COMMON_JS = true;
JS_SHA512_NO_ARRAY_BUFFER = true;
runWindowTest();
// browser env and no isView
JS_SHA512_NO_NODE_JS = true;
JS_SHA512_NO_COMMON_JS = true;
JS_SHA512_NO_ARRAY_BUFFER_IS_VIEW = true;
runWindowTest();
// browser AMD
JS_SHA512_NO_NODE_JS = true;
JS_SHA512_NO_COMMON_JS = true;
JS_SHA512_NO_ARRAY_BUFFER_IS_VIEW = false;
window = global;
define = function (func) {
sha512 = func();
@ -53,7 +77,43 @@ define = function (func) {
sha512_256 = sha512.sha512_256;
sha512_224 = sha512.sha512_224;
require('./test.js');
require('./hmac-test.js');
};
define.amd = true;
require('../src/sha512.js');
unset();
// webworker
WORKER = 'tests/worker.js';
SOURCE = 'src/sha512.js';
require('./worker-test.js');
delete require.cache[require.resolve('./worker-test.js')];
// cover webworker
JS_SHA512_NO_WINDOW = true;
JS_SHA512_NO_NODE_JS = true;
WORKER = './worker.js';
SOURCE = '../src/sha512.js';
window = global;
self = global;
Worker = function (file) {
require(file);
currentWorker = this;
this.postMessage = function (data) {
onmessage({data: data});
};
}
postMessage = function (data) {
currentWorker.onmessage({data: data});
}
importScripts = function () {};
requireToGlobal();
require('./worker-test.js');

@ -0,0 +1,27 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SHA512</title>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/expect.js/index.js"></script>
<script src="../node_modules/requirejs/require.js"></script>
</head>
<body>
<div id="mocha"></div>
<script>
mocha.setup('bdd');
require(['../src/sha512.js'], function (sha512) {
window.sha512 = sha512;
window.sha384 = sha512.sha384;
window.sha512_256 = sha512.sha512_256;
window.sha512_224 = sha512.sha512_224;
require(['test.js'], function () {
mocha.checkLeaks();
mocha.run();
});
});
</script>
</body>
</html>

@ -1,177 +1,353 @@
(function (sha512, sha384, sha512_256, sha512_224) {
describe('sha512', function () {
describe('ascii', function () {
describe('less than 128 bytes', function () {
it('should be successful', function () {
expect(sha512('')).to.be('cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e');
expect(sha512('The quick brown fox jumps over the lazy dog')).to.be('07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6');
expect(sha512('The quick brown fox jumps over the lazy dog.')).to.be('91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed');
});
});
describe('more than 128 bytes', function () {
it('should be successful', function () {
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');
});
});
});
describe('UTF8', function () {
describe('less than 128 bytes', function () {
it('should be successful', function () {
expect(sha512('中文')).to.be('8b88efc2ebbcbdad5ac2d65af05bec57bda25e71fd5fb25bbd892057a2755fbd05d8d8491cb2946febd5b0f124ffdfbaecf7e34946353c4f1b5ab29545895468');
expect(sha512('aécio')).to.be('e1c6925243db76985abacaf9fa85e22697f549e67f65a36c88e4046a2260990ff9eefc3402396ea8dcbe8c592d8d5671bea612156eda38d3708d394bbd17d493');
expect(sha512('𠜎')).to.be('f3e7ee9cdf7dbb52f7edd59ce3d49868c64f2b3aceceab060b8eaaebdf9de0dae5866d660e3319c5aad426a2176cb1703efc73eb24d1a90458ceda1b7f4e3940');
});
});
describe('more than 128 bytes', function () {
it('should be successful', function () {
expect(sha512('訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('6cb7f6d3381a187edadb43c7cdcfbbed4d2c213a7dce8ea08fe42b9882b64e643202b4974a6db94f94650ab9173d97c58bd59f6d19d27e01aab76d8d08855c65');
expect(sha512('訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一又譯雜湊演算法、摘要演算法等主流程式語言普遍已有MD5的實作。')).to.be('d24af1901aaf1458f089a6eddf784ce61c3012aee0df98bdb67ad2dc6b41a3b4051d40caac524373930ae396a2dde99a9204871b40892eea3e5f3c8d46da0c3c');
});
});
});
describe('special length', function () {
it('should be successful', function () {
expect(sha512('012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345')).to.be('a58b6132677883b0ab565dfb4284a4441a35a0954738d90ad8c17800de5ee44f6482efd4c13e05a6e907fb830fc9d9039bd96f004e5e3e8d00802fb9448dec03');
expect(sha512('0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456')).to.be('34a93615cbf314a799857dc9c1804ed04be60b481229ea3a53e00316e62ac7924ea7e65435cbd9fc7d8e10e0e52e56bd99ac571c1c470cbf9252bdd70b3fd8a3');
expect(sha512('123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567')).to.be('4555ce42c2e19f9a09a34b3e40f85178c22cde939ac42de4d33ca0e08e3c6a2e531a3abd9265a72cc22a19d085eb2c468c20d6cfb022d8914b53d6a2b485b7c9');
expect(sha512('1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678')).to.be('8bfa769385fe4b7d4aabf304498522d5aefa5356e42e9ae5ac9fa6b00a483685586cc0fa44da3df335719cbb524b12433afda18361d70e4c584ea81a927c670c');
});
});
});
Array.prototype.toHexString = ArrayBuffer.prototype.toHexString = function () {
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('sha384', function () {
describe('ascii', function () {
describe('less than 128 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.')).to.be('ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7');
});
});
describe('more than 128 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');
});
});
});
var testCases = {
sha512: {
'ascii': {
'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e': '',
'07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6': 'The quick brown fox jumps over the lazy dog',
'91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed': 'The quick brown fox jumps over the lazy dog.'
},
'ascii more than 64 bytes': {
'a8dedff31e3be9df6413ef5b4ecb93d62d3fbcb04297552eab5370e04afd45927854a4373037e81a50186e678d818c9ba824f4c850f3d0f02764af0252076979': '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.'
},
'UTF8': {
'8b88efc2ebbcbdad5ac2d65af05bec57bda25e71fd5fb25bbd892057a2755fbd05d8d8491cb2946febd5b0f124ffdfbaecf7e34946353c4f1b5ab29545895468': '中文',
'e1c6925243db76985abacaf9fa85e22697f549e67f65a36c88e4046a2260990ff9eefc3402396ea8dcbe8c592d8d5671bea612156eda38d3708d394bbd17d493': 'aécio',
'f3e7ee9cdf7dbb52f7edd59ce3d49868c64f2b3aceceab060b8eaaebdf9de0dae5866d660e3319c5aad426a2176cb1703efc73eb24d1a90458ceda1b7f4e3940': '𠜎',
'2665b06c3aa040d34e3ebeb8eb462a23e9789be9aa44c5242213f33c9bf18d391325de84cd91e1cb8b586739a9037acbf606eb8139a64e0244cdc4779faf62f7': 'Tehtäväsi on muotoilla teksti, jossa käyttäjälle suositellaan hänen henkilökohtaisiin tietoihinsa perustuen avioehdon tekemistä. Suosittelemme nimenomaan tätä tuotetta käyttäjän henkilökohtaisista tiedoista johtuen. Tuottamasi tekstin tulee olla lyhyt, persoonallinen, ymmärrettävä ja todenmukainen. Suositus perustuu seuraaviin seikkoihin:\n\nSeikka 0: Avioehdon tekeminen on verohyötyjen vuoksi suositeltavaa, koska olet ilmaissut että haluat kuoleman tapauksessa turvata ensisijaisesti aviopuolisosi asemaa (ennemmin kuin lasten asemaa). Avioehdolla voit määrätä, että puolet kuolleen puolison omaisuudesta siirtyy verovapaasti leskelle.\n\nVoit halutessasi hyödyntää seuraavia tietoja tekstin muotoilussa persoonallisemmaksi: Käyttäjällä on kumppani nimeltä PARTNER_NAME. Käyttäjällä on suurperhe. Käyttäjällä on lapsiperhe.\n\nPuhuttele käyttäjää siihen sävyyn että tiedät jo mitä käyttäjä haluaa, koska hän on kertonut toiveistaan, ja niiden perusteella tehty suositus on ilmiselvä. Joten älä käytä epävarmoja ilmaisuja kuten "halutessasi", vaan kirjoita itsevarmoilla ilmaisuilla kuten "koska haluat". Älä tee oletuksia käyttäjän sukupuolesta, emme tiedä onko hän mies vai nainen. Älä käytä sanaa jälkikasvu, puhu ennemmin lapsesta tai lapsista riippuen onko lapsia yksi vai useampia. Älä puhuttele käyttäjää ensimmäisessä persoonassa, käytä ennemmin passiivimuotoa. Tekstin sävyn tulisi olla neutraalin asiallinen, ei melodramaattinen eikä leikkisä. Pysy totuudessa, älä keksi uusia seikkoja yllä listattujen lisäksi. Viittaa ihmisiin nimillä silloin kun se on mahdollista. Tekstin tulisi olla vain muutaman lauseen mittainen. Älä siis kirjoita pitkiä selityksiä äläkä kirjoita listoja. Tiivistä oleellinen tieto lyhyeksi ja persoonalliseksi tekstiksi. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
},
'UTF8 more than 64 bytes': {
'6cb7f6d3381a187edadb43c7cdcfbbed4d2c213a7dce8ea08fe42b9882b64e643202b4974a6db94f94650ab9173d97c58bd59f6d19d27e01aab76d8d08855c65': '訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一',
'd24af1901aaf1458f089a6eddf784ce61c3012aee0df98bdb67ad2dc6b41a3b4051d40caac524373930ae396a2dde99a9204871b40892eea3e5f3c8d46da0c3c': '訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一又譯雜湊演算法、摘要演算法等主流程式語言普遍已有MD5的實作。'
},
'special length': {
'a58b6132677883b0ab565dfb4284a4441a35a0954738d90ad8c17800de5ee44f6482efd4c13e05a6e907fb830fc9d9039bd96f004e5e3e8d00802fb9448dec03': '012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345',
'34a93615cbf314a799857dc9c1804ed04be60b481229ea3a53e00316e62ac7924ea7e65435cbd9fc7d8e10e0e52e56bd99ac571c1c470cbf9252bdd70b3fd8a3': '0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456',
'4555ce42c2e19f9a09a34b3e40f85178c22cde939ac42de4d33ca0e08e3c6a2e531a3abd9265a72cc22a19d085eb2c468c20d6cfb022d8914b53d6a2b485b7c9': '123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567',
'8bfa769385fe4b7d4aabf304498522d5aefa5356e42e9ae5ac9fa6b00a483685586cc0fa44da3df335719cbb524b12433afda18361d70e4c584ea81a927c670c': '1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678'
},
'Array': {
'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e': [],
'07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6': [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]
}
},
sha384: {
'ascii': {
'38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b': '',
'ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1': 'The quick brown fox jumps over the lazy dog',
'ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7': 'The quick brown fox jumps over the lazy dog.'
},
'ascii more than 64 bytes': {
'3ab853dee5785c19346e8578d9cdb3b7258a8212c15fdf25d3055b4fbbc892a529e552788992fdae68dfa34f13f0af4e': '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.'
},
'UTF8': {
'93422ceb8291a69b22f02dc1114c39a287493ad525dcebc77e4019a44eaee2633a85d0f29cd298ee6799048c33a4be0c': '中文',
'a2146805faafc45b0055b49386768811c803ef9fa8a85b648e114276c1bf49ef1092ec1bc2d3f7e036238a97eace2087': 'aécio',
'e929475cf9b4d73e2a2366146381ac779b17499c92ddf910e028eb9f30280878a2843643ed8a539af5dffed64143b93d': '𠜎'
},
'UTF8 more than 64 bytes': {
'143f4bb1a89e576ea05bd8433a3903352c199bf6ef35bfa30afa1505d6fcef77476d1b1824105da160a5164b7c792328': '訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一',
'432a8d25efb1a8d463acc7bbbed57365cddc72de3cc19d42e1d260b1479770ecea9dd403fead2949d4fabb36939f263a': '訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一又譯雜湊演算法、摘要演算法等主流程式語言普遍已有MD5的實作。'
},
'special length': {
'a0ecc55799e1db4e7b30c2e78d445ada5ea2939d47e8aab51d6ae86cdcf93d5048b2666aa25fbaede7ec59ee59f46e88': '012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345',
'484b51b9f03f453a08c823afea1a32434b6545fb682b0b91d94e1f4bf2812abc8a31387050a2e74909f862436b88c798': '0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456',
'e4c83d6e5e18b4391a0b2e5fab84234ce9fc1071d87a9dcf38ca050ca3635781ef1d65ad4f9a835df26d186384105eac': '123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567',
'd7f6874057808f9e2eaa4eddace032c7bd548fbe3989be6591910277a25dee5972e1297deced3ad7b4e3040de88855f6': '1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678'
},
'Array': {
'38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b': [],
'ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1': [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]
}
},
sha512_256: {
'ascii': {
'c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a': '',
'dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d': 'The quick brown fox jumps over the lazy dog',
'1546741840f8a492b959d9b8b2344b9b0eb51b004bba35c0aebaac86d45264c3': 'The quick brown fox jumps over the lazy dog.'
},
'ascii more than 64 bytes': {
'21e2e940930b23f1de6377086d07e22033c6bbf3fd9fbf4b62ec66e6c08c25be': '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.'
},
'UTF8': {
'b6dab29c16ec35ab34a5d92ff135b58de96741dda78b1009a2181cf8b45d2f72': '中文',
'122802ca08e39c2ef46f6a81379dc5683bd8aa074dfb54259f0add4d8b5504bc': 'aécio',
'1032308151c0f4f5f8d4e0d96956352eb8ff87da98df8878d8795a858a7e7c08': '𠜎'
},
'UTF8 more than 64 bytes': {
'd32a41d9858e45b68402f77cf9f3c3f992c36a4bffd230f78d666c87f97eaf7e': '訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一',
'bd1abad59e6b8ad69bc17b6e05aa13f0cb725467fbeb45b83d3e4094332d1367': '訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一又譯雜湊演算法、摘要演算法等主流程式語言普遍已有MD5的實作。'
},
'special length': {
'b8bd1d6cae9992f8619904683d54cbed560ec3ef2a33d8bc225175ec27dc09a4': '012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345',
'4edb8bb93b959f482ce2e434585f3e164f53b00c42c4c46797445b39d225cfe3': '0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456',
'44da7ca023996ec4b4ded88410e4acc209ca26c34dc0f26550d28503974fe2d1': '123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567',
'465859c0edc706a2a9c39d3175a715fdf6b764ccc43d45b5668c56f6d8aa9b9e': '1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678'
},
'Array': {
'c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a': [],
'dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d': [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]
}
},
sha512_224: {
'ascii': {
'6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4': '',
'944cd2847fb54558d4775db0485a50003111c8e5daa63fe722c6aa37': 'The quick brown fox jumps over the lazy dog',
'6d6a9279495ec4061769752e7ff9c68b6b0b3c5a281b7917ce0572de': 'The quick brown fox jumps over the lazy dog.'
},
'ascii more than 64 bytes': {
'2e962464977b198ee758d615bbc92251ad2e3c0960068e279fd21d2f': '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.'
},
'UTF8': {
'0f46a0ae7f226517dd66ece0ce1efa29ffb7ced05ac4566fdcaed188': '中文',
'562f2e4ee7f7451d20dcc6a0ac1a1e1c4a75f09baaf1cf19af3e15f4': 'aécio',
'0533318c52b3d4ad355c2a6c7e727ae3d2efa749db480ac33560b059': '𠜎'
},
'UTF8 more than 64 bytes': {
'f67e191a5d4ee67a272ccaf6cf597f0c4d6a0c46bd631be7cadb0944': '訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一',
'009c3d1e3172d6df71344982eada855421592aea28acbf660ada7569': '訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一又譯雜湊演算法、摘要演算法等主流程式語言普遍已有MD5的實作。'
},
'special length': {
'36d6398ca22d885432e2fe3af53b9f83b2dc7a8e9284d65b81f7a879': '012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345',
'd930407a742d10f70747f05271181ad6c1c92f87482ce6a494cb566a': '0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456',
'1d60954ce7bee0bf5d235fe6bfc928c0f32c0f1c4992451dbc0f4dbf': '123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567',
'672aef84d579799cedf2fec090ce4ea0d72e0edd2788deaa8200fd67': '1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678'
},
'Array': {
'6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4': [],
'944cd2847fb54558d4775db0485a50003111c8e5daa63fe722c6aa37': [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]
}
}
};
describe('UTF8', function () {
describe('less than 128 bytes', function () {
it('should be successful', function () {
expect(sha384('中文')).to.be('93422ceb8291a69b22f02dc1114c39a287493ad525dcebc77e4019a44eaee2633a85d0f29cd298ee6799048c33a4be0c');
expect(sha384('aécio')).to.be('a2146805faafc45b0055b49386768811c803ef9fa8a85b648e114276c1bf49ef1092ec1bc2d3f7e036238a97eace2087');
expect(sha384('𠜎')).to.be('e929475cf9b4d73e2a2366146381ac779b17499c92ddf910e028eb9f30280878a2843643ed8a539af5dffed64143b93d');
});
});
if (!(typeof JS_SHA512_NO_ARRAY_BUFFER === 'boolean' && JS_SHA512_NO_ARRAY_BUFFER)) {
testCases.sha512['Uint8Array'] = {
'07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])
};
testCases.sha512['Int8Array'] = {
'07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])
};
testCases.sha512['ArrayBuffer'] = {
'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e': new ArrayBuffer(0),
'b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee': new ArrayBuffer(1)
};
testCases.sha384['Uint8Array'] = {
'ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])
};
testCases.sha384['Int8Array'] = {
'ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])
};
testCases.sha384['ArrayBuffer'] = {
'38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b': new ArrayBuffer(0),
'bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933ec2b413465966817a9c208a11717': new ArrayBuffer(1)
};
testCases.sha512_256['Uint8Array'] = {
'dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])
};
testCases.sha512_256['Int8Array'] = {
'dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])
};
testCases.sha512_256['ArrayBuffer'] = {
'c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a': new ArrayBuffer(0),
'10baad1713566ac2333467bddb0597dec9066120dd72ac2dcb8394221dcbe43d': new ArrayBuffer(1)
};
testCases.sha512_224['Uint8Array'] = {
'944cd2847fb54558d4775db0485a50003111c8e5daa63fe722c6aa37': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])
};
testCases.sha512_224['Int8Array'] = {
'944cd2847fb54558d4775db0485a50003111c8e5daa63fe722c6aa37': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])
};
testCases.sha512_224['ArrayBuffer'] = {
'6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4': new ArrayBuffer(0),
'283bb59af7081ed08197227d8f65b9591ffe1155be43e9550e57f941': new ArrayBuffer(1)
};
}
describe('more than 128 bytes', function () {
it('should be successful', function () {
expect(sha384('訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('143f4bb1a89e576ea05bd8433a3903352c199bf6ef35bfa30afa1505d6fcef77476d1b1824105da160a5164b7c792328');
expect(sha384('訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一又譯雜湊演算法、摘要演算法等主流程式語言普遍已有MD5的實作。')).to.be('432a8d25efb1a8d463acc7bbbed57365cddc72de3cc19d42e1d260b1479770ecea9dd403fead2949d4fabb36939f263a');
});
});
});
if (typeof BUFFER === 'boolean' && BUFFER) {
testCases.sha512['Buffer'] = {
'07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6': new Buffer(new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]))
};
testCases.sha384['Buffer'] = {
'ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1': new Buffer(new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]))
};
testCases.sha512_256['Buffer'] = {
'dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d': new Buffer(new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]))
};
testCases.sha512_224['Buffer'] = {
'944cd2847fb54558d4775db0485a50003111c8e5daa63fe722c6aa37': new Buffer(new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]))
};
}
describe('special length', function () {
it('should be successful', function () {
expect(sha384('012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345')).to.be('a0ecc55799e1db4e7b30c2e78d445ada5ea2939d47e8aab51d6ae86cdcf93d5048b2666aa25fbaede7ec59ee59f46e88');
expect(sha384('0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456')).to.be('484b51b9f03f453a08c823afea1a32434b6545fb682b0b91d94e1f4bf2812abc8a31387050a2e74909f862436b88c798');
expect(sha384('123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567')).to.be('e4c83d6e5e18b4391a0b2e5fab84234ce9fc1071d87a9dcf38ca050ca3635781ef1d65ad4f9a835df26d186384105eac');
expect(sha384('1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678')).to.be('d7f6874057808f9e2eaa4eddace032c7bd548fbe3989be6591910277a25dee5972e1297deced3ad7b4e3040de88855f6');
});
});
});
var errorTestCases = [null, undefined, { length: 0 }, 0, 1, false, true, NaN, Infinity, function () {}];
describe('sha512/256', function () {
describe('ascii', function () {
describe('less than 128 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.')).to.be('1546741840f8a492b959d9b8b2344b9b0eb51b004bba35c0aebaac86d45264c3');
});
});
function runTestCases(name, algorithm) {
var methods = [
{
name: name,
call: algorithm,
},
{
name: name + '.hex',
call: algorithm.hex
},
{
name: name + '.array',
call: function (message) {
return algorithm.array(message).toHexString();
}
},
{
name: name + '.digest',
call: function (message) {
return algorithm.digest(message).toHexString();
}
},
{
name: name + '.arrayBuffer',
call: function (message) {
return algorithm.arrayBuffer(message).toHexString();
}
}
];
describe('more than 128 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');
});
});
});
var classMethods = [
{
name: 'create',
call: function (message) {
return algorithm.create().update(message).toString();
}
},
{
name: 'update',
call: function (message) {
return algorithm.update(message).toString();
}
},
{
name: 'hex',
call: function (message) {
return algorithm.update(message).hex();
}
},
{
name: 'array',
call: function (message) {
return algorithm.update(message).array().toHexString();
}
},
{
name: 'digest',
call: function (message) {
return algorithm.update(message).digest().toHexString();
}
},
{
name: 'arrayBuffer',
call: function (message) {
return algorithm.update(message).arrayBuffer().toHexString();
}
},
{
name: 'finalize',
call: function (message) {
var hash = algorithm.update(message);
hash.hex();
hash.finalize();
return hash.hex();
}
},
{
name: 'clone',
call: function (message) {
var hash = algorithm.update(message);
var hash2 = hash.clone();
hash.update('any');
return hash2.hex();
}
}
];
describe('UTF8', function () {
describe('less than 128 bytes', function () {
it('should be successful', function () {
expect(sha512_256('中文')).to.be('b6dab29c16ec35ab34a5d92ff135b58de96741dda78b1009a2181cf8b45d2f72');
expect(sha512_256('aécio')).to.be('122802ca08e39c2ef46f6a81379dc5683bd8aa074dfb54259f0add4d8b5504bc');
expect(sha512_256('𠜎')).to.be('1032308151c0f4f5f8d4e0d96956352eb8ff87da98df8878d8795a858a7e7c08');
});
});
var subTestCases = testCases[name];
describe('more than 128 bytes', function () {
it('should be successful', function () {
expect(sha512_256('訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('d32a41d9858e45b68402f77cf9f3c3f992c36a4bffd230f78d666c87f97eaf7e');
expect(sha512_256('訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一又譯雜湊演算法、摘要演算法等主流程式語言普遍已有MD5的實作。')).to.be('bd1abad59e6b8ad69bc17b6e05aa13f0cb725467fbeb45b83d3e4094332d1367');
describe(name, function () {
methods.forEach(function (method) {
describe('#' + method.name, function () {
for (var testCaseName in subTestCases) {
(function (testCaseName) {
var testCase = subTestCases[testCaseName];
context('when ' + testCaseName, function () {
for (var hash in testCase) {
(function (message, hash) {
it('should be equal', function () {
expect(method.call(message)).to.be(hash);
});
})(testCase[hash], hash);
}
});
})(testCaseName);
}
});
});
});
describe('special length', function () {
it('should be successful', function () {
expect(sha512_256('012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345')).to.be('b8bd1d6cae9992f8619904683d54cbed560ec3ef2a33d8bc225175ec27dc09a4');
expect(sha512_256('0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456')).to.be('4edb8bb93b959f482ce2e434585f3e164f53b00c42c4c46797445b39d225cfe3');
expect(sha512_256('123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567')).to.be('44da7ca023996ec4b4ded88410e4acc209ca26c34dc0f26550d28503974fe2d1');
expect(sha512_256('1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678')).to.be('465859c0edc706a2a9c39d3175a715fdf6b764ccc43d45b5668c56f6d8aa9b9e');
});
});
});
describe('sha512/224', function () {
describe('ascii', function () {
describe('less than 128 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.')).to.be('6d6a9279495ec4061769752e7ff9c68b6b0b3c5a281b7917ce0572de');
classMethods.forEach(function (method) {
describe('#' + method.name, function () {
for (var testCaseName in subTestCases) {
(function (testCaseName) {
var testCase = subTestCases[testCaseName];
context('when ' + testCaseName, function () {
for (var hash in testCase) {
(function (message, hash) {
it('should be equal', function () {
expect(method.call(message)).to.be(hash);
});
})(testCase[hash], hash);
}
});
})(testCaseName);
}
});
});
describe('more than 128 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');
errorTestCases.forEach(function (testCase) {
context('when ' + testCase, function () {
it('should throw error', function () {
expect(function () {
algorithm(testCase);
}).to.throwError(/input is invalid type/);
});
});
});
});
describe('UTF8', function () {
describe('less than 128 bytes', function () {
it('should be successful', function () {
expect(sha512_224('中文')).to.be('0f46a0ae7f226517dd66ece0ce1efa29ffb7ced05ac4566fdcaed188');
expect(sha512_224('aécio')).to.be('562f2e4ee7f7451d20dcc6a0ac1a1e1c4a75f09baaf1cf19af3e15f4');
expect(sha512_224('𠜎')).to.be('0533318c52b3d4ad355c2a6c7e727ae3d2efa749db480ac33560b059');
context('when update after finalize', function () {
it('should throw error', function () {
expect(function () {
var hash = algorithm.update('any');
hash.hex();
hash.update('any');
}).to.throwError(/finalize already called/);
});
});
describe('more than 128 bytes', function () {
it('should be successful', function () {
expect(sha512_224('訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('f67e191a5d4ee67a272ccaf6cf597f0c4d6a0c46bd631be7cadb0944');
expect(sha512_224('訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一又譯雜湊演算法、摘要演算法等主流程式語言普遍已有MD5的實作。')).to.be('009c3d1e3172d6df71344982eada855421592aea28acbf660ada7569');
});
context('when large size', function () {
var hash = algorithm.create();
hash.bytes = 4294967295;
hash.update('any');
expect(hash.hBytes).to.be(1);
});
});
}
describe('special length', function () {
it('should be successful', function () {
expect(sha512_224('012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345')).to.be('36d6398ca22d885432e2fe3af53b9f83b2dc7a8e9284d65b81f7a879');
expect(sha512_224('0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456')).to.be('d930407a742d10f70747f05271181ad6c1c92f87482ce6a494cb566a');
expect(sha512_224('123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567')).to.be('1d60954ce7bee0bf5d235fe6bfc928c0f32c0f1c4992451dbc0f4dbf');
expect(sha512_224('1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678')).to.be('672aef84d579799cedf2fec090ce4ea0d72e0edd2788deaa8200fd67');
});
});
});
runTestCases('sha512', sha512);
runTestCases('sha384', sha384);
runTestCases('sha512_256', sha512_256);
runTestCases('sha512_224', sha512_224);
})(sha512, sha384, sha512_256, sha512_224);

@ -0,0 +1,24 @@
(function (Worker, WORKER, SOURCE) {
var cases = {
'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e': '',
'07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6': 'The quick brown fox jumps over the lazy dog',
'91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed': 'The quick brown fox jumps over the lazy dog.'
};
describe('#sha512', function () {
Object.keys(cases).forEach(function (hash) {
it('should be equal', function (done) {
var worker = new Worker(WORKER);
worker.onmessage = function(event) {
expect(event.data).to.be(hash);
if (worker.terminate) {
worker.terminate();
}
done();
};
worker.postMessage(SOURCE);
worker.postMessage(cases[hash]);
});
});
});
})(Worker, WORKER, SOURCE);

@ -0,0 +1,26 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SHA512</title>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/expect.js/index.js"></script>
<script src="../src/sha512.js"></script>
</head>
<body>
<div id="mocha"></div>
<script>
WORKER = 'worker.js';
SOURCE = '../src/sha512.js';
mocha.setup('bdd');
</script>
<script src="worker-test.js"></script>
<script>
mocha.checkLeaks();
mocha.run();
</script>
<script>
</script>
</body>
</html>

@ -0,0 +1,12 @@
var imported = false;
onmessage = function(e) {
if (imported) {
postMessage(sha512(e.data));
if (typeof exports !== 'undefined') {
imported = false;
}
} else {
imported = true;
importScripts(e.data);
}
}
Loading…
Cancel
Save