Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do Base64 encoding in Node.js?

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.

like image 775
murvinlai Avatar asked May 31 '11 02:05

murvinlai


People also ask

What is Base64 in Nodejs?

Base64 is a binary-to-text encoding scheme used to transport data. The encoding is necessary when the transfer medium is not able to handle binary data. This binary data is then translated to a text representation (base64) and transferred as text.

What is Base64 and Buffer?

The buffer object can be encoded and decoded into Base64 string. The buffer class can be used to encode a string into a series of bytes. The Buffer. from() method takes a string as an input and converts it into Base64. The converted bytes can be changed again into String.

What is Base64 encoding example?

Base64 Encoding Example A Base64 encoder starts by chunking the binary stream into groupings of six characters: 100110 111010 001011 101001. Each of these groupings translates into the numbers 38, 58, 11, and 41.


1 Answers

Buffers can be used for taking a string or piece of data and doing Base64 encoding of the result. For example:

> console.log(Buffer.from("Hello World").toString('base64')); SGVsbG8gV29ybGQ= > console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii')) Hello World 

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 and Buffer constructor encodings are as follows:

'ascii' - for 7 bit ASCII data only. This encoding method is very fast, and will strip the high bit if set.

'utf8' - Multi byte encoded Unicode characters. Many web pages and other document formats use UTF-8.

'ucs2' - 2-bytes, little endian encoded Unicode characters. It can encode only BMP(Basic Multilingual Plane, U+0000 - U+FFFF).

'base64' - Base64 string encoding.

'binary' - A way of encoding raw binary data into strings by using only the first 8 bits of each character. This 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.

like image 185
onteria_ Avatar answered Oct 07 '22 04:10

onteria_