Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
38
rated 0 times [  44] [ 6]  / answers: 1 / hits: 18197  / 8 Years ago, sun, september 4, 2016, 12:00:00

I'm trying to find an exact equivalent to javascript's function 'btoa', as I want to encode a password as base64. It appears that there are many options however, as listed here:



https://docs.python.org/3.4/library/base64.html



Is there an exact equivalent to 'btoa' in python?


More From » python

 Answers
24

Python's Base64:


import base64

encoded = base64.b64encode(b'Hello World!')
print(encoded)

# value of encoded is SGVsbG8gV29ybGQh

Javascript's btoa:


var str = "Hello World!";
var enc = window.btoa(str);

var res = enc;

// value of res is SGVsbG8gV29ybGQh

As you can see they both produce the same result.


[#60818] Thursday, September 1, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
carolynt

Total Points: 252
Total Questions: 98
Total Answers: 109

Location: French Southern and Antarctic Lands
Member since Sat, Oct 31, 2020
4 Years ago
;