### Added

- TypeScript interfaces. #6, #9
- HMAC feature.
- support for web worker. #14

### Fixed
- deprecated `new Buffer`, replace with `Buffer.from`. #10
- dependencies and security issues.
- refactor: simplify formatMessage internal logic.
- Generates incorrect hash in some cases.

### Changed
- remove `eval` and use `require` directly. #8
- throw error by Error oject.
- throw error if update after finalize
- use unsigned right shift.
master v0.7.0
Yi-Cyuan Chen 2 years ago
parent 4818f116e4
commit 33acf62431

@ -1,5 +1,23 @@
# Change Log # Change Log
## v0.7.0 / 2024-01-24
### Added
- TypeScript interfaces. #6, #9
- HMAC feature.
- support for web worker. #14
### Fixed
- deprecated `new Buffer`, replace with `Buffer.from`. #10
- dependencies and security issues.
- refactor: simplify formatMessage internal logic.
- Generates incorrect hash in some cases.
### Changed
- remove `eval` and use `require` directly. #8
- throw error by Error oject.
- throw error if update after finalize
- use unsigned right shift.
## v0.6.0 / 2017-12-21 ## v0.6.0 / 2017-12-21
### Fixed ### Fixed
- incorrect result when first bit is 1 of bytes. - incorrect result when first bit is 1 of bytes.

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

@ -6,6 +6,7 @@ A simple SHA1 hash function for JavaScript supports UTF-8 encoding.
## Demo ## Demo
[SHA1 Online](http://emn178.github.io/online-tools/sha1.html) [SHA1 Online](http://emn178.github.io/online-tools/sha1.html)
[SHA1 File Checksum Online](https://emn178.github.io/online-tools/sha1_checksum.html)
## Download ## Download
[Compress](https://raw.github.com/emn178/js-sha1/master/build/sha1.min.js) [Compress](https://raw.github.com/emn178/js-sha1/master/build/sha1.min.js)
@ -20,6 +21,9 @@ For node.js, you can use this command to install:
npm install js-sha1 npm install js-sha1
## Notice
NIST formally deprecated use of SHA-1 in 2011 and disallowed its use for digital signatures in 2013, and declared that it should be phased out by 2030. However, SHA-1 is still secure for HMAC. [wiki](https://en.wikipedia.org/wiki/SHA-1)
## Usage ## Usage
You could use like this: You could use like this:
```JavaScript ```JavaScript
@ -27,11 +31,28 @@ sha1('Message to hash');
var hash = sha1.create(); var hash = sha1.create();
hash.update('Message to hash'); hash.update('Message to hash');
hash.hex(); hash.hex();
// HMAC
sha1.hmac('key', 'Message to hash');
var hash = sha1.hmac.create('key');
hash.update('Message to hash');
hash.hex();
``` ```
### Node.js
If you use node.js, you should require the module first: If you use node.js, you should require the module first:
```JavaScript ```JavaScript
sha1 = require('js-sha1'); var sha1 = require('js-sha1');
``` ```
### TypeScript
If you use TypeScript, you can import like this:
```TypeScript
import { sha1 } from 'js-sha1';
```
## RequireJS
It supports AMD: It supports AMD:
```JavaScript ```JavaScript
require(['your/path/sha1.js'], function(sha1) { require(['your/path/sha1.js'], function(sha1) {
@ -58,6 +79,11 @@ sha1.hex(''); // da39a3ee5e6b4b0d3255bfef95601890afd80709
sha1.array(''); // [218, 57, 163, 238, 94, 107, 75, 13, 50, 85, 191, 239, 149, 96, 24, 144, 175, 216, 7, 9] sha1.array(''); // [218, 57, 163, 238, 94, 107, 75, 13, 50, 85, 191, 239, 149, 96, 24, 144, 175, 216, 7, 9]
sha1.digest(''); // [218, 57, 163, 238, 94, 107, 75, 13, 50, 85, 191, 239, 149, 96, 24, 144, 175, 216, 7, 9] sha1.digest(''); // [218, 57, 163, 238, 94, 107, 75, 13, 50, 85, 191, 239, 149, 96, 24, 144, 175, 216, 7, 9]
sha1.arrayBuffer(''); // ArrayBuffer sha1.arrayBuffer(''); // ArrayBuffer
// HMAC
sha1.hmac.hex('key', 'Message to hash');
sha1.hmac.array('key', 'Message to hash');
// ...
``` ```
## License ## License

6
build/sha1.min.js vendored

File diff suppressed because one or more lines are too long

148
index.d.ts vendored

@ -0,0 +1,148 @@
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: Message, message: Message): string;
/**
* Create a hash object using a secret key.
*
* @param secretKey The Secret Key
*/
create(secretKey: Message): 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: Message, message: Message): Hasher;
/**
* Return hash in hex string.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
hex(secretKey: Message, message: Message): string;
/**
* Return hash in ArrayBuffer.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
arrayBuffer(secretKey: Message, message: Message): ArrayBuffer;
/**
* Return hash in integer array.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
digest(secretKey: Message, message: Message): number[];
/**
* Return hash in integer array.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
array(secretKey: Message, message: Message): number[];
}
interface Hash {
/**
* Hash and return hex string.
*
* @param message The message you want to hash.
*/
(message: Message): string;
/**
* 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;
/**
* Return hash in hex string.
*
* @param message The message you want to hash.
*/
hex(message: Message): string;
/**
* Return hash in ArrayBuffer.
*
* @param message The message you want to hash.
*/
arrayBuffer(message: Message): ArrayBuffer;
/**
* Return hash in integer array.
*
* @param message The message you want to hash.
*/
digest(message: Message): number[];
/**
* Return hash in integer array.
*
* @param message The message you want to hash.
*/
array(message: Message): number[];
/**
* HMAC interface
*/
hmac: Hmac;
}
export var sha1: Hash;

27
package-lock.json generated

@ -1,6 +1,6 @@
{ {
"name": "js-sha1", "name": "js-sha1",
"version": "0.6.0", "version": "0.7.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@ -881,6 +881,12 @@
"integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==", "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==",
"dev": true "dev": true
}, },
"esm": {
"version": "3.2.25",
"resolved": "https://registry.npmjs.org/esm/-/esm-3.2.25.tgz",
"integrity": "sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==",
"dev": true
},
"esprima": { "esprima": {
"version": "4.0.1", "version": "4.0.1",
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
@ -1737,9 +1743,9 @@
"dev": true "dev": true
}, },
"semver": { "semver": {
"version": "6.3.0", "version": "6.3.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz",
"integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==",
"dev": true "dev": true
}, },
"serialize-javascript": { "serialize-javascript": {
@ -1867,6 +1873,15 @@
} }
} }
}, },
"tiny-worker": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/tiny-worker/-/tiny-worker-2.3.0.tgz",
"integrity": "sha512-pJ70wq5EAqTAEl9IkGzA+fN0836rycEuz2Cn6yeZ6FRzlVS5IDOkFHpIoEsksPRQV34GDqXm65+OlnZqUSyK2g==",
"dev": true,
"requires": {
"esm": "^3.2.25"
}
},
"to-fast-properties": { "to-fast-properties": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-2.0.0.tgz", "resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-2.0.0.tgz",
@ -1903,8 +1918,8 @@
"integrity": "sha512-++1NO/zZIEdWf6cDIGceSJQPX31SqIpbVAHwFG5+240MtZqPG/NIPoinj8zlXQtAfMBqEt1Jyv2FiLP3n9gVhQ==", "integrity": "sha512-++1NO/zZIEdWf6cDIGceSJQPX31SqIpbVAHwFG5+240MtZqPG/NIPoinj8zlXQtAfMBqEt1Jyv2FiLP3n9gVhQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"commander": "2.12.2", "commander": "~2.12.1",
"source-map": "0.6.1" "source-map": "~0.6.1"
}, },
"dependencies": { "dependencies": {
"commander": { "commander": {

@ -1,6 +1,6 @@
{ {
"name": "js-sha1", "name": "js-sha1",
"version": "0.6.0", "version": "0.7.0",
"description": "A simple SHA1 hash function for JavaScript supports UTF-8 encoding.", "description": "A simple SHA1 hash function for JavaScript supports UTF-8 encoding.",
"main": "src/sha1.js", "main": "src/sha1.js",
"devDependencies": { "devDependencies": {
@ -8,6 +8,7 @@
"mocha": "~10.2.0", "mocha": "~10.2.0",
"nyc": "^15.1.0", "nyc": "^15.1.0",
"requirejs": "^2.1.22", "requirejs": "^2.1.22",
"tiny-worker": "^2.3.0",
"uglify-js": "^3.1.9" "uglify-js": "^3.1.9"
}, },
"scripts": { "scripts": {
@ -37,5 +38,9 @@
"exclude": [ "exclude": [
"tests" "tests"
] ]
},
"browser": {
"crypto": false,
"buffer": false
} }
} }

@ -1,22 +1,32 @@
/* /*
* [js-sha1]{@link https://github.com/emn178/js-sha1} * [js-sha1]{@link https://github.com/emn178/js-sha1}
* *
* @version 0.6.0 * @version 0.7.0
* @author Chen, Yi-Cyuan [emn178@gmail.com] * @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2017 * @copyright Chen, Yi-Cyuan 2014-2024
* @license MIT * @license MIT
*/ */
/*jslint bitwise: true */ /*jslint bitwise: true */
(function() { (function() {
'use strict'; 'use strict';
var root = typeof window === 'object' ? window : {}; var INPUT_ERROR = 'input is invalid type';
var FINALIZE_ERROR = 'finalize already called';
var WINDOW = typeof window === 'object';
var root = WINDOW ? window : {};
if (root.JS_SHA1_NO_WINDOW) {
WINDOW = false;
}
var WEB_WORKER = !WINDOW && typeof self === 'object';
var NODE_JS = !root.JS_SHA1_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node; var NODE_JS = !root.JS_SHA1_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
if (NODE_JS) { if (NODE_JS) {
root = global; root = global;
} else if (WEB_WORKER) {
root = self;
} }
var COMMON_JS = !root.JS_SHA1_NO_COMMON_JS && typeof module === 'object' && module.exports; var COMMON_JS = !root.JS_SHA1_NO_COMMON_JS && typeof module === 'object' && module.exports;
var AMD = typeof define === 'function' && define.amd; var AMD = typeof define === 'function' && define.amd;
var ARRAY_BUFFER = !root.JS_SHA1_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
var HEX_CHARS = '0123456789abcdef'.split(''); var HEX_CHARS = '0123456789abcdef'.split('');
var EXTRA = [-2147483648, 8388608, 32768, 128]; var EXTRA = [-2147483648, 8388608, 32768, 128];
var SHIFT = [24, 16, 8, 0]; var SHIFT = [24, 16, 8, 0];
@ -24,6 +34,38 @@
var blocks = []; var blocks = [];
var isArray = Array.isArray;
if (root.JS_SHA1_NO_NODE_JS || !isArray) {
isArray = function (obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
};
}
var isView = ArrayBuffer.isView;
if (ARRAY_BUFFER && (root.JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW || !isView)) {
isView = function (obj) {
return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
};
}
// [message: string, isString: bool]
var formatMessage = function (message) {
var type = typeof message;
if (type === 'string') {
return [message, true];
}
if (type !== 'object' || message === null) {
throw new Error(INPUT_ERROR);
}
if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
}
if (!isArray(message) && !isView(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false];
}
var createOutputMethod = function (outputType) { var createOutputMethod = function (outputType) {
return function (message) { return function (message) {
return new Sha1(true).update(message)[outputType](); return new Sha1(true).update(message)[outputType]();
@ -49,21 +91,57 @@
}; };
var nodeWrap = function (method) { var nodeWrap = function (method) {
var crypto = eval("require('crypto')"); var crypto = require('crypto')
var Buffer = eval("require('buffer').Buffer"); var Buffer = require('buffer').Buffer;
var bufferFrom;
if (Buffer.from && !root.JS_SHA1_NO_BUFFER_FROM) {
bufferFrom = Buffer.from;
} else {
bufferFrom = function (message) {
return new Buffer(message);
};
}
var nodeMethod = function (message) { var nodeMethod = function (message) {
if (typeof message === 'string') { if (typeof message === 'string') {
return crypto.createHash('sha1').update(message, 'utf8').digest('hex'); return crypto.createHash('sha1').update(message, 'utf8').digest('hex');
} else if (message.constructor === ArrayBuffer) { } else {
message = new Uint8Array(message); if (message === null || message === undefined) {
} else if (message.length === undefined) { throw new Error(INPUT_ERROR);
} else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
}
}
if (isArray(message) || isView(message) ||
message.constructor === Buffer) {
return crypto.createHash('sha1').update(bufferFrom(message)).digest('hex');
} else {
return method(message); return method(message);
} }
return crypto.createHash('sha1').update(new Buffer(message)).digest('hex');
}; };
return nodeMethod; return nodeMethod;
}; };
var createHmacOutputMethod = function (outputType) {
return function (key, message) {
return new HmacSha1(key, true).update(message)[outputType]();
};
};
var createHmacMethod = function () {
var method = createHmacOutputMethod('hex');
method.create = function (key) {
return new HmacSha1(key);
};
method.update = function (key, message) {
return method.create(key).update(message);
};
for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
var type = OUTPUT_TYPES[i];
method[type] = createHmacOutputMethod(type);
}
return method;
};
function Sha1(sharedMemory) { function Sha1(sharedMemory) {
if (sharedMemory) { if (sharedMemory) {
blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] = blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
@ -88,48 +166,48 @@
Sha1.prototype.update = function (message) { Sha1.prototype.update = function (message) {
if (this.finalized) { if (this.finalized) {
return; throw new Error(FINALIZE_ERROR);
}
var notString = typeof(message) !== 'string';
if (notString && message.constructor === root.ArrayBuffer) {
message = new Uint8Array(message);
} }
var result = formatMessage(message);
message = result[0];
var isString = result[1];
var code, index = 0, i, length = message.length || 0, blocks = this.blocks; var code, index = 0, i, length = message.length || 0, blocks = this.blocks;
while (index < length) { while (index < length) {
if (this.hashed) { if (this.hashed) {
this.hashed = false; this.hashed = false;
blocks[0] = this.block; blocks[0] = this.block;
blocks[16] = blocks[1] = blocks[2] = blocks[3] = this.block = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
blocks[4] = blocks[5] = blocks[6] = blocks[7] = blocks[4] = blocks[5] = blocks[6] = blocks[7] =
blocks[8] = blocks[9] = blocks[10] = blocks[11] = blocks[8] = blocks[9] = blocks[10] = blocks[11] =
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
} }
if(notString) { if(isString) {
for (i = this.start; index < length && i < 64; ++index) {
blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
}
} else {
for (i = this.start; index < length && i < 64; ++index) { for (i = this.start; index < length && i < 64; ++index) {
code = message.charCodeAt(index); code = message.charCodeAt(index);
if (code < 0x80) { if (code < 0x80) {
blocks[i >> 2] |= code << SHIFT[i++ & 3]; blocks[i >>> 2] |= code << SHIFT[i++ & 3];
} else if (code < 0x800) { } else if (code < 0x800) {
blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; blocks[i >>> 2] |= (0xc0 | (code >>> 6)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else if (code < 0xd800 || code >= 0xe000) { } else if (code < 0xd800 || code >= 0xe000) {
blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; blocks[i >>> 2] |= (0xe0 | (code >>> 12)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else { } else {
code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; blocks[i >>> 2] |= (0xf0 | (code >>> 18)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; blocks[i >>> 2] |= (0x80 | ((code >>> 12) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} }
} }
} else {
for (i = this.start; index < length && i < 64; ++index) {
blocks[i >>> 2] |= message[index] << SHIFT[i++ & 3];
}
} }
this.lastByteIndex = i; this.lastByteIndex = i;
@ -157,7 +235,7 @@
this.finalized = true; this.finalized = true;
var blocks = this.blocks, i = this.lastByteIndex; var blocks = this.blocks, i = this.lastByteIndex;
blocks[16] = this.block; blocks[16] = this.block;
blocks[i >> 2] |= EXTRA[i & 3]; blocks[i >>> 2] |= EXTRA[i & 3];
this.block = blocks[16]; this.block = blocks[16];
if (i >= 56) { if (i >= 56) {
if (!this.hashed) { if (!this.hashed) {
@ -303,26 +381,26 @@
var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4; var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4;
return HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] + return HEX_CHARS[(h0 >>> 28) & 0x0F] + HEX_CHARS[(h0 >>> 24) & 0x0F] +
HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] + HEX_CHARS[(h0 >>> 20) & 0x0F] + HEX_CHARS[(h0 >>> 16) & 0x0F] +
HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] + HEX_CHARS[(h0 >>> 12) & 0x0F] + HEX_CHARS[(h0 >>> 8) & 0x0F] +
HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] + HEX_CHARS[(h0 >>> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] + HEX_CHARS[(h1 >>> 28) & 0x0F] + HEX_CHARS[(h1 >>> 24) & 0x0F] +
HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] + HEX_CHARS[(h1 >>> 20) & 0x0F] + HEX_CHARS[(h1 >>> 16) & 0x0F] +
HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] + HEX_CHARS[(h1 >>> 12) & 0x0F] + HEX_CHARS[(h1 >>> 8) & 0x0F] +
HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] + HEX_CHARS[(h1 >>> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] + HEX_CHARS[(h2 >>> 28) & 0x0F] + HEX_CHARS[(h2 >>> 24) & 0x0F] +
HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] + HEX_CHARS[(h2 >>> 20) & 0x0F] + HEX_CHARS[(h2 >>> 16) & 0x0F] +
HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] + HEX_CHARS[(h2 >>> 12) & 0x0F] + HEX_CHARS[(h2 >>> 8) & 0x0F] +
HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] + HEX_CHARS[(h2 >>> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] + HEX_CHARS[(h3 >>> 28) & 0x0F] + HEX_CHARS[(h3 >>> 24) & 0x0F] +
HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] + HEX_CHARS[(h3 >>> 20) & 0x0F] + HEX_CHARS[(h3 >>> 16) & 0x0F] +
HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] + HEX_CHARS[(h3 >>> 12) & 0x0F] + HEX_CHARS[(h3 >>> 8) & 0x0F] +
HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] + HEX_CHARS[(h3 >>> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] + HEX_CHARS[(h4 >>> 28) & 0x0F] + HEX_CHARS[(h4 >>> 24) & 0x0F] +
HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] + HEX_CHARS[(h4 >>> 20) & 0x0F] + HEX_CHARS[(h4 >>> 16) & 0x0F] +
HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] + HEX_CHARS[(h4 >>> 12) & 0x0F] + HEX_CHARS[(h4 >>> 8) & 0x0F] +
HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F]; HEX_CHARS[(h4 >>> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F];
}; };
Sha1.prototype.toString = Sha1.prototype.hex; Sha1.prototype.toString = Sha1.prototype.hex;
@ -333,11 +411,11 @@
var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4; var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4;
return [ return [
(h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF, (h0 >>> 24) & 0xFF, (h0 >>> 16) & 0xFF, (h0 >>> 8) & 0xFF, h0 & 0xFF,
(h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF, (h1 >>> 24) & 0xFF, (h1 >>> 16) & 0xFF, (h1 >>> 8) & 0xFF, h1 & 0xFF,
(h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF, (h2 >>> 24) & 0xFF, (h2 >>> 16) & 0xFF, (h2 >>> 8) & 0xFF, h2 & 0xFF,
(h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF, (h3 >>> 24) & 0xFF, (h3 >>> 16) & 0xFF, (h3 >>> 8) & 0xFF, h3 & 0xFF,
(h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF (h4 >>> 24) & 0xFF, (h4 >>> 16) & 0xFF, (h4 >>> 8) & 0xFF, h4 & 0xFF
]; ];
}; };
@ -356,7 +434,68 @@
return buffer; return buffer;
}; };
function HmacSha1(key, sharedMemory) {
var i, result = formatMessage(key);
key = result[0];
if (result[1]) {
var bytes = [], length = key.length, index = 0, code;
for (i = 0; i < length; ++i) {
code = key.charCodeAt(i);
if (code < 0x80) {
bytes[index++] = code;
} else if (code < 0x800) {
bytes[index++] = (0xc0 | (code >>> 6));
bytes[index++] = (0x80 | (code & 0x3f));
} else if (code < 0xd800 || code >= 0xe000) {
bytes[index++] = (0xe0 | (code >>> 12));
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f));
} else {
code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
bytes[index++] = (0xf0 | (code >>> 18));
bytes[index++] = (0x80 | ((code >>> 12) & 0x3f));
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f));
}
}
key = bytes;
}
if (key.length > 64) {
key = (new Sha1(true)).update(key).array();
}
var oKeyPad = [], iKeyPad = [];
for (i = 0; i < 64; ++i) {
var b = key[i] || 0;
oKeyPad[i] = 0x5c ^ b;
iKeyPad[i] = 0x36 ^ b;
}
Sha1.call(this, sharedMemory);
this.update(iKeyPad);
this.oKeyPad = oKeyPad;
this.inner = true;
this.sharedMemory = sharedMemory;
}
HmacSha1.prototype = new Sha1();
HmacSha1.prototype.finalize = function () {
Sha1.prototype.finalize.call(this);
if (this.inner) {
this.inner = false;
var innerHash = this.array();
Sha1.call(this, this.sharedMemory);
this.update(this.oKeyPad);
this.update(innerHash);
Sha1.prototype.finalize.call(this);
}
};
var exports = createMethod(); var exports = createMethod();
exports.sha1 = exports;
exports.sha1.hmac = createHmacMethod();
if (COMMON_JS) { if (COMMON_JS) {
module.exports = exports; module.exports = exports;

@ -0,0 +1,202 @@
(function (sha1) {
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 = {
sha1_hmac: {
'Test Vectors': {
'b617318655057264e28bc0b6fb378c8ef146be00': [
[0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b],
'Hi There'
],
'effcdf6ae5eb2fa2d27416d5f184df9c259a7c79': [
'Jefe',
'what do ya want for nothing?'
],
'125d7342b9ac11cd91a39af48aa17b4f63f175d3': [
[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]
],
'4c9007f4026250c6bc8414f9bf50c86c2d7235da': [
[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]
],
'4c1a03424b55e07fe7f27be1d58bb9324a9a5a04': [
[0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c],
'Test With Truncation'
],
'aa4ae5e15272d00e95705637ce8a3b55ed402112': [
[0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 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'
],
'e8e99d0f45237d786d6bbaa7965c7808bbff1a91': [
[0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 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 and Larger Than One Block-Size Data'
]
},
'UTF8': {
'f495de6d0f1b5681070a024bbaed5b5f42847306': ['中文', '中文'],
'58891d68487ffebddba5925abedec77a5a578db2': ['aécio', 'aécio'],
'a1816bff2dae324c283aeab564d5edb5170fbada': ['𠜎', '𠜎']
}
}
};
if (!(typeof JS_SHA1_NO_ARRAY_BUFFER === 'boolean' && JS_SHA1_NO_ARRAY_BUFFER)) {
testCases.sha1_hmac.Uint8Array = {
'69536cc84eee5fe51c5b051aff8485f5c9ef0b58': [
new Uint8Array(0),
'Hi There'
]
};
testCases.sha1_hmac.ArrayBuffer = {
'69536cc84eee5fe51c5b051aff8485f5c9ef0b58': [
new ArrayBuffer(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();
return hash.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);
}
});
});
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('sha1_hmac', sha1.hmac);
})(sha1);

@ -1,32 +1,63 @@
// Node.js env
expect = require('expect.js'); expect = require('expect.js');
sha1 = require('../src/sha1.js'); Worker = require("tiny-worker");
require('./test.js');
function unset() {
delete require.cache[require.resolve('../src/sha1.js')];
delete require.cache[require.resolve('./test.js')];
delete require.cache[require.resolve('./hmac-test.js')];
sha1 = null;
BUFFER = undefined;
JS_SHA1_NO_WINDOW = undefined;
JS_SHA1_NO_NODE_JS = undefined;
JS_SHA1_NO_COMMON_JS = undefined;
JS_SHA1_NO_ARRAY_BUFFER = undefined;
JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW = undefined;
window = undefined;
}
function runCommonJsTest() {
sha1 = require('../src/sha1.js');
require('./test.js');
require('./hmac-test.js');
unset();
}
function runWindowTest() {
window = global;
require('../src/sha1.js');
require('./test.js');
require('./hmac-test.js');
unset();
}
// Node.js env
BUFFER = true;
runCommonJsTest();
delete require.cache[require.resolve('../src/sha1.js')]; // Node.js env, no Buffer.from
delete require.cache[require.resolve('./test.js')]; JS_SHA1_NO_BUFFER_FROM = true
sha1 = null; runCommonJsTest();
// Webpack browser env // Webpack browser env
JS_SHA1_NO_NODE_JS = true; JS_SHA1_NO_NODE_JS = true;
window = global; runCommonJsTest();
sha1 = require('../src/sha1.js');
require('./test.js');
delete require.cache[require.resolve('../src/sha1.js')];
delete require.cache[require.resolve('./test.js')];
sha1 = null;
// browser env // browser env
JS_SHA1_NO_NODE_JS = true; JS_SHA1_NO_NODE_JS = true;
JS_SHA1_NO_COMMON_JS = true; JS_SHA1_NO_COMMON_JS = true;
window = global; runWindowTest();
require('../src/sha1.js');
require('./test.js');
delete require.cache[require.resolve('../src/sha1.js')]; // browser env and no array buffer
delete require.cache[require.resolve('./test.js')]; JS_SHA1_NO_NODE_JS = true;
sha1 = null; JS_SHA1_NO_COMMON_JS = true;
JS_SHA1_NO_ARRAY_BUFFER = true;
runWindowTest();
// browser env and no isView
JS_SHA1_NO_NODE_JS = true;
JS_SHA1_NO_COMMON_JS = true;
JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW = true;
runWindowTest();
// browser AMD // browser AMD
JS_SHA1_NO_NODE_JS = true; JS_SHA1_NO_NODE_JS = true;
@ -35,7 +66,93 @@ window = global;
define = function (func) { define = function (func) {
sha1 = func(); sha1 = func();
require('./test.js'); require('./test.js');
require('./hmac-test.js');
}; };
define.amd = true; define.amd = true;
require('../src/sha1.js'); require('../src/sha1.js');
unset();
// webworker
WORKER = 'tests/worker.js';
SOURCE = 'src/sha1.js';
require('./worker-test.js');
delete require.cache[require.resolve('./worker-test.js')];
// cover webworker
JS_SHA1_NO_WINDOW = true;
JS_SHA1_NO_NODE_JS = true;
WORKER = './worker.js';
SOURCE = '../src/sha1.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 () {};
sha1 = require('../src/sha1.js');
require('./worker-test.js');
// sha1 = require('../src/sha1.js');
// require('./test.js');
// delete require.cache[require.resolve('../src/sha1.js')];
// delete require.cache[require.resolve('./test.js')];
// delete require.cache[require.resolve('./hmac-test.js')];
// sha1 = null;
// // Webpack browser env
// JS_SHA1_NO_NODE_JS = true;
// window = global;
// sha1 = require('../src/sha1.js');
// require('./test.js');
// delete require.cache[require.resolve('../src/sha1.js')];
// delete require.cache[require.resolve('./test.js')];
// delete require.cache[require.resolve('./hmac-test.js')];
// sha1 = null;
// // browser env
// JS_SHA1_NO_NODE_JS = true;
// JS_SHA1_NO_COMMON_JS = true;
// window = global;
// require('../src/sha1.js');
// require('./test.js');
// delete require.cache[require.resolve('../src/sha1.js')];
// delete require.cache[require.resolve('./test.js')];
// delete require.cache[require.resolve('./hmac-test.js')];
// sha1 = null;
// // browser AMD
// JS_SHA1_NO_NODE_JS = true;
// JS_SHA1_NO_COMMON_JS = true;
// window = global;
// define = function (func) {
// sha1 = func();
// require('./test.js');
// require('./hmac-test.js');
// };
// define.amd = true;
// require('../src/sha1.js');

@ -0,0 +1,24 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SHA1</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/sha1.js'], function (sha1) {
window.sha1 = sha1;
require(['test.js', 'hmac-test.js'], function () {
mocha.checkLeaks();
mocha.run();
});
});
</script>
</body>
</html>

@ -21,7 +21,8 @@
'UTF8': { 'UTF8': {
'7be2d2d20c106eee0836c9bc2b939890a78e8fb3': '中文', '7be2d2d20c106eee0836c9bc2b939890a78e8fb3': '中文',
'9e4e5d978deced901d621475b03f1ded19e945bf': 'aécio', '9e4e5d978deced901d621475b03f1ded19e945bf': 'aécio',
'4667688a63420661469c8dbc0f871770349bab08': '𠜎' '4667688a63420661469c8dbc0f871770349bab08': '𠜎',
'20cc8fef058da244b90ed814ab1b6d9d5b4796bf': '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.'
}, },
'UTF8 more than 64 bytes': { 'UTF8 more than 64 bytes': {
'ad8aae581c915fe01c4964a5e8b322cae74ee5c5': '訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一', 'ad8aae581c915fe01c4964a5e8b322cae74ee5c5': '訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一',
@ -38,28 +39,30 @@
'da39a3ee5e6b4b0d3255bfef95601890afd80709': [], 'da39a3ee5e6b4b0d3255bfef95601890afd80709': [],
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': [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], '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': [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],
'55a13698cdc010c0d16dab2f7dc10f43a713f12f': [48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55] '55a13698cdc010c0d16dab2f7dc10f43a713f12f': [48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55]
}, }
'Uint8Array': { };
if (!(typeof JS_SHA1_NO_ARRAY_BUFFER === 'boolean' && JS_SHA1_NO_ARRAY_BUFFER)) {
testCases['Uint8Array'] = {
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': 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]) '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': 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])
}, };
'Int8Array': { testCases['Int8Array'] = {
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': 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]) '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': 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])
}, };
'ArrayBuffer': { testCases['ArrayBuffer'] = {
'5ba93c9db0cff93f52b521d7420e43f6eda2784f': new ArrayBuffer(1) '5ba93c9db0cff93f52b521d7420e43f6eda2784f': new ArrayBuffer(1)
}, };
'Object': { }
'da39a3ee5e6b4b0d3255bfef95601890afd80709': {what: 'ever'}
}
};
if (typeof process == 'object') { if (typeof BUFFER === 'boolean' && BUFFER) {
testCases['Buffer'] = { testCases['Buffer'] = {
'da39a3ee5e6b4b0d3255bfef95601890afd80709': new Buffer(0), 'da39a3ee5e6b4b0d3255bfef95601890afd80709': Buffer.from([]),
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': 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])) '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': Buffer.from(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]))
}; }
} }
var errorTestCases = [null, undefined, { length: 0 }, 0, 1, false, true, NaN, Infinity, function () {}];
var methods = [ var methods = [
{ {
name: 'sha1', name: 'sha1',
@ -131,32 +134,31 @@
call: function (message) { call: function (message) {
var hash = sha1.update(message); var hash = sha1.update(message);
hash.hex(); hash.hex();
hash.update(message);
return hash.hex(); return hash.hex();
} }
} }
]; ];
methods.forEach(function (method) { describe('sha1', function () {
describe('#' + method.name, function () { methods.forEach(function (method) {
for (var testCaseName in testCases) { describe('#' + method.name, function () {
(function (testCaseName) { for (var testCaseName in testCases) {
var testCase = testCases[testCaseName]; (function (testCaseName) {
context('when ' + testCaseName, function () { var testCase = testCases[testCaseName];
for (var hash in testCase) { context('when ' + testCaseName, function () {
(function (message, hash) { for (var hash in testCase) {
it('should be equal', function () { (function (message, hash) {
expect(method.call(message)).to.be(hash); it('should be equal', function () {
}); expect(method.call(message)).to.be(hash);
})(testCase[hash], hash); });
} })(testCase[hash], hash);
}); }
})(testCaseName); });
} })(testCaseName);
}
});
}); });
});
describe('Sha1', function () {
classMethods.forEach(function (method) { classMethods.forEach(function (method) {
describe('#' + method.name, function () { describe('#' + method.name, function () {
for (var testCaseName in testCases) { for (var testCaseName in testCases) {
@ -175,6 +177,28 @@
} }
}); });
}); });
});
describe('#sha1', function () {
errorTestCases.forEach(function (testCase) {
context('when ' + testCase, function () {
it('should throw error', function () {
expect(function () {
sha1(testCase);
}).to.throwError(/input is invalid type/);
});
});
});
context('when update after finalize', function () {
it('should throw error', function () {
expect(function () {
var hash = sha1.update('any');
hash.hex();
hash.update('any');
}).to.throwError(/finalize already called/);
});
});
context('when large size', function () { context('when large size', function () {
var hash = sha1.create(); var hash = sha1.create();

@ -0,0 +1,24 @@
(function (Worker, WORKER, SOURCE) {
var cases = {
'da39a3ee5e6b4b0d3255bfef95601890afd80709': '',
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': 'The quick brown fox jumps over the lazy dog',
'408d94384216f890ff7a0c3528e8bed1e0b01621': 'The quick brown fox jumps over the lazy dog.'
};
describe('#sha1', 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,25 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SHA1</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/sha1.js"></script>
</head>
<body>
<div id="mocha"></div>
<script>
WORKER = 'worker.js';
SOURCE = '../src/sha1.js';
mocha.setup('bdd');
</script>
<script src="worker-test.js"></script>
<script>
mocha.run();
</script>
<script>
</script>
</body>
</html>

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