114

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!!!

praveen kumar
  • 1,474
  • 4
  • 13
  • 19

6 Answers6

237

Use the btoa() function to encode:

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

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

console.log(atob("cGFzc3dvcmQ=")); // password
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
25

From Angular 12 btoa() and atob() functions are deprecated. Use these instead:

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

Note: you must explicit encoding!

Eros Mazza
  • 304
  • 3
  • 7
  • 7
    getting `ReferenceError: Buffer is not defined` – Pierre Oct 07 '22 at 10:05
  • I got this error too. After some research, I noticed that the deprecation warning of the btoa function says: "This function is only provided for compatibility with legacy web platform APIs and should never be used in new code, because they use strings to represent binary data and predate the introduction of typed arrays in JavaScript. For code running using Node.js APIs, converting between base64-encoded strings and binary data should be performed using Buffer.from(str, 'base64') andbuf.toString('base64')". So it may be used for APIs, not for client side code. – NFTX Oct 10 '22 at 16:09
  • 2
    `ReferenceError: Buffer is not defined` error you can solve doing: installing Buffer, with npm i buffer and import it where you call the function See [Convert a string to base64 in JavaScript. btoa and atob are deprecated](https://stackoverflow.com/a/70733727/7757505) – kergrau Nov 08 '22 at 17:12
  • 4
    Thoose deprecations are a false alert and cames from the nodeJS api, that is needed by angular tools but will not be present in browser context. You must use btoa/atob in browser platform an Buffer in server platform. Using window.atob instead of atob is a workaround to get rid of it. See https://github.com/microsoft/TypeScript/issues/45566 – max-lt Nov 26 '22 at 11:24
5

For encoding to base64 in Angular2, you can use btoa() function.

Example:-

console.log(btoa("stringAngular2")); 
// Output:- c3RyaW5nQW5ndWxhcjI=

For decoding from base64 in Angular2, you can use atob() function.

Example:-

console.log(atob("c3RyaW5nQW5ndWxhcjI=")); 
// Output:- stringAngular2
VIKAS KOHLI
  • 8,164
  • 4
  • 50
  • 61
4

Use btoa("yourstring")

more info: https://developer.mozilla.org/en/docs/Web/API/WindowBase64/Base64_encoding_and_decoding

TypeScript is a superset of Javascript, it can use existing Javascript libraries and web APIs

shusson
  • 5,542
  • 34
  • 38
2

Use btoa() for encode and atob() for decode

text_val:any="your encoding text";

Encoded Text: console.log(btoa(this.text_val)); //eW91ciBlbmNvZGluZyB0ZXh0

Decoded Text: console.log(atob("eW91ciBlbmNvZGluZyB0ZXh0")); //your encoding text

Yasintha
  • 17
  • 4
  • 8
    It's awesome that you want to help, but this answer doesn't add any extra value, since it is a copy of the accepted answer. On SO we like to avoid repetition. Please consider removing this answer. – ViG Mar 28 '18 at 10:11
-1

If you want a more complete solution, you can use the turbocommons library. Just install it:

npm install turbocommons-ts

And then call the method you need:

import { ConversionUtils } from 'turbocommons-ts';

console.log(ConversionUtils.stringToBase64('hello'));
console.log(ConversionUtils.base64ToString('aGVsbG8='));

This library is open source and extensively tested. More info here:

https://turboframework.org/en/blog/2022-10-26/encode-decode-base64-strings-javascript-typescript-php

Jaume Mussons Abad
  • 706
  • 1
  • 6
  • 20