satazor / js-spark-md5

Lightning fast normal and incremental md5 for javascript
Do What The F*ck You Want To Public License
2.46k stars 470 forks source link

fix: hashing empty buffer #37

Closed zhuangya closed 8 years ago

zhuangya commented 8 years ago

i.e. a zero byte file.

zhuangya commented 8 years ago

in node.js environment, if read a empty file or new Buffer('')

spark-md5 will crash, throw this error:

TypeError: s.substring is not a function
    at md51 (/Users/nos/Documents/GitHub/js-spark-md5/spark-md5.js:177:15)
    at Function.SparkMD5.hashBinary (/Users/nos/Documents/GitHub/js-spark-md5/spark-md5.js:570:20)
    at Function.SparkMD5.hash (/Users/nos/Documents/GitHub/js-spark-md5/spark-md5.js:558:25)
    at Object.<anonymous> (/Users/nos/Documents/GitHub/js-spark-md5/devil.js:3:5)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Function.Module.runMain (module.js:575:10)

it's a problem in here

if the content in buffer does not contain non-ascii charaters, toUtf8 will return str as it was (Buffer(object) in this case). and there is the bug.

but i can not figure out a way to write test case for this :/

any idea about the spec?

thanks.

zhuangya commented 8 years ago

below is the devil.js:


const md5 = require('.');

md5.hash(new Buffer(''));
satazor commented 8 years ago

spark-md5 does not support nodejs buffers..

You have to do:

const SparkMD5 = require('spark-md5');

const nodejsBuffer = new Buffer('');

SparkMD5.ArrayBuffer.hash(nodejsBuffer.toArrayBuffer());