Does Node.js have built-in Base64 encoding yet?
The reason why I ask this is that final()
from crypto
can only output hexadecimal, binary or ASCII data. For example:
var cipher = crypto.createCipheriv('des-ede3-cbc', encryption_key, iv);
var ciph = cipher.update(plaintext, 'utf8', 'hex');
ciph += cipher.final('hex');
var decipher = crypto.createDecipheriv('des-ede3-cbc', encryption_key, iv);
var txt = decipher.update(ciph, 'hex', 'utf8');
txt += decipher.final('utf8');
According to the documentation, update()
can output Base64-encoded data. However, final()
doesn't support Base64. I tried and it will break.
If I do this:
var ciph = cipher.update(plaintext, 'utf8', 'base64');
ciph += cipher.final('hex');
Then what should I use for decryption? Hexadecimal or Base64?
Therefore, I'm looking for a function to Base64-encode my encrypted hexadecimal output.
Best Answer
Buffers can be used for taking a string or piece of data and doing Base64 encoding of the result. For example:
Buffers are a global object, so no require is needed. Buffers created with strings can take an optional encoding parameter to specify what encoding the string is in. The available
toString
andBuffer
constructor encodings are as follows: