Why crypto.createHash() is not writable until setEncoding() is called?

  • Version: v6.9.1
  • Platform: Darwin 15.2.0 Darwin Kernel Version 15.2.0: Fri Nov 13 19:56:56 PST 2015; root:xnu-3248.20.55~2/RELEASE_X86_64 x86_64

Please look at the code below:

var hasher = crypto.createHash('sha256'); 
// hasher.setEncoding('hex');
console.log(hasher.writable); 

So, if one use hasher with the legacy stream.pipe() implementation there will be an error, because the hash will be always calculated from the empty value.

It's easy to reproduce:

const crypto = require('crypto'); 
const request = require('request');  // this package uses oldschool streams

var hasher = crypto.createHash('sha256'); 
// Uncomment the line below to fix!
// hasher.setEncoding('hex');
console.log(hasher.writable); 
request('http://ya.ru').pipe(hasher).on('finish', function() {
    console.log('Hash is', hasher.read()); 
});