Compare commits
14 Commits
Author | SHA1 | Date |
---|---|---|
![]() |
5c5ec87644 | 2 years ago |
![]() |
33acf62431 | 2 years ago |
![]() |
4818f116e4 | 2 years ago |
![]() |
5069f39a36 | 2 years ago |
![]() |
474c6457dc | 2 years ago |
![]() |
f0cbf4ba45 | 2 years ago |
![]() |
65b3898c4e | 8 years ago |
![]() |
4ee5af05d3 | 8 years ago |
![]() |
c724643de0 | 9 years ago |
![]() |
80d540792b | 9 years ago |
![]() |
731295e355 | 11 years ago |
![]() |
2f22f6285d | 11 years ago |
![]() |
72f01d0043 | 11 years ago |
![]() |
4122482ac4 | 11 years ago |
@ -0,0 +1,2 @@
|
|||||||
|
/node_modules/
|
||||||
|
/tests/
|
@ -1 +1,3 @@
|
|||||||
node_modules/
|
/node_modules/
|
||||||
|
/coverage/
|
||||||
|
/.nyc_output/
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
/node_modules/
|
||||||
|
/coverage/
|
||||||
|
/.nyc_output/
|
||||||
|
/tests/
|
||||||
|
.covignore
|
||||||
|
.travis.yml
|
||||||
|
.npmignore
|
||||||
|
bower.json
|
@ -1,12 +1,10 @@
|
|||||||
language: node_js
|
language: node_js
|
||||||
node_js:
|
node_js:
|
||||||
- "0.11"
|
- "6.11.4"
|
||||||
- "0.10"
|
- "8.6.0"
|
||||||
- "0.8"
|
before_install:
|
||||||
install:
|
- npm install coveralls
|
||||||
- npm install mocha -g
|
after_success: npm run coveralls
|
||||||
- npm install coveralls -g
|
branches:
|
||||||
- npm install mocha-lcov-reporter -g
|
only:
|
||||||
- npm install
|
- master
|
||||||
script:
|
|
||||||
- npm run-script coveralls
|
|
||||||
|
@ -1,18 +1,76 @@
|
|||||||
# v0.1.3 / 2015-01-07
|
# Change Log
|
||||||
|
|
||||||
* Add bower package.
|
## v0.7.0 / 2024-01-24
|
||||||
* Fixed JSHint warnings.
|
### Added
|
||||||
* Add travis.
|
- TypeScript interfaces. #6, #9
|
||||||
* Add coveralls.
|
- HMAC feature.
|
||||||
|
- support for web worker. #14
|
||||||
|
|
||||||
# v0.1.2 / 2014-07-27
|
### Fixed
|
||||||
|
- deprecated `new Buffer`, replace with `Buffer.from`. #10
|
||||||
|
- dependencies and security issues.
|
||||||
|
- refactor: simplify formatMessage internal logic.
|
||||||
|
- Generates incorrect hash in some cases.
|
||||||
|
|
||||||
Fixed accents bug
|
### Changed
|
||||||
|
- remove `eval` and use `require` directly. #8
|
||||||
|
- throw error by Error oject.
|
||||||
|
- throw error if update after finalize
|
||||||
|
- use unsigned right shift.
|
||||||
|
|
||||||
# v0.1.1 / 2014-01-05
|
## v0.6.0 / 2017-12-21
|
||||||
|
### Fixed
|
||||||
|
- incorrect result when first bit is 1 of bytes.
|
||||||
|
|
||||||
Update license
|
### Changed
|
||||||
|
- prevent webpack to require dependencies.
|
||||||
|
|
||||||
# v0.1.0 / 2014-01-04
|
## v0.5.0 / 2017-10-31
|
||||||
|
### Fixed
|
||||||
|
- incorrect result when file size >= 512M.
|
||||||
|
|
||||||
Initial release
|
## v0.4.1 / 2016-12-30
|
||||||
|
### Fixed
|
||||||
|
- ArrayBuffer dosen't work in Webpack.
|
||||||
|
|
||||||
|
## v0.4.0 / 2016-12-05
|
||||||
|
### Added
|
||||||
|
- update method.
|
||||||
|
- support Array output.
|
||||||
|
- support ArrayBuffer output.
|
||||||
|
- support AMD.
|
||||||
|
|
||||||
|
## v0.3.0 / 2015-03-04
|
||||||
|
### Added
|
||||||
|
- support ArrayBuffer input.
|
||||||
|
### Changed
|
||||||
|
- improve performance.
|
||||||
|
|
||||||
|
## v0.2.0 / 2015-02-16
|
||||||
|
### Added
|
||||||
|
- support byte array input.
|
||||||
|
- test cases.
|
||||||
|
### Removed
|
||||||
|
- ascii parameter.
|
||||||
|
### Changed
|
||||||
|
- improve performance.
|
||||||
|
|
||||||
|
## v0.1.3 / 2015-01-07
|
||||||
|
### Added
|
||||||
|
- bower package.
|
||||||
|
- travis.
|
||||||
|
- coveralls.
|
||||||
|
### Fixed
|
||||||
|
- JSHint warnings.
|
||||||
|
|
||||||
|
## v0.1.2 / 2014-07-27
|
||||||
|
### Fixed
|
||||||
|
- accents bug.
|
||||||
|
|
||||||
|
## v0.1.1 / 2014-01-05
|
||||||
|
### Changed
|
||||||
|
- update license.
|
||||||
|
|
||||||
|
## v0.1.0 / 2014-01-04
|
||||||
|
### Added
|
||||||
|
- initial release.
|
||||||
|
File diff suppressed because one or more lines are too long
@ -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;
|
File diff suppressed because it is too large
Load Diff
@ -1,173 +1,510 @@
|
|||||||
/*
|
/*
|
||||||
* js-sha1 v0.1.3
|
* [js-sha1]{@link https://github.com/emn178/js-sha1}
|
||||||
* https://github.com/emn178/js-sha1
|
|
||||||
*
|
*
|
||||||
* Copyright 2014-2015, emn178@gmail.com
|
* @version 0.7.0
|
||||||
*
|
* @author Chen, Yi-Cyuan [emn178@gmail.com]
|
||||||
* Licensed under the MIT license:
|
* @copyright Chen, Yi-Cyuan 2014-2024
|
||||||
* http://www.opensource.org/licenses/MIT
|
* @license MIT
|
||||||
*/
|
*/
|
||||||
;(function(root, undefined){
|
/*jslint bitwise: true */
|
||||||
|
(function() {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
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;
|
||||||
|
if (NODE_JS) {
|
||||||
|
root = global;
|
||||||
|
} else if (WEB_WORKER) {
|
||||||
|
root = self;
|
||||||
|
}
|
||||||
|
var COMMON_JS = !root.JS_SHA1_NO_COMMON_JS && typeof module === 'object' && module.exports;
|
||||||
|
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 SHIFT = [24, 16, 8, 0];
|
||||||
|
var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer'];
|
||||||
|
|
||||||
|
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) {
|
||||||
|
return function (message) {
|
||||||
|
return new Sha1(true).update(message)[outputType]();
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
var createMethod = function () {
|
||||||
|
var method = createOutputMethod('hex');
|
||||||
|
if (NODE_JS) {
|
||||||
|
method = nodeWrap(method);
|
||||||
|
}
|
||||||
|
method.create = function () {
|
||||||
|
return new Sha1();
|
||||||
|
};
|
||||||
|
method.update = function (message) {
|
||||||
|
return method.create().update(message);
|
||||||
|
};
|
||||||
|
for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
|
||||||
|
var type = OUTPUT_TYPES[i];
|
||||||
|
method[type] = createOutputMethod(type);
|
||||||
|
}
|
||||||
|
return method;
|
||||||
|
};
|
||||||
|
|
||||||
|
var nodeWrap = function (method) {
|
||||||
|
var crypto = require('crypto')
|
||||||
|
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) {
|
||||||
|
if (typeof message === 'string') {
|
||||||
|
return crypto.createHash('sha1').update(message, 'utf8').digest('hex');
|
||||||
|
} else {
|
||||||
|
if (message === null || message === 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 nodeMethod;
|
||||||
|
};
|
||||||
|
|
||||||
var sha1 = function(message, asciiOnly) {
|
var createHmacOutputMethod = function (outputType) {
|
||||||
var blocks, h0, h1, h2, h3, h4;
|
return function (key, message) {
|
||||||
if(!asciiOnly && /[^\x00-\x7F]/.test(message)) {
|
return new HmacSha1(key, true).update(message)[outputType]();
|
||||||
blocks = getBlocksFromUtf8(message);
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
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) {
|
||||||
|
if (sharedMemory) {
|
||||||
|
blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
|
||||||
|
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
|
||||||
|
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
|
||||||
|
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
|
||||||
|
this.blocks = blocks;
|
||||||
} else {
|
} else {
|
||||||
blocks = getBlocksFromAscii(message);
|
this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
||||||
}
|
}
|
||||||
h0 = 0x67452301;
|
|
||||||
h1 = 0xEFCDAB89;
|
|
||||||
h2 = 0x98BADCFE;
|
|
||||||
h3 = 0x10325476;
|
|
||||||
h4 = 0xC3D2E1F0;
|
|
||||||
|
|
||||||
for(var i = 0, length = blocks.length;i < length;i += 16) {
|
this.h0 = 0x67452301;
|
||||||
var w = [], j;
|
this.h1 = 0xEFCDAB89;
|
||||||
for(j = 0;j < 16;++j) {
|
this.h2 = 0x98BADCFE;
|
||||||
w[j] = blocks[i + j];
|
this.h3 = 0x10325476;
|
||||||
|
this.h4 = 0xC3D2E1F0;
|
||||||
|
|
||||||
|
this.block = this.start = this.bytes = this.hBytes = 0;
|
||||||
|
this.finalized = this.hashed = false;
|
||||||
|
this.first = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Sha1.prototype.update = function (message) {
|
||||||
|
if (this.finalized) {
|
||||||
|
throw new Error(FINALIZE_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = formatMessage(message);
|
||||||
|
message = result[0];
|
||||||
|
var isString = result[1];
|
||||||
|
var code, index = 0, i, length = message.length || 0, blocks = this.blocks;
|
||||||
|
|
||||||
|
while (index < length) {
|
||||||
|
if (this.hashed) {
|
||||||
|
this.hashed = false;
|
||||||
|
blocks[0] = this.block;
|
||||||
|
this.block = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
|
||||||
|
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
|
||||||
|
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
|
||||||
|
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isString) {
|
||||||
|
for (i = this.start; index < length && i < 64; ++index) {
|
||||||
|
code = message.charCodeAt(index);
|
||||||
|
if (code < 0x80) {
|
||||||
|
blocks[i >>> 2] |= code << SHIFT[i++ & 3];
|
||||||
|
} else if (code < 0x800) {
|
||||||
|
blocks[i >>> 2] |= (0xc0 | (code >>> 6)) << SHIFT[i++ & 3];
|
||||||
|
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
|
||||||
|
} else if (code < 0xd800 || code >= 0xe000) {
|
||||||
|
blocks[i >>> 2] |= (0xe0 | (code >>> 12)) << SHIFT[i++ & 3];
|
||||||
|
blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
|
||||||
|
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
|
||||||
|
} else {
|
||||||
|
code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
|
||||||
|
blocks[i >>> 2] |= (0xf0 | (code >>> 18)) << SHIFT[i++ & 3];
|
||||||
|
blocks[i >>> 2] |= (0x80 | ((code >>> 12) & 0x3f)) << SHIFT[i++ & 3];
|
||||||
|
blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
|
||||||
|
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
|
||||||
}
|
}
|
||||||
for(j = 16;j < 80;++j) {
|
|
||||||
var x = w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16];
|
|
||||||
w[j] = leftrotate(x, 1);
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
for (i = this.start; index < length && i < 64; ++index) {
|
||||||
|
blocks[i >>> 2] |= message[index] << SHIFT[i++ & 3];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.lastByteIndex = i;
|
||||||
|
this.bytes += i - this.start;
|
||||||
|
if (i >= 64) {
|
||||||
|
this.block = blocks[16];
|
||||||
|
this.start = i - 64;
|
||||||
|
this.hash();
|
||||||
|
this.hashed = true;
|
||||||
|
} else {
|
||||||
|
this.start = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (this.bytes > 4294967295) {
|
||||||
|
this.hBytes += this.bytes / 4294967296 << 0;
|
||||||
|
this.bytes = this.bytes % 4294967296;
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
Sha1.prototype.finalize = function () {
|
||||||
|
if (this.finalized) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.finalized = true;
|
||||||
|
var blocks = this.blocks, i = this.lastByteIndex;
|
||||||
|
blocks[16] = this.block;
|
||||||
|
blocks[i >>> 2] |= EXTRA[i & 3];
|
||||||
|
this.block = blocks[16];
|
||||||
|
if (i >= 56) {
|
||||||
|
if (!this.hashed) {
|
||||||
|
this.hash();
|
||||||
|
}
|
||||||
|
blocks[0] = this.block;
|
||||||
|
blocks[16] = blocks[1] = blocks[2] = blocks[3] =
|
||||||
|
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
|
||||||
|
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
|
||||||
|
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
|
||||||
|
}
|
||||||
|
blocks[14] = this.hBytes << 3 | this.bytes >>> 29;
|
||||||
|
blocks[15] = this.bytes << 3;
|
||||||
|
this.hash();
|
||||||
|
};
|
||||||
|
|
||||||
var a = h0;
|
Sha1.prototype.hash = function () {
|
||||||
var b = h1;
|
var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4;
|
||||||
var c = h2;
|
var f, j, t, blocks = this.blocks;
|
||||||
var d = h3;
|
|
||||||
var e = h4;
|
|
||||||
var f, tmp;
|
|
||||||
|
|
||||||
for(j = 0;j < 20;++j) {
|
for(j = 16; j < 80; ++j) {
|
||||||
|
t = blocks[j - 3] ^ blocks[j - 8] ^ blocks[j - 14] ^ blocks[j - 16];
|
||||||
|
blocks[j] = (t << 1) | (t >>> 31);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(j = 0; j < 20; j += 5) {
|
||||||
f = (b & c) | ((~b) & d);
|
f = (b & c) | ((~b) & d);
|
||||||
tmp = leftrotate(a, 5) + f + e + 0x5A827999 + w[j];
|
t = (a << 5) | (a >>> 27);
|
||||||
e = d;
|
e = t + f + e + 1518500249 + blocks[j] << 0;
|
||||||
d = c;
|
b = (b << 30) | (b >>> 2);
|
||||||
c = leftrotate(b, 30);
|
|
||||||
b = a;
|
f = (a & b) | ((~a) & c);
|
||||||
a = tmp;
|
t = (e << 5) | (e >>> 27);
|
||||||
|
d = t + f + d + 1518500249 + blocks[j + 1] << 0;
|
||||||
|
a = (a << 30) | (a >>> 2);
|
||||||
|
|
||||||
|
f = (e & a) | ((~e) & b);
|
||||||
|
t = (d << 5) | (d >>> 27);
|
||||||
|
c = t + f + c + 1518500249 + blocks[j + 2] << 0;
|
||||||
|
e = (e << 30) | (e >>> 2);
|
||||||
|
|
||||||
|
f = (d & e) | ((~d) & a);
|
||||||
|
t = (c << 5) | (c >>> 27);
|
||||||
|
b = t + f + b + 1518500249 + blocks[j + 3] << 0;
|
||||||
|
d = (d << 30) | (d >>> 2);
|
||||||
|
|
||||||
|
f = (c & d) | ((~c) & e);
|
||||||
|
t = (b << 5) | (b >>> 27);
|
||||||
|
a = t + f + a + 1518500249 + blocks[j + 4] << 0;
|
||||||
|
c = (c << 30) | (c >>> 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(;j < 40;++j) {
|
for(; j < 40; j += 5) {
|
||||||
f = b ^ c ^ d;
|
f = b ^ c ^ d;
|
||||||
tmp = leftrotate(a, 5) + f + e + 0x6ED9EBA1 + w[j];
|
t = (a << 5) | (a >>> 27);
|
||||||
e = d;
|
e = t + f + e + 1859775393 + blocks[j] << 0;
|
||||||
d = c;
|
b = (b << 30) | (b >>> 2);
|
||||||
c = leftrotate(b, 30);
|
|
||||||
b = a;
|
f = a ^ b ^ c;
|
||||||
a = tmp;
|
t = (e << 5) | (e >>> 27);
|
||||||
|
d = t + f + d + 1859775393 + blocks[j + 1] << 0;
|
||||||
|
a = (a << 30) | (a >>> 2);
|
||||||
|
|
||||||
|
f = e ^ a ^ b;
|
||||||
|
t = (d << 5) | (d >>> 27);
|
||||||
|
c = t + f + c + 1859775393 + blocks[j + 2] << 0;
|
||||||
|
e = (e << 30) | (e >>> 2);
|
||||||
|
|
||||||
|
f = d ^ e ^ a;
|
||||||
|
t = (c << 5) | (c >>> 27);
|
||||||
|
b = t + f + b + 1859775393 + blocks[j + 3] << 0;
|
||||||
|
d = (d << 30) | (d >>> 2);
|
||||||
|
|
||||||
|
f = c ^ d ^ e;
|
||||||
|
t = (b << 5) | (b >>> 27);
|
||||||
|
a = t + f + a + 1859775393 + blocks[j + 4] << 0;
|
||||||
|
c = (c << 30) | (c >>> 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// k = 0x8F1BBCDC;
|
for(; j < 60; j += 5) {
|
||||||
for(;j < 60;++j) {
|
|
||||||
f = (b & c) | (b & d) | (c & d);
|
f = (b & c) | (b & d) | (c & d);
|
||||||
tmp = leftrotate(a, 5) + f + e + 0x8F1BBCDC + w[j];
|
t = (a << 5) | (a >>> 27);
|
||||||
e = d;
|
e = t + f + e - 1894007588 + blocks[j] << 0;
|
||||||
d = c;
|
b = (b << 30) | (b >>> 2);
|
||||||
c = leftrotate(b, 30);
|
|
||||||
b = a;
|
f = (a & b) | (a & c) | (b & c);
|
||||||
a = tmp;
|
t = (e << 5) | (e >>> 27);
|
||||||
|
d = t + f + d - 1894007588 + blocks[j + 1] << 0;
|
||||||
|
a = (a << 30) | (a >>> 2);
|
||||||
|
|
||||||
|
f = (e & a) | (e & b) | (a & b);
|
||||||
|
t = (d << 5) | (d >>> 27);
|
||||||
|
c = t + f + c - 1894007588 + blocks[j + 2] << 0;
|
||||||
|
e = (e << 30) | (e >>> 2);
|
||||||
|
|
||||||
|
f = (d & e) | (d & a) | (e & a);
|
||||||
|
t = (c << 5) | (c >>> 27);
|
||||||
|
b = t + f + b - 1894007588 + blocks[j + 3] << 0;
|
||||||
|
d = (d << 30) | (d >>> 2);
|
||||||
|
|
||||||
|
f = (c & d) | (c & e) | (d & e);
|
||||||
|
t = (b << 5) | (b >>> 27);
|
||||||
|
a = t + f + a - 1894007588 + blocks[j + 4] << 0;
|
||||||
|
c = (c << 30) | (c >>> 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(;j < 80;++j) {
|
for(; j < 80; j += 5) {
|
||||||
f = b ^ c ^ d;
|
f = b ^ c ^ d;
|
||||||
tmp = leftrotate(a, 5) + f + e + 0xCA62C1D6 + w[j];
|
t = (a << 5) | (a >>> 27);
|
||||||
e = d;
|
e = t + f + e - 899497514 + blocks[j] << 0;
|
||||||
d = c;
|
b = (b << 30) | (b >>> 2);
|
||||||
c = leftrotate(b, 30);
|
|
||||||
b = a;
|
f = a ^ b ^ c;
|
||||||
a = tmp;
|
t = (e << 5) | (e >>> 27);
|
||||||
}
|
d = t + f + d - 899497514 + blocks[j + 1] << 0;
|
||||||
|
a = (a << 30) | (a >>> 2);
|
||||||
|
|
||||||
h0 += a;
|
f = e ^ a ^ b;
|
||||||
h1 += b;
|
t = (d << 5) | (d >>> 27);
|
||||||
h2 += c;
|
c = t + f + c - 899497514 + blocks[j + 2] << 0;
|
||||||
h3 += d;
|
e = (e << 30) | (e >>> 2);
|
||||||
h4 += e;
|
|
||||||
|
f = d ^ e ^ a;
|
||||||
|
t = (c << 5) | (c >>> 27);
|
||||||
|
b = t + f + b - 899497514 + blocks[j + 3] << 0;
|
||||||
|
d = (d << 30) | (d >>> 2);
|
||||||
|
|
||||||
|
f = c ^ d ^ e;
|
||||||
|
t = (b << 5) | (b >>> 27);
|
||||||
|
a = t + f + a - 899497514 + blocks[j + 4] << 0;
|
||||||
|
c = (c << 30) | (c >>> 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
return toHexString(h0) + toHexString(h1)+ toHexString(h2) + toHexString(h3) + toHexString(h4);
|
this.h0 = this.h0 + a << 0;
|
||||||
|
this.h1 = this.h1 + b << 0;
|
||||||
|
this.h2 = this.h2 + c << 0;
|
||||||
|
this.h3 = this.h3 + d << 0;
|
||||||
|
this.h4 = this.h4 + e << 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
var leftrotate = function(x, c) {
|
Sha1.prototype.hex = function () {
|
||||||
return (x << c) | (x >>> (32 - c));
|
this.finalize();
|
||||||
|
|
||||||
|
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] +
|
||||||
|
HEX_CHARS[(h0 >>> 20) & 0x0F] + HEX_CHARS[(h0 >>> 16) & 0x0F] +
|
||||||
|
HEX_CHARS[(h0 >>> 12) & 0x0F] + HEX_CHARS[(h0 >>> 8) & 0x0F] +
|
||||||
|
HEX_CHARS[(h0 >>> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
|
||||||
|
HEX_CHARS[(h1 >>> 28) & 0x0F] + HEX_CHARS[(h1 >>> 24) & 0x0F] +
|
||||||
|
HEX_CHARS[(h1 >>> 20) & 0x0F] + HEX_CHARS[(h1 >>> 16) & 0x0F] +
|
||||||
|
HEX_CHARS[(h1 >>> 12) & 0x0F] + HEX_CHARS[(h1 >>> 8) & 0x0F] +
|
||||||
|
HEX_CHARS[(h1 >>> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
|
||||||
|
HEX_CHARS[(h2 >>> 28) & 0x0F] + HEX_CHARS[(h2 >>> 24) & 0x0F] +
|
||||||
|
HEX_CHARS[(h2 >>> 20) & 0x0F] + HEX_CHARS[(h2 >>> 16) & 0x0F] +
|
||||||
|
HEX_CHARS[(h2 >>> 12) & 0x0F] + HEX_CHARS[(h2 >>> 8) & 0x0F] +
|
||||||
|
HEX_CHARS[(h2 >>> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
|
||||||
|
HEX_CHARS[(h3 >>> 28) & 0x0F] + HEX_CHARS[(h3 >>> 24) & 0x0F] +
|
||||||
|
HEX_CHARS[(h3 >>> 20) & 0x0F] + HEX_CHARS[(h3 >>> 16) & 0x0F] +
|
||||||
|
HEX_CHARS[(h3 >>> 12) & 0x0F] + HEX_CHARS[(h3 >>> 8) & 0x0F] +
|
||||||
|
HEX_CHARS[(h3 >>> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
|
||||||
|
HEX_CHARS[(h4 >>> 28) & 0x0F] + HEX_CHARS[(h4 >>> 24) & 0x0F] +
|
||||||
|
HEX_CHARS[(h4 >>> 20) & 0x0F] + HEX_CHARS[(h4 >>> 16) & 0x0F] +
|
||||||
|
HEX_CHARS[(h4 >>> 12) & 0x0F] + HEX_CHARS[(h4 >>> 8) & 0x0F] +
|
||||||
|
HEX_CHARS[(h4 >>> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F];
|
||||||
};
|
};
|
||||||
|
|
||||||
var toHexString = function(num) {
|
Sha1.prototype.toString = Sha1.prototype.hex;
|
||||||
var hex = '';
|
|
||||||
for(var i = 0; i < 4; i++) {
|
Sha1.prototype.digest = function () {
|
||||||
var offset = 3 - i << 3;
|
this.finalize();
|
||||||
hex += HEX_CHARS[(num >> (offset + 4)) & 0x0F] + HEX_CHARS[(num >> offset) & 0x0F];
|
|
||||||
}
|
var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4;
|
||||||
return hex;
|
|
||||||
|
return [
|
||||||
|
(h0 >>> 24) & 0xFF, (h0 >>> 16) & 0xFF, (h0 >>> 8) & 0xFF, h0 & 0xFF,
|
||||||
|
(h1 >>> 24) & 0xFF, (h1 >>> 16) & 0xFF, (h1 >>> 8) & 0xFF, h1 & 0xFF,
|
||||||
|
(h2 >>> 24) & 0xFF, (h2 >>> 16) & 0xFF, (h2 >>> 8) & 0xFF, h2 & 0xFF,
|
||||||
|
(h3 >>> 24) & 0xFF, (h3 >>> 16) & 0xFF, (h3 >>> 8) & 0xFF, h3 & 0xFF,
|
||||||
|
(h4 >>> 24) & 0xFF, (h4 >>> 16) & 0xFF, (h4 >>> 8) & 0xFF, h4 & 0xFF
|
||||||
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
var getBytesFromUtf8 = function(str) {
|
Sha1.prototype.array = Sha1.prototype.digest;
|
||||||
var bytes = [], index = 0;
|
|
||||||
for (var i = 0;i < str.length; i++) {
|
Sha1.prototype.arrayBuffer = function () {
|
||||||
var c = str.charCodeAt(i);
|
this.finalize();
|
||||||
if (c < 0x80) {
|
|
||||||
bytes[index++] = c;
|
var buffer = new ArrayBuffer(20);
|
||||||
} else if (c < 0x800) {
|
var dataView = new DataView(buffer);
|
||||||
bytes[index++] = 0xc0 | (c >> 6);
|
dataView.setUint32(0, this.h0);
|
||||||
bytes[index++] = 0x80 | (c & 0x3f);
|
dataView.setUint32(4, this.h1);
|
||||||
} else if (c < 0xd800 || c >= 0xe000) {
|
dataView.setUint32(8, this.h2);
|
||||||
bytes[index++] = 0xe0 | (c >> 12);
|
dataView.setUint32(12, this.h3);
|
||||||
bytes[index++] = 0x80 | ((c >> 6) & 0x3f);
|
dataView.setUint32(16, this.h4);
|
||||||
bytes[index++] = 0x80 | (c & 0x3f);
|
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 {
|
} else {
|
||||||
c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff));
|
code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
|
||||||
bytes[index++] = 0xf0 | (c >> 18);
|
bytes[index++] = (0xf0 | (code >>> 18));
|
||||||
bytes[index++] = 0x80 | ((c >> 12) & 0x3f);
|
bytes[index++] = (0x80 | ((code >>> 12) & 0x3f));
|
||||||
bytes[index++] = 0x80 | ((c >> 6) & 0x3f);
|
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
|
||||||
bytes[index++] = 0x80 | (c & 0x3f);
|
bytes[index++] = (0x80 | (code & 0x3f));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return bytes;
|
key = bytes;
|
||||||
};
|
}
|
||||||
|
|
||||||
var getBlocksFromAscii = function(message) {
|
if (key.length > 64) {
|
||||||
// a block is 32 bits(4 bytes), a chunk is 512 bits(64 bytes)
|
key = (new Sha1(true)).update(key).array();
|
||||||
var length = message.length;
|
|
||||||
var chunkCount = ((length + 8) >> 6) + 1;
|
|
||||||
var blockCount = chunkCount << 4; // chunkCount * 16
|
|
||||||
var blocks = [], i;
|
|
||||||
for(i = 0;i < blockCount;++i) {
|
|
||||||
blocks[i] = 0;
|
|
||||||
}
|
}
|
||||||
for(i = 0;i < length;++i) {
|
|
||||||
blocks[i >> 2] |= message.charCodeAt(i) << (3 - (i & 3) << 3);
|
var oKeyPad = [], iKeyPad = [];
|
||||||
|
for (i = 0; i < 64; ++i) {
|
||||||
|
var b = key[i] || 0;
|
||||||
|
oKeyPad[i] = 0x5c ^ b;
|
||||||
|
iKeyPad[i] = 0x36 ^ b;
|
||||||
}
|
}
|
||||||
blocks[i >> 2] |= 0x80 << (3 - (i & 3) << 3);
|
|
||||||
blocks[blockCount - 1] = length << 3; // length * 8
|
|
||||||
return blocks;
|
|
||||||
};
|
|
||||||
|
|
||||||
var getBlocksFromUtf8 = function(message) {
|
Sha1.call(this, sharedMemory);
|
||||||
var bytes = getBytesFromUtf8(message);
|
|
||||||
var length = bytes.length;
|
this.update(iKeyPad);
|
||||||
var chunkCount = ((length + 8) >> 6) + 1;
|
this.oKeyPad = oKeyPad;
|
||||||
var blockCount = chunkCount << 4; // chunkCount * 16
|
this.inner = true;
|
||||||
var blocks = [], i;
|
this.sharedMemory = sharedMemory;
|
||||||
for(i = 0;i < blockCount;++i) {
|
|
||||||
blocks[i] = 0;
|
|
||||||
}
|
}
|
||||||
for(i = 0;i < length;++i) {
|
HmacSha1.prototype = new Sha1();
|
||||||
blocks[i >> 2] |= bytes[i] << (3 - (i & 3) << 3);
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
blocks[i >> 2] |= 0x80 << (3 - (i & 3) << 3);
|
|
||||||
blocks[blockCount - 1] = length << 3; // length * 8
|
|
||||||
return blocks;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if(typeof(module) != 'undefined') {
|
var exports = createMethod();
|
||||||
module.exports = sha1;
|
exports.sha1 = exports;
|
||||||
|
exports.sha1.hmac = createHmacMethod();
|
||||||
|
|
||||||
|
if (COMMON_JS) {
|
||||||
|
module.exports = exports;
|
||||||
|
} else {
|
||||||
|
root.sha1 = exports;
|
||||||
|
if (AMD) {
|
||||||
|
define(function () {
|
||||||
|
return exports;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else if(root) {
|
|
||||||
root.sha1 = sha1;
|
|
||||||
}
|
}
|
||||||
}(this));
|
})();
|
||||||
|
@ -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,3 +1,158 @@
|
|||||||
sha1 = require('../src/sha1.js');
|
|
||||||
expect = require('expect.js');
|
expect = require('expect.js');
|
||||||
|
Worker = require("tiny-worker");
|
||||||
|
|
||||||
|
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('./test.js');
|
||||||
|
require('./hmac-test.js');
|
||||||
|
unset();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Node.js env
|
||||||
|
BUFFER = true;
|
||||||
|
runCommonJsTest();
|
||||||
|
|
||||||
|
// Node.js env, no Buffer.from
|
||||||
|
JS_SHA1_NO_BUFFER_FROM = true
|
||||||
|
runCommonJsTest();
|
||||||
|
|
||||||
|
// Webpack browser env
|
||||||
|
JS_SHA1_NO_NODE_JS = true;
|
||||||
|
runCommonJsTest();
|
||||||
|
|
||||||
|
// browser env
|
||||||
|
JS_SHA1_NO_NODE_JS = true;
|
||||||
|
JS_SHA1_NO_COMMON_JS = true;
|
||||||
|
runWindowTest();
|
||||||
|
|
||||||
|
// browser env and no array buffer
|
||||||
|
JS_SHA1_NO_NODE_JS = true;
|
||||||
|
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
|
||||||
|
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');
|
||||||
|
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>
|
@ -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…
Reference in New Issue