Added missing arrayBuffer method.

master v0.3.2
Chen, Yi-Cyuan 9 years ago
parent 06519f17db
commit 309beb285b

@ -1,8 +1,13 @@
# Change Log # Change Log
## v0.3.2 / 2017-01-24
### Added
- missing arrayBuffer method.
## v0.3.1 / 2017-01-23 ## v0.3.1 / 2017-01-23
### Fixed ### Fixed
- ArrayBuffer dosen't work in Webpack. - ArrayBuffer dosen't work in Webpack.
### Deprecated ### Deprecated
- `buffer` maybe confuse with Buffer in node.js. Please use `arrayBuffer` instead. - `buffer` maybe confuse with Buffer in node.js. Please use `arrayBuffer` instead.

@ -27,9 +27,14 @@ For node.js, you can use this command to install:
You could use like this: You could use like this:
```JavaScript ```JavaScript
md4('Message to hash'); md4('Message to hash');
var hash = md4.create(); var hash = md4.create();
hash.update('Message to hash'); hash.update('Message to hash');
hash.hex(); hash.hex();
var hash2 = md4.update('Message to hash');
hash2.update('Message2 to hash');
hash2.array();
``` ```
If you use node.js, you should require the module first: If you use node.js, you should require the module first:
```JavaScript ```JavaScript

@ -1,6 +1,6 @@
{ {
"name": "js-md4", "name": "js-md4",
"version": "0.3.1", "version": "0.3.2",
"main": ["src/md4.js"], "main": ["src/md4.js"],
"ignore": [ "ignore": [
"samples", "samples",

4
build/md4.min.js vendored

File diff suppressed because one or more lines are too long

@ -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="md4.js.html">md4.js</a>, <a href="md4.js.html#line114">line 114</a> <a href="md4.js.html">md4.js</a>, <a href="md4.js.html#line132">line 132</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="md4.js.html">md4.js</a>, <a href="md4.js.html#line480">line 480</a> <a href="md4.js.html">md4.js</a>, <a href="md4.js.html#line498">line 498</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="md4.js.html">md4.js</a>, <a href="md4.js.html#line492">line 492</a> <a href="md4.js.html">md4.js</a>, <a href="md4.js.html#line510">line 510</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="md4.js.html">md4.js</a>, <a href="md4.js.html#line514">line 514</a> <a href="md4.js.html">md4.js</a>, <a href="md4.js.html#line532">line 532</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="md4.js.html">md4.js</a>, <a href="md4.js.html#line458">line 458</a> <a href="md4.js.html">md4.js</a>, <a href="md4.js.html#line476">line 476</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="md4.js.html">md4.js</a>, <a href="md4.js.html#line413">line 413</a> <a href="md4.js.html">md4.js</a>, <a href="md4.js.html#line431">line 431</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="md4.js.html">md4.js</a>, <a href="md4.js.html#line446">line 446</a> <a href="md4.js.html">md4.js</a>, <a href="md4.js.html#line464">line 464</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="md4.js.html">md4.js</a>, <a href="md4.js.html#line142">line 142</a> <a href="md4.js.html">md4.js</a>, <a href="md4.js.html#line160">line 160</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.3</a> on Mon Jan 23 2017 15:12:38 GMT+0800 (CST) Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Tue Jan 24 2017 15:15:12 GMT+0800 (CST)
</footer> </footer>
<script> prettyPrint(); </script> <script> prettyPrint(); </script>

@ -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="md4.js.html">md4.js</a>, <a href="md4.js.html#line532">line 532</a> <a href="md4.js.html">md4.js</a>, <a href="md4.js.html#line550">line 550</a>
</li></ul></dd> </li></ul></dd>
@ -285,7 +285,7 @@
<br class="clear"> <br class="clear">
<footer> <footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jan 23 2017 15:12:38 GMT+0800 (CST) Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Tue Jan 24 2017 15:15:12 GMT+0800 (CST)
</footer> </footer>
<script> prettyPrint(); </script> <script> prettyPrint(); </script>

@ -52,9 +52,14 @@
<pre class="prettyprint source"><code>npm install js-md4</code></pre><h2>Notice</h2><p><code>buffer</code> method is deprecated. This maybe confuse with Buffer in node.js. Please use <code>arrayBuffer</code> instead.</p> <pre class="prettyprint source"><code>npm install js-md4</code></pre><h2>Notice</h2><p><code>buffer</code> method is deprecated. This maybe confuse with Buffer in node.js. Please use <code>arrayBuffer</code> instead.</p>
<h2>Usage</h2><p>You could use like this:</p> <h2>Usage</h2><p>You could use like this:</p>
<pre class="prettyprint source lang-JavaScript"><code>md4('Message to hash'); <pre class="prettyprint source lang-JavaScript"><code>md4('Message to hash');
var hash = md4.create(); var hash = md4.create();
hash.update('Message to hash'); hash.update('Message to hash');
hash.hex();</code></pre><p>If you use node.js, you should require the module first:</p> hash.hex();
var hash2 = md4.update('Message to hash');
hash2.update('Message2 to hash');
hash2.array();</code></pre><p>If you use node.js, you should require the module first:</p>
<pre class="prettyprint source lang-JavaScript"><code>var md4 = require('js-md4');</code></pre><p>It supports AMD:</p> <pre class="prettyprint source lang-JavaScript"><code>var md4 = require('js-md4');</code></pre><p>It supports AMD:</p>
<pre class="prettyprint source lang-JavaScript"><code>require(['your/path/md4.js'], function (md4) { <pre class="prettyprint source lang-JavaScript"><code>require(['your/path/md4.js'], function (md4) {
// ... // ...
@ -93,7 +98,7 @@ md4.arrayBuffer(''); // ArrayBuffer</code></pre><h2>License</h2><p>The project i
<br class="clear"> <br class="clear">
<footer> <footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jan 23 2017 15:12:38 GMT+0800 (CST) Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Tue Jan 24 2017 15:15:12 GMT+0800 (CST)
</footer> </footer>
<script> prettyPrint(); </script> <script> prettyPrint(); </script>

@ -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.3.1</li></ul></dd> <dd class="tag-version"><ul class="dummy"><li>0.3.2</li></ul></dd>
@ -1085,7 +1085,7 @@
<br class="clear"> <br class="clear">
<footer> <footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jan 23 2017 15:12:38 GMT+0800 (CST) Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Tue Jan 24 2017 15:15:12 GMT+0800 (CST)
</footer> </footer>
<script> prettyPrint(); </script> <script> prettyPrint(); </script>

@ -30,7 +30,7 @@
* [js-md4]{@link https://github.com/emn178/js-md4} * [js-md4]{@link https://github.com/emn178/js-md4}
* *
* @namespace md4 * @namespace md4
* @version 0.3.1 * @version 0.3.2
* @author Yi-Cyuan Chen [emn178@gmail.com] * @author Yi-Cyuan Chen [emn178@gmail.com]
* @copyright Yi-Cyuan Chen 2015-2027 * @copyright Yi-Cyuan Chen 2015-2027
* @license MIT * @license MIT
@ -50,7 +50,7 @@
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];
var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer']; var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer', 'arrayBuffer'];
var blocks = [], buffer8; var blocks = [], buffer8;
if (ARRAY_BUFFER) { if (ARRAY_BUFFER) {
@ -125,6 +125,9 @@
*/ */
var createMethod = function () { var createMethod = function () {
var method = createOutputMethod('hex'); var method = createOutputMethod('hex');
if (NODE_JS) {
method = nodeWrap(method);
}
method.create = function () { method.create = function () {
return new Md4(); return new Md4();
}; };
@ -138,6 +141,21 @@
return method; return method;
}; };
var nodeWrap = function (method) {
var crypto = require('crypto');
var Buffer = require('buffer').Buffer;
var nodeMethod = function (message) {
if (typeof message === 'string') {
return crypto.createHash('md4').update(message, 'utf8').digest('hex');
} else if (ARRAY_BUFFER &amp;&amp; message instanceof ArrayBuffer) {
message = new Uint8Array(message);
} else if (message.length === undefined) {
return method(message);
}
return crypto.createHash('md4').update(new Buffer(message)).digest('hex');
};
return nodeMethod;
};
/** /**
* Md4 class * Md4 class
@ -181,7 +199,7 @@
return; return;
} }
var notString = typeof message !== 'string'; var notString = typeof message !== 'string';
if(notString &amp;&amp; ARRAY_BUFFER &amp;&amp; message instanceof ArrayBuffer) { if (notString &amp;&amp; ARRAY_BUFFER &amp;&amp; message instanceof ArrayBuffer) {
message = new Uint8Array(message); message = new Uint8Array(message);
} }
var code, index = 0, i, length = message.length || 0, blocks = this.blocks; var code, index = 0, i, length = message.length || 0, blocks = this.blocks;
@ -598,7 +616,7 @@
<br class="clear"> <br class="clear">
<footer> <footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Mon Jan 23 2017 15:12:38 GMT+0800 (CST) Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Tue Jan 24 2017 15:15:12 GMT+0800 (CST)
</footer> </footer>
<script> prettyPrint(); </script> <script> prettyPrint(); </script>

@ -1,6 +1,6 @@
{ {
"name": "js-md4", "name": "js-md4",
"version": "0.3.1", "version": "0.3.2",
"description": "A simple MD4 hash function for JavaScript supports UTF-8 encoding.", "description": "A simple MD4 hash function for JavaScript supports UTF-8 encoding.",
"main": "src/md4.js", "main": "src/md4.js",
"devDependencies": { "devDependencies": {

@ -2,7 +2,7 @@
* [js-md4]{@link https://github.com/emn178/js-md4} * [js-md4]{@link https://github.com/emn178/js-md4}
* *
* @namespace md4 * @namespace md4
* @version 0.3.1 * @version 0.3.2
* @author Yi-Cyuan Chen [emn178@gmail.com] * @author Yi-Cyuan Chen [emn178@gmail.com]
* @copyright Yi-Cyuan Chen 2015-2027 * @copyright Yi-Cyuan Chen 2015-2027
* @license MIT * @license MIT
@ -22,7 +22,7 @@
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];
var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer']; var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer', 'arrayBuffer'];
var blocks = [], buffer8; var blocks = [], buffer8;
if (ARRAY_BUFFER) { if (ARRAY_BUFFER) {
@ -97,6 +97,9 @@
*/ */
var createMethod = function () { var createMethod = function () {
var method = createOutputMethod('hex'); var method = createOutputMethod('hex');
if (NODE_JS) {
method = nodeWrap(method);
}
method.create = function () { method.create = function () {
return new Md4(); return new Md4();
}; };
@ -110,6 +113,21 @@
return method; return method;
}; };
var nodeWrap = function (method) {
var crypto = require('crypto');
var Buffer = require('buffer').Buffer;
var nodeMethod = function (message) {
if (typeof message === 'string') {
return crypto.createHash('md4').update(message, 'utf8').digest('hex');
} else if (ARRAY_BUFFER && message instanceof ArrayBuffer) {
message = new Uint8Array(message);
} else if (message.length === undefined) {
return method(message);
}
return crypto.createHash('md4').update(new Buffer(message)).digest('hex');
};
return nodeMethod;
};
/** /**
* Md4 class * Md4 class
@ -153,7 +171,7 @@
return; return;
} }
var notString = typeof message !== 'string'; var notString = typeof message !== 'string';
if(notString && ARRAY_BUFFER && message instanceof ArrayBuffer) { if (notString && ARRAY_BUFFER && message instanceof ArrayBuffer) {
message = new Uint8Array(message); message = new Uint8Array(message);
} }
var code, index = 0, i, length = message.length || 0, blocks = this.blocks; var code, index = 0, i, length = message.length || 0, blocks = this.blocks;

@ -4,7 +4,7 @@
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;
} }
return hex; return hex;
}; };
@ -141,26 +141,26 @@
delete testCases['ArrayBuffer']; delete testCases['ArrayBuffer'];
} }
methods.forEach(function (method) { describe('Md4', 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('Md4', 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) {

Loading…
Cancel
Save