8 Commits
Author SHA1 Message Date
Yi-Cyuan Chen 65b3898c4e Fixed
- incorrect result when first bit is 1 of bytes.

Changed
- prevent webpack to require dependencies.
2017-12-21 20:13:31 +08:00
Yi-Cyuan Chen 4ee5af05d3 Fixed incorrect result when file size >= 512M. 2017-10-31 20:20:45 +08:00
Chen, Yi-Cyuan c724643de0 Fixed ArrayBuffer dosen't work in Webpack. 2016-12-30 09:56:58 +08:00
Chen, Yi-Cyuan 80d540792b Added
- update method.
- support Array output.
- support ArrayBuffer output.
- support AMD.
2016-12-05 17:00:43 +08:00
Chen Yi-Cyuan 731295e355 * Support ArrayBuffer input.
* Improve performance.
2015-03-04 21:16:06 +08:00
Chen Yi-Cyuan 2f22f6285d update README 2015-02-17 10:28:09 +08:00
Chen Yi-Cyuan 72f01d0043 * Support byte array input.
* Remove ascii parameter.
* Improve performance.
* Add test cases.
2015-02-16 22:09:23 +08:00
emn178 4122482ac4 update covignore 2015-01-07 15:30:59 +08:00
14 changed files with 2473 additions and 263 deletions
+2
View File
@@ -0,0 +1,2 @@
/node_modules/
/tests/
+3 -1
View File
@@ -1 +1,3 @@
node_modules/ /node_modules/
/coverage/
/.nyc_output/
+8
View File
@@ -0,0 +1,8 @@
/node_modules/
/coverage/
/.nyc_output/
/tests/
.covignore
.travis.yml
.npmignore
bower.json
+8 -10
View File
@@ -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
+51 -11
View File
@@ -1,18 +1,58 @@
# v0.1.3 / 2015-01-07 # Change Log
* Add bower package. ## v0.6.0 / 2017-12-21
* Fixed JSHint warnings. ### Fixed
* Add travis. - incorrect result when first bit is 1 of bytes.
* Add coveralls.
# v0.1.2 / 2014-07-27 ### Changed
- prevent webpack to require dependencies.
Fixed accents bug ## v0.5.0 / 2017-10-31
### Fixed
- incorrect result when file size >= 512M.
# v0.1.1 / 2014-01-05 ## v0.4.1 / 2016-12-30
### Fixed
- ArrayBuffer dosen't work in Webpack.
Update license ## v0.4.0 / 2016-12-05
### Added
- update method.
- support Array output.
- support ArrayBuffer output.
- support AMD.
# v0.1.0 / 2014-01-04 ## v0.3.0 / 2015-03-04
### Added
- support ArrayBuffer input.
### Changed
- improve performance.
Initial release ## 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.
+1 -1
View File
@@ -1,4 +1,4 @@
Copyright 2014-2015 emn178@gmail.com Copyright 2014-2017 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
+29 -61
View File
@@ -1,6 +1,6 @@
# js-sha1 # js-sha1
[![Build Status](https://api.travis-ci.org/emn178/js-sha1.png)](https://travis-ci.org/emn178/js-sha1) [![Build Status](https://travis-ci.org/emn178/js-sha1.svg?branch=master)](https://travis-ci.org/emn178/js-sha1)
[![Build Status](https://coveralls.io/repos/emn178/js-sha1/badge.png?branch=master)](https://coveralls.io/r/emn178/js-sha1?branch=master) [![Coverage Status](https://coveralls.io/repos/emn178/js-sha1/badge.svg?branch=master)](https://coveralls.io/r/emn178/js-sha1?branch=master)
[![NPM](https://nodei.co/npm/js-sha1.png?stars&downloads)](https://nodei.co/npm/js-sha1/) [![NPM](https://nodei.co/npm/js-sha1.png?stars&downloads)](https://nodei.co/npm/js-sha1/)
A simple SHA1 hash function for JavaScript supports UTF-8 encoding. A simple SHA1 hash function for JavaScript supports UTF-8 encoding.
@@ -24,77 +24,45 @@ For node.js, you can use this command to install:
You could use like this: You could use like this:
```JavaScript ```JavaScript
sha1('Message to hash'); sha1('Message to hash');
var hash = sha1.create();
hash.update('Message to hash');
hash.hex();
``` ```
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'); sha1 = require('js-sha1');
``` ```
It supports AMD:
### Methods ```JavaScript
require(['your/path/sha1.js'], function(sha1) {
#### sha1(str, asciiOnly) // ...
});
Hash string to sha1, set asciiOnly to true for better performace if you ensure input is ascii. ```
##### *str: `String`*
String to hash.
##### *asciiOnly: `Boolean` (default: `false`)*
Specify the string encoding is ASCII.
## Example ## Example
Code
```JavaScript ```JavaScript
sha1(''); sha1(''); // da39a3ee5e6b4b0d3255bfef95601890afd80709
sha1('The quick brown fox jumps over the lazy dog'); sha1('The quick brown fox jumps over the lazy dog'); // 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12
sha1('The quick brown fox jumps over the lazy dog.'); sha1('The quick brown fox jumps over the lazy dog.'); // 408d94384216f890ff7a0c3528e8bed1e0b01621
// It also supports UTF-8 encoding
sha1('中文'); // 7be2d2d20c106eee0836c9bc2b939890a78e8fb3
// It also supports byte `Array`, `Uint8Array`, `ArrayBuffer`
sha1([]); // da39a3ee5e6b4b0d3255bfef95601890afd80709
sha1(new Uint8Array([])); // da39a3ee5e6b4b0d3255bfef95601890afd80709
// Different output
sha1(''); // da39a3ee5e6b4b0d3255bfef95601890afd80709
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.digest(''); // [218, 57, 163, 238, 94, 107, 75, 13, 50, 85, 191, 239, 149, 96, 24, 144, 175, 216, 7, 9]
sha1.arrayBuffer(''); // ArrayBuffer
``` ```
Output
da39a3ee5e6b4b0d3255bfef95601890afd80709
2fd4e1c67a2d28fced849ee1bb76e7391b93eb12
408d94384216f890ff7a0c3528e8bed1e0b01621
It also supports UTF-8 encoding:
Code
```JavaScript
sha1('中文');
```
Output
7be2d2d20c106eee0836c9bc2b939890a78e8fb3
## Extensions
### jQuery
If you prefer jQuery style, you can add following code to add a jQuery extension.
Code
```JavaScript
jQuery.sha1 = sha1
```
And then you could use like this:
```JavaScript
$.sha1('message');
```
### Prototype
If you prefer prototype style, you can add following code to add a prototype extension.
Code
```JavaScript
String.prototype.sha1 = function() {
return sha1(this);
};
```
And then you could use like this:
```JavaScript
'message'.sha1();
```
## License ## License
The project is released under the [MIT license](http://www.opensource.org/licenses/MIT). The project is released under the [MIT license](http://www.opensource.org/licenses/MIT).
## Contact ## Contact
The project's website is located at https://github.com/emn178/js-sha1 The project's website is located at https://github.com/emn178/js-sha1
Author: emn178@gmail.com Author: Chen, Yi-Cyuan (emn178@gmail.com)
+2 -2
View File
@@ -1,7 +1,7 @@
{ {
"name": "js-sha1", "name": "js-sha1",
"version": "0.1.3", "version": "0.6.0",
"main": ["build/sha1.min.js"], "main": ["src/sha1.js"],
"ignore": [ "ignore": [
"samples", "samples",
"tests" "tests"
+9 -3
View File
File diff suppressed because one or more lines are too long
+1784
View File
File diff suppressed because it is too large Load Diff
+15 -5
View File
@@ -1,15 +1,20 @@
{ {
"name": "js-sha1", "name": "js-sha1",
"version": "0.1.3", "version": "0.6.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": {
"expect.js": "~0.3.1", "expect.js": "~0.3.1",
"jscoverage": "~0.5.9" "mocha": "~2.3.4",
"nyc": "^11.3.0",
"requirejs": "^2.1.22",
"uglify-js": "^3.1.9"
}, },
"scripts": { "scripts": {
"test": "mocha tests/node-test.js -r jscoverage", "test": "nyc mocha tests/node-test.js",
"coveralls": "mocha tests/node-test.js -R mocha-lcov-reporter -r jscoverage | coveralls" "report": "nyc --reporter=html --reporter=text mocha tests/node-test.js",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"build": "uglifyjs src/sha1.js -c -m eval --comments -o build/sha1.min.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@@ -23,9 +28,14 @@
"HMAC" "HMAC"
], ],
"license": "MIT", "license": "MIT",
"author": "emn178 <emn178@gmail.com>", "author": "Chen, Yi-Cyuan <emn178@gmail.com>",
"homepage": "https://github.com/emn178/js-sha1", "homepage": "https://github.com/emn178/js-sha1",
"bugs": { "bugs": {
"url": "https://github.com/emn178/js-sha1/issues" "url": "https://github.com/emn178/js-sha1/issues"
},
"nyc": {
"exclude": [
"tests"
]
} }
} }
+336 -138
View File
@@ -1,173 +1,371 @@
/* /*
* 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.6.0
* * @author Chen, Yi-Cyuan [emn178@gmail.com]
* Licensed under the MIT license: * @copyright Chen, Yi-Cyuan 2014-2017
* http://www.opensource.org/licenses/MIT * @license MIT
*/ */
;(function(root, undefined){ /*jslint bitwise: true */
(function() {
'use strict'; 'use strict';
var root = typeof window === 'object' ? window : {};
var NODE_JS = !root.JS_SHA1_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
if (NODE_JS) {
root = global;
}
var COMMON_JS = !root.JS_SHA1_NO_COMMON_JS && typeof module === 'object' && module.exports;
var AMD = typeof define === 'function' && define.amd;
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 sha1 = function(message, asciiOnly) { var blocks = [];
var blocks, h0, h1, h2, h3, h4;
if(!asciiOnly && /[^\x00-\x7F]/.test(message)) { var createOutputMethod = function (outputType) {
blocks = getBlocksFromUtf8(message); 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 = eval("require('crypto')");
var Buffer = eval("require('buffer').Buffer");
var nodeMethod = function (message) {
if (typeof message === 'string') {
return crypto.createHash('sha1').update(message, 'utf8').digest('hex');
} else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
} else if (message.length === undefined) {
return method(message);
}
return crypto.createHash('sha1').update(new Buffer(message)).digest('hex');
};
return nodeMethod;
};
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) {
return;
}
var notString = typeof(message) !== 'string';
if (notString && message.constructor === root.ArrayBuffer) {
message = new Uint8Array(message);
}
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;
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(notString) {
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) {
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];
}
}
}
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();
};
Sha1.prototype.hash = function () {
var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4;
var f, j, t, blocks = this.blocks;
for(j = 16; j < 80; ++j) { for(j = 16; j < 80; ++j) {
var x = w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16]; t = blocks[j - 3] ^ blocks[j - 8] ^ blocks[j - 14] ^ blocks[j - 16];
w[j] = leftrotate(x, 1); blocks[j] = (t << 1) | (t >>> 31);
} }
var a = h0; for(j = 0; j < 20; j += 5) {
var b = h1;
var c = h2;
var d = h3;
var e = h4;
var f, tmp;
for(j = 0;j < 20;++j) {
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);
f = e ^ a ^ b;
t = (d << 5) | (d >>> 27);
c = t + f + c - 899497514 + blocks[j + 2] << 0;
e = (e << 30) | (e >>> 2);
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);
} }
h0 += a; this.h0 = this.h0 + a << 0;
h1 += b; this.h1 = this.h1 + b << 0;
h2 += c; this.h2 = this.h2 + c << 0;
h3 += d; this.h3 = this.h3 + d << 0;
h4 += e; this.h4 = this.h4 + e << 0;
}
return toHexString(h0) + toHexString(h1)+ toHexString(h2) + toHexString(h3) + toHexString(h4);
}; };
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;
};
var exports = createMethod();
if (COMMON_JS) {
module.exports = exports;
} else { } else {
c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff)); root.sha1 = exports;
bytes[index++] = 0xf0 | (c >> 18); if (AMD) {
bytes[index++] = 0x80 | ((c >> 12) & 0x3f); define(function () {
bytes[index++] = 0x80 | ((c >> 6) & 0x3f); return exports;
bytes[index++] = 0x80 | (c & 0x3f); });
} }
} }
return bytes; })();
};
var getBlocksFromAscii = function(message) {
// a block is 32 bits(4 bytes), a chunk is 512 bits(64 bytes)
var length = message.length;
var chunkCount = ((length + 8) >> 6) + 1;
var blockCount = chunkCount << 4; // chunkCount * 16
var blocks = [], 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);
}
blocks[i >> 2] |= 0x80 << (3 - (i & 3) << 3);
blocks[blockCount - 1] = length << 3; // length * 8
return blocks;
};
var getBlocksFromUtf8 = function(message) {
var bytes = getBytesFromUtf8(message);
var length = bytes.length;
var chunkCount = ((length + 8) >> 6) + 1;
var blockCount = chunkCount << 4; // chunkCount * 16
var blocks = [], i;
for(i = 0;i < blockCount;++i) {
blocks[i] = 0;
}
for(i = 0;i < length;++i) {
blocks[i >> 2] |= bytes[i] << (3 - (i & 3) << 3);
}
blocks[i >> 2] |= 0x80 << (3 - (i & 3) << 3);
blocks[blockCount - 1] = length << 3; // length * 8
return blocks;
};
if(typeof(module) != 'undefined') {
module.exports = sha1;
}
else if(root) {
root.sha1 = sha1;
}
}(this));
+39 -1
View File
@@ -1,3 +1,41 @@
sha1 = require('../src/sha1.js'); // Node.js env
expect = require('expect.js'); expect = require('expect.js');
sha1 = require('../src/sha1.js');
require('./test.js'); require('./test.js');
delete require.cache[require.resolve('../src/sha1.js')];
delete require.cache[require.resolve('./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')];
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')];
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');
};
define.amd = true;
require('../src/sha1.js');
+176 -20
View File
@@ -1,30 +1,186 @@
describe('ascii', function() { (function (sha1) {
describe('less than 64 bytes', function() { Array.prototype.toHexString = ArrayBuffer.prototype.toHexString = function () {
it('should be successful', function() { var array = new Uint8Array(this);
expect(sha1('')).to.be('da39a3ee5e6b4b0d3255bfef95601890afd80709'); var hex = '';
expect(sha1('The quick brown fox jumps over the lazy dog')).to.be('2fd4e1c67a2d28fced849ee1bb76e7391b93eb12'); for (var i = 0; i < array.length; ++i) {
expect(sha1('The quick brown fox jumps over the lazy dog.', true)).to.be('408d94384216f890ff7a0c3528e8bed1e0b01621'); var c = array[i].toString('16');
hex += c.length == 1 ? '0' + c : c;
}
return hex;
};
var testCases = {
'ascii': {
'da39a3ee5e6b4b0d3255bfef95601890afd80709': '',
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': 'The quick brown fox jumps over the lazy dog',
'408d94384216f890ff7a0c3528e8bed1e0b01621': 'The quick brown fox jumps over the lazy dog.'
},
'ascii more than 64 bytes': {
'8690faab7755408a03875895176fac318f14a699': 'The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.'
},
'UTF8': {
'7be2d2d20c106eee0836c9bc2b939890a78e8fb3': '中文',
'9e4e5d978deced901d621475b03f1ded19e945bf': 'aécio',
'4667688a63420661469c8dbc0f871770349bab08': '𠜎'
},
'UTF8 more than 64 bytes': {
'ad8aae581c915fe01c4964a5e8b322cae74ee5c5': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一',
'3a15ad3ce9efdd4bf982eaaaecdeda36a887a3f9': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。'
},
'special length': {
'4cdeae78e8b7285aef73e0a15eec7d5b30f3f3e3': '0123456780123456780123456780123456780123456780123456780',
'e657e6bb6b5d0c2bf7e929451c14a5302589a60b': '01234567801234567801234567801234567801234567801234567801',
'e7ad97591c1a99d54d80751d341899769884c75a': '0123456780123456780123456780123456780123456780123456780123456780',
'55a13698cdc010c0d16dab2f7dc10f43a713f12f': '01234567801234567801234567801234567801234567801234567801234567801234567',
'006575418c27b0158e55a6d261c46f86b33a496a': '012345678012345678012345678012345678012345678012345678012345678012345678'
},
'Array': {
'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],
'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': {
'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': {
'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': {
'5ba93c9db0cff93f52b521d7420e43f6eda2784f': new ArrayBuffer(1)
},
'Object': {
'da39a3ee5e6b4b0d3255bfef95601890afd80709': {what: 'ever'}
}
};
if (typeof process == 'object') {
testCases['Buffer'] = {
'da39a3ee5e6b4b0d3255bfef95601890afd80709': new Buffer(0),
'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]))
};
}
var methods = [
{
name: 'sha1',
call: sha1,
},
{
name: 'sha1.hex',
call: sha1.hex
},
{
name: 'sha1.array',
call: function (message) {
return sha1.array(message).toHexString();
}
},
{
name: 'sha1.digest',
call: function (message) {
return sha1.digest(message).toHexString();
}
},
{
name: 'sha1.arrayBuffer',
call: function (message) {
return sha1.arrayBuffer(message).toHexString();
}
}
];
var classMethods = [
{
name: 'create',
call: function (message) {
return sha1.create().update(message).toString();
}
},
{
name: 'update',
call: function (message) {
return sha1.update(message).toString();
}
},
{
name: 'hex',
call: function (message) {
return sha1.update(message).hex();
}
},
{
name: 'array',
call: function (message) {
return sha1.update(message).array().toHexString();
}
},
{
name: 'digest',
call: function (message) {
return sha1.update(message).digest().toHexString();
}
},
{
name: 'arrayBuffer',
call: function (message) {
return sha1.update(message).arrayBuffer().toHexString();
}
},
{
name: 'finalize',
call: function (message) {
var hash = sha1.update(message);
hash.hex();
hash.update(message);
return hash.hex();
}
}
];
methods.forEach(function (method) {
describe('#' + method.name, function () {
for (var testCaseName in testCases) {
(function (testCaseName) {
var testCase = testCases[testCaseName];
context('when ' + testCaseName, function () {
for (var hash in testCase) {
(function (message, hash) {
it('should be equal', function () {
expect(method.call(message)).to.be(hash);
});
})(testCase[hash], hash);
}
});
})(testCaseName);
}
}); });
}); });
describe('more than 64 bytes', function() { describe('Sha1', function () {
it('should be successful', function() { classMethods.forEach(function (method) {
expect(sha1('The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.')).to.be('8690faab7755408a03875895176fac318f14a699'); describe('#' + method.name, function () {
for (var testCaseName in testCases) {
(function (testCaseName) {
var testCase = testCases[testCaseName];
context('when ' + testCaseName, function () {
for (var hash in testCase) {
(function (message, hash) {
it('should be equal', function () {
expect(method.call(message)).to.be(hash);
}); });
})(testCase[hash], hash);
}
}); });
}); })(testCaseName);
}
describe('UTF8', function() {
describe('less than 64 bytes', function() {
it('should be successful', function() {
expect(sha1('中文')).to.be('7be2d2d20c106eee0836c9bc2b939890a78e8fb3');
expect(sha1('aécio')).to.be('9e4e5d978deced901d621475b03f1ded19e945bf');
}); });
}); });
describe('more than 64 bytes', function() { context('when large size', function () {
it('should be successful', function() { var hash = sha1.create();
expect(sha1('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('3a15ad3ce9efdd4bf982eaaaecdeda36a887a3f9'); hash.bytes = 4294967295;
}); hash.update('any');
expect(hash.hBytes).to.be(1);
}); });
}); });
})(sha1);