Skip to content

Commit

Permalink
Accept Uint8Array
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Sep 14, 2018
1 parent 4de642a commit 902fcbc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ npm install md5
md5(message)
~~~

* `message` -- `String` or `Buffer`
* `message` -- `String`, `Buffer`, `Array` or `Uint8Array`
* returns `String`


Expand Down
2 changes: 1 addition & 1 deletion md5.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
message = utf8.stringToBytes(message);
else if (isBuffer(message))
message = Array.prototype.slice.call(message, 0);
else if (!Array.isArray(message))
else if (!Array.isArray(message) && message.constructor !== Uint8Array)
message = message.toString();
// else, assume byte array already

Expand Down
26 changes: 26 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,30 @@ describe('md5', function () {
var hash3 = md5(hash1 + 'a', { encoding : 'binary' });
assert.equal(hash3, '131f0ac52813044f5110e4aec638c169');
});

it('should support Uint8Array', function() {
// Polyfills
if (!Array.from) {
Array.from = function(src, fn) {
var result = new Array(src.length);
for (var i = 0; i < src.length; ++i)
result[i] = fn(src[i]);
return result;
};
}
if (!Uint8Array.from) {
Uint8Array.from = function(src) {
var result = new Uint8Array(src.length);
for (var i = 0; i < src.length; ++i)
result[i] = src[i];
return result;
};
}

var message = 'foobarbaz';
var u8arr = Uint8Array.from(
Array.from(message, function(c) { return c.charCodeAt(0); }));
var u8aHash = md5(u8arr);
assert.equal(u8aHash, md5(message));
});
});

0 comments on commit 902fcbc

Please sign in to comment.