Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

base 64 encode and decode a string in angular (2+)

How to encode or decode a string in angular 2 with base64 ??? My front-end tool is Angular 2. I had a password string, before passing it to API I need to base64 encode. Since in service base64 encoded string will be decoded.

So I am looking for some base64 encode/decode library for Angular2/Typescript and some options.

Thanks!!!

like image 650
praveen kumar Avatar asked Feb 01 '17 05:02

praveen kumar


People also ask

How do I encode a string in Base64?

To convert a string into a Base64 character the following steps should be followed: Get the ASCII value of each character in the string. Compute the 8-bit binary equivalent of the ASCII values. Convert the 8-bit characters chunk into chunks of 6 bits by re-grouping the digits.

What is Base64 in angular?

Base64 Encoding means of encoding text in ASCII(American Standard Code for Information Interchange) format. The Base64 scheme takes any kind of data type, such as binary data, and translates it into text format in ASCII. If you want to Encode, the method name is btoa().

What is BTOA in angular?

The btoa() method encodes a string in base-64. The btoa() method uses the "A-Z", "a-z", "0-9", "+", "/" and "=" characters to encode the string.


1 Answers

Use the btoa() function to encode:

console.log(btoa("password")); // cGFzc3dvcmQ=

To decode, you can use the atob() function:

console.log(atob("cGFzc3dvcmQ=")); // password
like image 147
Robby Cornelissen Avatar answered Oct 13 '22 05:10

Robby Cornelissen