Passing buffers to crypto Hash objects

77 views
Skip to first unread message

David Goodlad

unread,
Jul 13, 2010, 7:54:44 AM7/13/10
to nod...@googlegroups.com
Hi all

I'm trying to do some simple hashing of files that I'm reading from
the filesystem. Using fs.createReadStream, I stream the file from
disk, and pass the data to a crypto Hash object's update function.
Once I get the end event, I call digest. This ends up generating a
different hash from what my native system's cmdline tools generate.
Here's the function I'm using:

process: (file, callback) ->
hash: crypto.createHash @algorithm
stream: fs.createReadStream file
bytes: 0
stream.addListener 'data', (data) ->
#hash.update data.toString('binary', 0, data.length)
hash.update data
stream.addListener 'end', () =>
callback hash.digest('hex')

If, instead of passing the buffer object (data) directly to
hash.update, I first convert it to a string using the 'binary'
encoding, it works:

hash.update data.toString('binary', 0, data.length)

However, the docs for the binary encoding explicitly state "A legacy
encoding. [...] Don't use this."

I'm now confused on two fronts. It would make sense to me to be able
to pass a Buffer directly to the Hash's update function, since Buffers
are explicitly designed for handling binary data, unlike javascript
strings. If there's a reasonable argument against this, can someone
explain why I have to use the binary encoding, which the docs say not
to? Neither the utf8 nor the ascii encodings work, obviously, since
they do too much processing on the data...

Thanks!
Dave

Reply all
Reply to author
Forward
0 new messages