The goal is to output both the base64 and hex encodings of a file's MD5 hash without relying on deprecated node.js features. This means using Hash.read()
instead of Hash.digest()
, and using Buffer
instances instead of "binary"
-encoded strings. From the docs:
Once the writable side of the [Hash] stream is ended, use the read() method to get the computed hash digest. The legacy update and digest methods are also supported.
and
[The "binary"] encoding method is deprecated and should be avoided in favor of Buffer objects where possible. This encoding will be removed in future versions of Node.
So far, I cannot see how computing multiple hash encodings is possible in 0.10.22 without using these deprecated features. (Technically, hash.digest() is not deprecated, but I'm wary to use anything labeled legacy).
The best I've found still relies on "binary"
-encoded strings:
var fs = require("fs"), crypto = require("crypto");
var input = fs.createReadStream("test.txt");
var hash = crypto.createHash("md5");
hash.setEncoding("binary");
input.pipe(hash).on("finish", function() {
var buffer = new Buffer(hash.read(), "binary");
console.log(buffer.toString("hex"));
console.log(buffer.toString("base64"));
});
If the call to hash.setEncoding
is removed, then the subsequent hash.read()
method returns null. Perhaps this is a bug? Kinda expected an instance of Buffer...
Any ideas on how to avoid the above deprecated features?
(For reference, this question covers how to compute one hash encoding, not multiple encodings.)