4 Commits
Author SHA1 Message Date
Chen, Yi-Cyuan 2bdc4894fc fixed travis build 2017-07-14 13:28:43 +08:00
Chen, Yi-Cyuan c00df3af37 Added
- support for web worker. #11

Changed
- throw error if input type is incorrect.
- prevent webpack to require dependencies.
2017-07-14 13:10:27 +08:00
Chen, Yi-Cyuan 8b9a23ce32 Fixed root is undefined in some special environment. #7 2017-01-18 09:44:08 +08:00
Chen Yi-Cyuan da009f933f update comment version 2016-07-30 20:39:20 +08:00
21 changed files with 439 additions and 216 deletions
+1 -1
View File
@@ -1,2 +1,2 @@
/node_modules/
/tests/ /tests/
node_modules/
+2 -2
View File
@@ -1,2 +1,2 @@
covreporter /node_modules/
node_modules /covreporter/
+12 -5
View File
@@ -1,13 +1,20 @@
language: node_js language: node_js
node_js: node_js:
- "4.1" - "0.12.15"
- "4.0" - "4.5"
- "0.12" - "6.5.0"
- "0.11"
- "0.10"
before_install: before_install:
- npm install coveralls - npm install coveralls
- npm install mocha-lcov-reporter - npm install mocha-lcov-reporter
install:
- CXX="g++-4.8" CC="gcc-4.8" npm install
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-4.8
- g++-4.8
script: npm run-script coveralls script: npm run-script coveralls
branches: branches:
only: only:
+60 -40
View File
@@ -1,54 +1,74 @@
# v0.4.1 / 2016-03-31 # Change Log
* Removed length detection in node.js. ## v0.5.0 / 2017-07-14
* Deprecated `buffer` and replace by `arrayBuffer`. ### Added
- support for web worker. #11
# v0.4.0 / 2015-12-28 ### Changed
- throw error if input type is incorrect.
- prevent webpack to require dependencies.
* Added support for update hash. ## v0.4.2 / 2017-01-18
* Added support for bytes array output. ### Fixed
* Added support for ArrayBuffer output. - `root` is undefined in some special environment. #7
* Added support for AMD.
# v0.3.0 / 2015-03-07 ## v0.4.1 / 2016-03-31
### Removed
- length detection in node.js.
### Deprecated
- `buffer` and replace by `arrayBuffer`.
* Support byte Array, Uint8Array and ArrayBuffer input. ## v0.4.0 / 2015-12-28
### Added
- support for update hash.
- support for bytes array output.
- support for ArrayBuffer output.
- support for AMD.
# v0.2.2 / 2015-02-01 ## v0.3.0 / 2015-03-07
### Added
- support byte Array, Uint8Array and ArrayBuffer input.
* Fixed bug when special length. ## v0.2.2 / 2015-02-01
* Improve performance for node.js. ### Fixed
- bug when special length.
### Improve
- performance for node.js.
# v0.2.1 / 2015-01-13 ## v0.2.1 / 2015-01-13
### Improve
- performance.
* Improve performance. ## v0.2.0 / 2015-01-12
### Removed
- ascii parameter.
### Improve
- performance.
# v0.2.0 / 2015-01-12 ## v0.1.4 / 2015-01-11
### Improve
- performance.
### Added
- test cases.
* Remove ascii parameter. ## v0.1.3 / 2015-01-05
* Improve performance. ### Added
- bower package.
- travis.
- coveralls.
### Improved
- performance.
### Fixed
- JSHint warnings.
# v0.1.4 / 2015-01-11 ## v0.1.2 / 2014-07-27
### Fixed
- accents bug
* Improve performance. ## v0.1.1 / 2014-01-05
* Add test case. ### Changed
- license
# v0.1.3 / 2015-01-05 ## v0.1.0 / 2014-01-04
### Added
* Improve performance. - initial release
* Add bower package.
* Fixed JSHint warnings.
* Add travis.
* Add coveralls.
# v0.1.2 / 2014-07-27
Fixed accents bug
# v0.1.1 / 2014-01-05
Update license
# v0.1.0 / 2014-01-04
Initial release
+1 -1
View File
@@ -1,4 +1,4 @@
Copyright 2014-2016 Chen, Yi-Cyuan 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
-4
View File
@@ -67,10 +67,6 @@ md5.arrayBuffer(''); // ArrayBuffer
md5.buffer(''); // ArrayBuffer, deprecated, This maybe confuse with Buffer in node.js. Please use arrayBuffer instead. md5.buffer(''); // ArrayBuffer, deprecated, This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
``` ```
## Benchmark
[UTF8](http://jsperf.com/md5-shootout/81)
[ASCII](http://jsperf.com/md5-shootout/82)
## 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).
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "js-md5", "name": "js-md5",
"version": "0.4.1", "version": "0.5.0",
"main": ["src/md5.js"], "main": ["src/md5.js"],
"ignore": [ "ignore": [
"tests" "tests"
+3 -3
View File
File diff suppressed because one or more lines are too long
+9 -9
View File
@@ -91,7 +91,7 @@
<dt class="tag-source">Source:</dt> <dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li> <dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line149">line 149</a> <a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line162">line 162</a>
</li></ul></dd> </li></ul></dd>
@@ -198,7 +198,7 @@
<dt class="tag-source">Source:</dt> <dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li> <dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line531">line 531</a> <a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line556">line 556</a>
</li></ul></dd> </li></ul></dd>
@@ -314,7 +314,7 @@
<dt class="tag-source">Source:</dt> <dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li> <dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line543">line 543</a> <a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line568">line 568</a>
</li></ul></dd> </li></ul></dd>
@@ -432,7 +432,7 @@
<dt class="tag-source">Source:</dt> <dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li> <dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line565">line 565</a> <a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line590">line 590</a>
</li></ul></dd> </li></ul></dd>
@@ -548,7 +548,7 @@
<dt class="tag-source">Source:</dt> <dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li> <dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line509">line 509</a> <a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line534">line 534</a>
</li></ul></dd> </li></ul></dd>
@@ -664,7 +664,7 @@
<dt class="tag-source">Source:</dt> <dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li> <dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line464">line 464</a> <a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line489">line 489</a>
</li></ul></dd> </li></ul></dd>
@@ -780,7 +780,7 @@
<dt class="tag-source">Source:</dt> <dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li> <dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line497">line 497</a> <a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line522">line 522</a>
</li></ul></dd> </li></ul></dd>
@@ -954,7 +954,7 @@
<dt class="tag-source">Source:</dt> <dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li> <dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line177">line 177</a> <a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line190">line 190</a>
</li></ul></dd> </li></ul></dd>
@@ -1031,7 +1031,7 @@
<br class="clear"> <br class="clear">
<footer> <footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Mar 31 2016 16:24:21 GMT+0800 (CST) Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jul 14 2017 12:58:59 GMT+0800 (CST)
</footer> </footer>
<script> prettyPrint(); </script> <script> prettyPrint(); </script>
+2 -2
View File
@@ -210,7 +210,7 @@
<dt class="tag-source">Source:</dt> <dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li> <dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line583">line 583</a> <a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line608">line 608</a>
</li></ul></dd> </li></ul></dd>
@@ -285,7 +285,7 @@
</div> </div>
<nav> <nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Md5_.html">Md5</a></li></ul><h3>Namespaces</h3><ul><li><a href="md5.html">md5</a></li></ul><h3>Global</h3><ul><li><a href="global.html#md5%2508">md5</a></li></ul> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Md5_.html">Md5</a></li></ul><h3>Namespaces</h3><ul><li><a href="md5.html">md5</a></li></ul><h3>Global</h3><ul><li><a href="global.html#md5%2508">md5</a></li></ul>
+2 -3
View File
@@ -77,8 +77,7 @@ md5.hex(''); // d41d8cd98f00b204e9800998ecf8427e
md5.array(''); // [212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126] md5.array(''); // [212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126]
md5.digest(''); // [212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126] md5.digest(''); // [212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126]
md5.arrayBuffer(''); // ArrayBuffer md5.arrayBuffer(''); // ArrayBuffer
md5.buffer(''); // ArrayBuffer, deprecated, This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.</code></pre><h2>Benchmark</h2><p><a href="http://jsperf.com/md5-shootout/81">UTF8</a><br><a href="http://jsperf.com/md5-shootout/82">ASCII</a></p> md5.buffer(''); // ArrayBuffer, deprecated, This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.</code></pre><h2>License</h2><p>The project is released under the <a href="http://www.opensource.org/licenses/MIT">MIT license</a>.</p>
<h2>License</h2><p>The project is released under the <a href="http://www.opensource.org/licenses/MIT">MIT license</a>.</p>
<h2>Contact</h2><p>The project's website is located at https://github.com/emn178/js-md5<br>Author: Chen, Yi-Cyuan (emn178@gmail.com)</p></article> <h2>Contact</h2><p>The project's website is located at https://github.com/emn178/js-md5<br>Author: Chen, Yi-Cyuan (emn178@gmail.com)</p></article>
</section> </section>
@@ -96,7 +95,7 @@ md5.buffer(''); // ArrayBuffer, deprecated, This maybe confuse with Buffer in no
<br class="clear"> <br class="clear">
<footer> <footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Mar 31 2016 16:24:21 GMT+0800 (CST) Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jul 14 2017 12:58:59 GMT+0800 (CST)
</footer> </footer>
<script> prettyPrint(); </script> <script> prettyPrint(); </script>
+10 -10
View File
@@ -47,7 +47,7 @@
<dt class="tag-version">Version:</dt> <dt class="tag-version">Version:</dt>
<dd class="tag-version"><ul class="dummy"><li>0.4.0</li></ul></dd> <dd class="tag-version"><ul class="dummy"><li>0.5.0</li></ul></dd>
@@ -75,7 +75,7 @@
<dt class="tag-copyright">Copyright:</dt> <dt class="tag-copyright">Copyright:</dt>
<dd class="tag-copyright"><ul class="dummy"><li>Chen, Yi-Cyuan 2014-2016</li></ul></dd> <dd class="tag-copyright"><ul class="dummy"><li>Chen, Yi-Cyuan 2014-2017</li></ul></dd>
@@ -233,7 +233,7 @@
<dt class="tag-source">Source:</dt> <dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li> <dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line52">line 52</a> <a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line67">line 67</a>
</li></ul></dd> </li></ul></dd>
@@ -400,7 +400,7 @@
<dt class="tag-source">Source:</dt> <dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li> <dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line61">line 61</a> <a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line76">line 76</a>
</li></ul></dd> </li></ul></dd>
@@ -569,7 +569,7 @@
<dt class="tag-source">Source:</dt> <dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li> <dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line70">line 70</a> <a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line85">line 85</a>
</li></ul></dd> </li></ul></dd>
@@ -678,7 +678,7 @@
<dt class="tag-source">Source:</dt> <dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li> <dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line86">line 86</a> <a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line101">line 101</a>
</li></ul></dd> </li></ul></dd>
@@ -845,7 +845,7 @@
<dt class="tag-source">Source:</dt> <dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li> <dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line43">line 43</a> <a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line58">line 58</a>
</li></ul></dd> </li></ul></dd>
@@ -1012,7 +1012,7 @@
<dt class="tag-source">Source:</dt> <dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li> <dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line32">line 32</a> <a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line47">line 47</a>
</li></ul></dd> </li></ul></dd>
@@ -1179,7 +1179,7 @@
<dt class="tag-source">Source:</dt> <dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li> <dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line109">line 109</a> <a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line109">line 109</a>
</li></ul></dd> </li></ul></dd>
@@ -1254,7 +1254,7 @@
<nav> <nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Md5_.html">Md5</a></li></ul><h3>Namespaces</h3><ul><li><a href="md5.html">md5</a></li></ul><h3>Global</h3><ul><li><a href="global.html#md5%2508">md5</a></li></ul> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Md5_.html">Md5</a></li></ul><h3>Namespaces</h3><ul><li><a href="md5.html">md5</a></li></ul><h3>Global</h3><ul><li><a href="global.html#md5%2508">md5</a></li></ul>
</nav> </nav>
<br class="clear"> <br class="clear">
+58 -33
View File
@@ -30,21 +30,30 @@
* [js-md5]{@link https://github.com/emn178/js-md5} * [js-md5]{@link https://github.com/emn178/js-md5}
* *
* @namespace md5 * @namespace md5
* @version 0.4.0 * @version 0.5.0
* @author Chen, Yi-Cyuan [emn178@gmail.com] * @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2016 * @copyright Chen, Yi-Cyuan 2014-2017
* @license MIT * @license MIT
*/ */
(function (root) { (function () {
'use strict'; 'use strict';
var NODE_JS = typeof process == 'object' &amp;&amp; process.versions &amp;&amp; process.versions.node; var ERROR = 'input is invalid type';
var WINDOW = typeof window === 'object';
var root = WINDOW ? window : {};
if (root.JS_MD5_NO_WINDOW) {
WINDOW = false;
}
var WEB_WORKER = !WINDOW &amp;&amp; typeof self === 'object';
var NODE_JS = !root.JS_MD5_NO_NODE_JS &amp;&amp; typeof process === 'object' &amp;&amp; process.versions &amp;&amp; process.versions.node;
if (NODE_JS) { if (NODE_JS) {
root = global; root = global;
} else if (WEB_WORKER) {
root = self;
} }
var COMMON_JS = !root.JS_MD5_TEST &amp;&amp; typeof module == 'object' &amp;&amp; module.exports; var COMMON_JS = !root.JS_MD5_NO_COMMON_JS &amp;&amp; typeof module === 'object' &amp;&amp; module.exports;
var AMD = typeof define == 'function' &amp;&amp; define.amd; var AMD = typeof define === 'function' &amp;&amp; define.amd;
var ARRAY_BUFFER = !root.JS_MD5_TEST &amp;&amp; typeof ArrayBuffer != 'undefined'; var ARRAY_BUFFER = !root.JS_MD5_NO_ARRAY_BUFFER &amp;&amp; typeof ArrayBuffer !== 'undefined';
var HEX_CHARS = '0123456789abcdef'.split(''); var HEX_CHARS = '0123456789abcdef'.split('');
var EXTRA = [128, 32768, 8388608, -2147483648]; var EXTRA = [128, 32768, 8388608, -2147483648];
var SHIFT = [0, 8, 16, 24]; var SHIFT = [0, 8, 16, 24];
@@ -57,6 +66,12 @@
blocks = new Uint32Array(buffer); blocks = new Uint32Array(buffer);
} }
if (root.JS_MD5_NO_NODE_JS || !Array.isArray) {
Array.isArray = function (obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
};
}
/** /**
* @method hex * @method hex
* @memberof md5 * @memberof md5
@@ -142,7 +157,7 @@
method.update = function (message) { method.update = function (message) {
return method.create().update(message); return method.create().update(message);
}; };
for (var i = 0;i &lt; OUTPUT_TYPES.length;++i) { for (var i = 0; i &lt; OUTPUT_TYPES.length; ++i) {
var type = OUTPUT_TYPES[i]; var type = OUTPUT_TYPES[i];
method[type] = createOutputMethod(type); method[type] = createOutputMethod(type);
} }
@@ -150,26 +165,24 @@
}; };
var nodeWrap = function (method) { var nodeWrap = function (method) {
var crypto, Buffer; var crypto = eval("require('crypto')");
try { var Buffer = eval("require('buffer').Buffer");
if (root.JS_MD5_TEST) {
throw 'JS_MD5_TEST';
}
crypto = require('crypto');
Buffer = require('buffer').Buffer;
} catch (e) {
console.log(e);
return method;
}
var nodeMethod = function (message) { var nodeMethod = function (message) {
if (typeof message == 'string') { if (typeof message === 'string') {
return crypto.createHash('md5').update(message, 'utf8').digest('hex'); return crypto.createHash('md5').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 ERROR;
} else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
}
}
if (Array.isArray(message) || ArrayBuffer.isView(message) ||
message.constructor === Buffer) {
return crypto.createHash('md5').update(new Buffer(message)).digest('hex');
} else {
return method(message); return method(message);
} }
return crypto.createHash('md5').update(new Buffer(message)).digest('hex');
}; };
return nodeMethod; return nodeMethod;
}; };
@@ -216,10 +229,22 @@
return; return;
} }
var notString = typeof(message) != 'string'; var notString = typeof(message) != 'string';
if (notString &amp;&amp; message.constructor == root.ArrayBuffer) { if (notString) {
message = new Uint8Array(message); if (message === null || message === undefined) {
throw ERROR;
} else if (message.constructor === root.ArrayBuffer) {
message = new Uint8Array(message);
}
} }
var code, index = 0, i, length = message.length || 0, blocks = this.blocks; var length = message.length;
if (notString) {
if (typeof length !== 'number' ||
!Array.isArray(message) &amp;&amp;
!(ARRAY_BUFFER &amp;&amp; ArrayBuffer.isView(message))) {
throw ERROR;
}
}
var code, index = 0, i, blocks = this.blocks;
var buffer8 = this.buffer8; var buffer8 = this.buffer8;
while (index &lt; length) { while (index &lt; length) {
@@ -234,17 +259,17 @@
if (notString) { if (notString) {
if (ARRAY_BUFFER) { if (ARRAY_BUFFER) {
for (i = this.start;index &lt; length &amp;&amp; i &lt; 64; ++index) { for (i = this.start; index &lt; length &amp;&amp; i &lt; 64; ++index) {
buffer8[i++] = message[index]; buffer8[i++] = message[index];
} }
} else { } else {
for (i = this.start;index &lt; length &amp;&amp; i &lt; 64; ++index) { for (i = this.start; index &lt; length &amp;&amp; i &lt; 64; ++index) {
blocks[i >> 2] |= message[index] &lt;&lt; SHIFT[i++ &amp; 3]; blocks[i >> 2] |= message[index] &lt;&lt; SHIFT[i++ &amp; 3];
} }
} }
} else { } else {
if (ARRAY_BUFFER) { if (ARRAY_BUFFER) {
for (i = this.start;index &lt; length &amp;&amp; i &lt; 64; ++index) { for (i = this.start; index &lt; length &amp;&amp; i &lt; 64; ++index) {
code = message.charCodeAt(index); code = message.charCodeAt(index);
if (code &lt; 0x80) { if (code &lt; 0x80) {
buffer8[i++] = code; buffer8[i++] = code;
@@ -264,7 +289,7 @@
} }
} }
} else { } else {
for (i = this.start;index &lt; length &amp;&amp; i &lt; 64; ++index) { for (i = this.start; index &lt; length &amp;&amp; i &lt; 64; ++index) {
code = message.charCodeAt(index); code = message.charCodeAt(index);
if (code &lt; 0x80) { if (code &lt; 0x80) {
blocks[i >> 2] |= code &lt;&lt; SHIFT[i++ &amp; 3]; blocks[i >> 2] |= code &lt;&lt; SHIFT[i++ &amp; 3];
@@ -632,7 +657,7 @@
}); });
} }
} }
}(this)); })();
</code></pre> </code></pre>
</article> </article>
</section> </section>
@@ -649,7 +674,7 @@
<br class="clear"> <br class="clear">
<footer> <footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Mar 31 2016 16:24:20 GMT+0800 (CST) Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jul 14 2017 12:58:59 GMT+0800 (CST)
</footer> </footer>
<script> prettyPrint(); </script> <script> prettyPrint(); </script>
+34 -33
View File
@@ -153,7 +153,7 @@ h1
margin: 12px 24px 20px; margin: 12px 24px 20px;
} }
h2, h3 h2, h3.subsection-title
{ {
font-size: 30px; font-size: 30px;
font-weight: 700; font-weight: 700;
@@ -161,6 +161,13 @@ h2, h3
margin-bottom: 12px; margin-bottom: 12px;
} }
h3
{
font-size: 24px;
letter-spacing: -0.5px;
margin-bottom: 12px;
}
h4 h4
{ {
font-size: 18px; font-size: 18px;
@@ -185,6 +192,32 @@ h6
font-style: italic; font-style: italic;
} }
table
{
border-spacing: 0;
border: 0;
border-collapse: collapse;
}
td, th
{
border: 1px solid #ddd;
margin: 0px;
text-align: left;
vertical-align: top;
padding: 4px 6px;
display: table-cell;
}
thead tr
{
background-color: #ddd;
font-weight: bold;
}
th { border-right: 1px solid #aaa; }
tr > th:last-child { border-right: 1px solid #ddd; }
.ancestors { color: #999; } .ancestors { color: #999; }
.ancestors a .ancestors a
{ {
@@ -296,44 +329,12 @@ h6
user-select: text; user-select: text;
} }
.params, .props
{
border-spacing: 0;
border: 0;
border-collapse: collapse;
}
.params .name, .props .name, .name code { .params .name, .props .name, .name code {
color: #4D4E53; color: #4D4E53;
font-family: Consolas, Monaco, 'Andale Mono', monospace; font-family: Consolas, Monaco, 'Andale Mono', monospace;
font-size: 100%; font-size: 100%;
} }
.params td, .params th, .props td, .props th
{
border: 1px solid #ddd;
margin: 0px;
text-align: left;
vertical-align: top;
padding: 4px 6px;
display: table-cell;
}
.params thead tr, .props thead tr
{
background-color: #ddd;
font-weight: bold;
}
.params .params thead tr, .props .props thead tr
{
background-color: #fff;
font-weight: bold;
}
.params th, .props th { border-right: 1px solid #aaa; }
.params thead .last, .props thead .last { border-right: 1px solid #ddd; }
.params td.description > p:first-child, .params td.description > p:first-child,
.props td.description > p:first-child .props td.description > p:first-child
{ {
+3 -2
View File
@@ -1,6 +1,6 @@
{ {
"name": "js-md5", "name": "js-md5",
"version": "0.4.1", "version": "0.5.0",
"description": "A simple MD5 hash function for JavaScript supports UTF-8 encoding.", "description": "A simple MD5 hash function for JavaScript supports UTF-8 encoding.",
"main": "src/md5.js", "main": "src/md5.js",
"devDependencies": { "devDependencies": {
@@ -9,7 +9,8 @@
"jsdoc": "^3.4.0", "jsdoc": "^3.4.0",
"mocha": "~2.3.4", "mocha": "~2.3.4",
"requirejs": "^2.1.22", "requirejs": "^2.1.22",
"uglifyjs": "~2.4.10" "uglifyjs": "~2.4.10",
"webworker-threads": "^0.7.11"
}, },
"scripts": { "scripts": {
"test": "mocha tests/node-test.js -r jscoverage", "test": "mocha tests/node-test.js -r jscoverage",
+57 -32
View File
@@ -2,21 +2,30 @@
* [js-md5]{@link https://github.com/emn178/js-md5} * [js-md5]{@link https://github.com/emn178/js-md5}
* *
* @namespace md5 * @namespace md5
* @version 0.4.0 * @version 0.5.0
* @author Chen, Yi-Cyuan [emn178@gmail.com] * @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2016 * @copyright Chen, Yi-Cyuan 2014-2017
* @license MIT * @license MIT
*/ */
(function (root) { (function () {
'use strict'; 'use strict';
var NODE_JS = typeof process == 'object' && process.versions && process.versions.node; var ERROR = 'input is invalid type';
var WINDOW = typeof window === 'object';
var root = WINDOW ? window : {};
if (root.JS_MD5_NO_WINDOW) {
WINDOW = false;
}
var WEB_WORKER = !WINDOW && typeof self === 'object';
var NODE_JS = !root.JS_MD5_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_MD5_TEST && typeof module == 'object' && module.exports; var COMMON_JS = !root.JS_MD5_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_MD5_TEST && typeof ArrayBuffer != 'undefined'; var ARRAY_BUFFER = !root.JS_MD5_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
var HEX_CHARS = '0123456789abcdef'.split(''); var HEX_CHARS = '0123456789abcdef'.split('');
var EXTRA = [128, 32768, 8388608, -2147483648]; var EXTRA = [128, 32768, 8388608, -2147483648];
var SHIFT = [0, 8, 16, 24]; var SHIFT = [0, 8, 16, 24];
@@ -29,6 +38,12 @@
blocks = new Uint32Array(buffer); blocks = new Uint32Array(buffer);
} }
if (root.JS_MD5_NO_NODE_JS || !Array.isArray) {
Array.isArray = function (obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
};
}
/** /**
* @method hex * @method hex
* @memberof md5 * @memberof md5
@@ -114,7 +129,7 @@
method.update = function (message) { method.update = function (message) {
return method.create().update(message); return method.create().update(message);
}; };
for (var i = 0;i < OUTPUT_TYPES.length;++i) { for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
var type = OUTPUT_TYPES[i]; var type = OUTPUT_TYPES[i];
method[type] = createOutputMethod(type); method[type] = createOutputMethod(type);
} }
@@ -122,26 +137,24 @@
}; };
var nodeWrap = function (method) { var nodeWrap = function (method) {
var crypto, Buffer; var crypto = eval("require('crypto')");
try { var Buffer = eval("require('buffer').Buffer");
if (root.JS_MD5_TEST) {
throw 'JS_MD5_TEST';
}
crypto = require('crypto');
Buffer = require('buffer').Buffer;
} catch (e) {
console.log(e);
return method;
}
var nodeMethod = function (message) { var nodeMethod = function (message) {
if (typeof message == 'string') { if (typeof message === 'string') {
return crypto.createHash('md5').update(message, 'utf8').digest('hex'); return crypto.createHash('md5').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 ERROR;
} else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
}
}
if (Array.isArray(message) || ArrayBuffer.isView(message) ||
message.constructor === Buffer) {
return crypto.createHash('md5').update(new Buffer(message)).digest('hex');
} else {
return method(message); return method(message);
} }
return crypto.createHash('md5').update(new Buffer(message)).digest('hex');
}; };
return nodeMethod; return nodeMethod;
}; };
@@ -188,10 +201,22 @@
return; return;
} }
var notString = typeof(message) != 'string'; var notString = typeof(message) != 'string';
if (notString && message.constructor == root.ArrayBuffer) { if (notString) {
message = new Uint8Array(message); if (message === null || message === undefined) {
throw ERROR;
} else if (message.constructor === root.ArrayBuffer) {
message = new Uint8Array(message);
}
} }
var code, index = 0, i, length = message.length || 0, blocks = this.blocks; var length = message.length;
if (notString) {
if (typeof length !== 'number' ||
!Array.isArray(message) &&
!(ARRAY_BUFFER && ArrayBuffer.isView(message))) {
throw ERROR;
}
}
var code, index = 0, i, blocks = this.blocks;
var buffer8 = this.buffer8; var buffer8 = this.buffer8;
while (index < length) { while (index < length) {
@@ -206,17 +231,17 @@
if (notString) { if (notString) {
if (ARRAY_BUFFER) { if (ARRAY_BUFFER) {
for (i = this.start;index < length && i < 64; ++index) { for (i = this.start; index < length && i < 64; ++index) {
buffer8[i++] = message[index]; buffer8[i++] = message[index];
} }
} else { } else {
for (i = this.start;index < length && i < 64; ++index) { for (i = this.start; index < length && i < 64; ++index) {
blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
} }
} }
} else { } else {
if (ARRAY_BUFFER) { if (ARRAY_BUFFER) {
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) {
buffer8[i++] = code; buffer8[i++] = code;
@@ -236,7 +261,7 @@
} }
} }
} else { } 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];
@@ -604,4 +629,4 @@
}); });
} }
} }
}(this)); })();
+79 -2
View File
@@ -1,12 +1,31 @@
md5 = require('../src/md5.js');
expect = require('expect.js'); expect = require('expect.js');
Worker = require('webworker-threads').Worker;
// Node.js env
BUFFER = true;
md5 = require('../src/md5.js');
require('./test.js'); require('./test.js');
delete require.cache[require.resolve('../src/md5.js')] delete require.cache[require.resolve('../src/md5.js')]
delete require.cache[require.resolve('./test.js')] delete require.cache[require.resolve('./test.js')]
md5 = null md5 = null
JS_MD5_TEST = true; // Webpack browser env
JS_MD5_NO_NODE_JS = true;
BUFFER = false;
window = global;
md5 = require('../src/md5.js');
require('./test.js');
delete require.cache[require.resolve('../src/md5.js')];
delete require.cache[require.resolve('./test.js')];
md5 = null;
// browser env
JS_MD5_NO_NODE_JS = true;
JS_MD5_NO_COMMON_JS = true;
BUFFER = false;
window = global;
require('../src/md5.js'); require('../src/md5.js');
require('./test.js'); require('./test.js');
@@ -14,6 +33,25 @@ delete require.cache[require.resolve('../src/md5.js')];
delete require.cache[require.resolve('./test.js')]; delete require.cache[require.resolve('./test.js')];
md5 = null; md5 = null;
// browser env and no array buffer
JS_MD5_NO_NODE_JS = true;
JS_MD5_NO_COMMON_JS = true;
JS_MD5_NO_ARRAY_BUFFER = true;
BUFFER = false;
window = global;
require('../src/md5.js');
require('./test.js');
delete require.cache[require.resolve('../src/md5.js')];
delete require.cache[require.resolve('./test.js')];
md5 = null;
// browser AMD
JS_MD5_NO_NODE_JS = true;
JS_MD5_NO_COMMON_JS = true;
JS_MD5_NO_ARRAY_BUFFER = false;
BUFFER = false;
window = global;
define = function (func) { define = function (func) {
md5 = func(); md5 = func();
require('./test.js'); require('./test.js');
@@ -21,3 +59,42 @@ define = function (func) {
define.amd = true; define.amd = true;
require('../src/md5.js'); require('../src/md5.js');
delete require.cache[require.resolve('../src/md5.js')];
delete require.cache[require.resolve('./test.js')];
md5 = null;
// webworker
WORKER = 'tests/worker.js';
SOURCE = 'src/md5.js';
require('./worker-test.js');
// cover webworker
JS_MD5_NO_WINDOW = true;
JS_MD5_NO_NODE_JS = true;
JS_MD5_NO_COMMON_JS = false;
JS_MD5_NO_ARRAY_BUFFER = false;
BUFFER = false;
WORKER = './worker.js';
SOURCE = '../src/md5.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 () {};
md5 = require('../src/md5.js');
require('./worker-test.js');
+46 -33
View File
@@ -2,7 +2,7 @@
Array.prototype.toHexString = ArrayBuffer.prototype.toHexString = function () { Array.prototype.toHexString = ArrayBuffer.prototype.toHexString = function () {
var array = new Uint8Array(this); var array = new Uint8Array(this);
var hex = ''; var hex = '';
for (var i = 0;i < array.length;++i) { for (var i = 0; i < array.length; ++i) {
var c = array[i].toString('16'); var c = array[i].toString('16');
hex += c.length == 1 ? '0' + c : c; hex += c.length == 1 ? '0' + c : c;
} }
@@ -42,29 +42,30 @@
'd41d8cd98f00b204e9800998ecf8427e': [], 'd41d8cd98f00b204e9800998ecf8427e': [],
'93b885adfe0da089cdf634904fd59f71': [0], '93b885adfe0da089cdf634904fd59f71': [0],
'9e107d9d372bb6826bd81d3542a419d6': [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] '9e107d9d372bb6826bd81d3542a419d6': [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]
},
'Uint8Array': {
'9e107d9d372bb6826bd81d3542a419d6': 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': {
'9e107d9d372bb6826bd81d3542a419d6': 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': {
'93b885adfe0da089cdf634904fd59f71': new ArrayBuffer(1)
},
'Object': {
'd41d8cd98f00b204e9800998ecf8427e': {},
'd41d8cd98f00b204e9800998ecf8427e': {what: 'ever'}
} }
}; };
if (typeof process == 'object') { if (!(typeof JS_MD5_NO_ARRAY_BUFFER === 'boolean' && JS_MD5_NO_ARRAY_BUFFER)) {
testCases['Uint8Array'] = {
'9e107d9d372bb6826bd81d3542a419d6': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])
};
testCases['Int8Array'] = {
'9e107d9d372bb6826bd81d3542a419d6': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])
};
testCases['ArrayBuffer'] = {
'93b885adfe0da089cdf634904fd59f71': new ArrayBuffer(1)
};
}
if (typeof BUFFER === 'boolean' && BUFFER) {
testCases['Buffer'] = { testCases['Buffer'] = {
'd41d8cd98f00b204e9800998ecf8427e': new Buffer(0), 'd41d8cd98f00b204e9800998ecf8427e': new Buffer(0),
'9e107d9d372bb6826bd81d3542a419d6': 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])) '9e107d9d372bb6826bd81d3542a419d6': 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 errorTestCases = [null, undefined, { length: 0 }, 0, 1, false, true, NaN, Infinity, function () {}];
var methods = [ var methods = [
{ {
name: 'md5', name: 'md5',
@@ -154,26 +155,26 @@
} }
]; ];
methods.forEach(function (method) { describe('md5', 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('Md5', 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) {
@@ -192,5 +193,17 @@
} }
}); });
}); });
describe('#md5', function () {
errorTestCases.forEach(function (testCase) {
context('when ' + testCase, function () {
it('should throw error', function () {
expect(function () {
md5(testCase);
}).to.throwError(/input is invalid type/);
});
});
});
});
}); });
})(md5); })(md5);
+21
View File
@@ -0,0 +1,21 @@
(function () {
var cases = {
'd41d8cd98f00b204e9800998ecf8427e': '',
'9e107d9d372bb6826bd81d3542a419d6': 'The quick brown fox jumps over the lazy dog',
'e4d909c290d0fb1ca068ffaddf22cbd0': 'The quick brown fox jumps over the lazy dog.'
};
describe('#md5', 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);
done();
};
worker.postMessage(SOURCE);
worker.postMessage(cases[hash]);
});
});
});
})();
+26
View File
@@ -0,0 +1,26 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MD5</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/md5.js"></script>
</head>
<body>
<div id="mocha"></div>
<script>
WORKER = 'worker.js';
SOURCE = '../src/md5.js';
mocha.setup('bdd');
</script>
<script src="worker-test.js"></script>
<script>
mocha.checkLeaks();
mocha.run();
</script>
<script>
</script>
</body>
</html>
+12
View File
@@ -0,0 +1,12 @@
var imported = false;
onmessage = function(e) {
if (imported) {
postMessage(md5(e.data));
if (typeof exports !== 'undefined') {
imported = false;
}
} else {
imported = true;
importScripts(e.data);
}
}