File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ npm install md5
2121md5 (message)
2222~~~
2323
24- * ` message ` -- ` String ` or ` Buffer `
24+ * ` message ` -- ` String ` , ` Buffer ` , ` Array ` or ` Uint8Array `
2525 * returns ` String `
2626
2727
Original file line number Diff line number Diff line change 1414 message = utf8 . stringToBytes ( message ) ;
1515 else if ( isBuffer ( message ) )
1616 message = Array . prototype . slice . call ( message , 0 ) ;
17- else if ( ! Array . isArray ( message ) )
17+ else if ( ! Array . isArray ( message ) && message . constructor !== Uint8Array )
1818 message = message . toString ( ) ;
1919 // else, assume byte array already
2020
Original file line number Diff line number Diff line change @@ -46,4 +46,30 @@ describe('md5', function () {
4646 var hash3 = md5 ( hash1 + 'a' , { encoding : 'binary' } ) ;
4747 assert . equal ( hash3 , '131f0ac52813044f5110e4aec638c169' ) ;
4848 } ) ;
49+
50+ it ( 'should support Uint8Array' , function ( ) {
51+ // Polyfills
52+ if ( ! Array . from ) {
53+ Array . from = function ( src , fn ) {
54+ var result = new Array ( src . length ) ;
55+ for ( var i = 0 ; i < src . length ; ++ i )
56+ result [ i ] = fn ( src [ i ] ) ;
57+ return result ;
58+ } ;
59+ }
60+ if ( ! Uint8Array . from ) {
61+ Uint8Array . from = function ( src ) {
62+ var result = new Uint8Array ( src . length ) ;
63+ for ( var i = 0 ; i < src . length ; ++ i )
64+ result [ i ] = src [ i ] ;
65+ return result ;
66+ } ;
67+ }
68+
69+ var message = 'foobarbaz' ;
70+ var u8arr = Uint8Array . from (
71+ Array . from ( message , function ( c ) { return c . charCodeAt ( 0 ) ; } ) ) ;
72+ var u8aHash = md5 ( u8arr ) ;
73+ assert . equal ( u8aHash , md5 ( message ) ) ;
74+ } ) ;
4975} ) ;
You can’t perform that action at this time.
0 commit comments