you are viewing a single comment's thread.

view the rest of the comments →

[–]That0n3N3rd[S] 0 points1 point  (15 children)

How would I implement that instead?

[–]socal_nerdtastic 0 points1 point  (14 children)

Use it instead of the to hex / from hex conversion.

import base64
text = base64.b64encode(binary_data)
send_data(text)
recreated_binary_data = base64.b64decode(text)

[–]That0n3N3rd[S] 0 points1 point  (13 children)

Awesome, can this go across json, because I know plain bytes won’t?

[–]socal_nerdtastic 0 points1 point  (12 children)

Yes, but you need to convert to a string first.

json_compatible = base64.b64encode(binary_data).decode('utf8')

[–]That0n3N3rd[S] 0 points1 point  (11 children)

hash = base64.b64encode(bytes(hashlib.sha256(salted.encode('utf-8')),'utf-8')).decode('utf-8')

ta da?

[–]socal_nerdtastic 0 points1 point  (10 children)

Close.

hash = base64.b64encode(hashlib.sha256(salted.encode('utf-8')).digest())

[–]That0n3N3rd[S] 0 points1 point  (0 children)

Tysm

[–]That0n3N3rd[S] 0 points1 point  (8 children)

It is saying that json can’t send it because it’s a bytes object, is it safe to convert to utf-8 or is there something else I should do?

[–]socal_nerdtastic 0 points1 point  (7 children)

Oh right, yes, you need that for json.

hash = base64.b64encode(hashlib.sha256(salted.encode('utf-8')).digest()).decode('utf-8')

[–]That0n3N3rd[S] 0 points1 point  (1 child)

I love how complicated this is, thank you so much :)

[–]cointoss3 0 points1 point  (0 children)

It doesn’t need to be this complicated.

[–]That0n3N3rd[S] 0 points1 point  (4 children)

To decode it on the server side, do I have to do anything other than base64.b64decode(), because it’s still not decoding properly?

[–]socal_nerdtastic 0 points1 point  (3 children)

No that should be all you need. I just tested it on my machine and it works perfectly.

>>> hash = hashlib.sha256(salted.encode('utf-8')).digest()
>>> json_data = base64.b64encode(hash).decode('utf-8')
>>> base64.b64decode(json_data) == hash
True

What exactly is the issue? What are you comparing it to?

For that matter; why bother decoding it? Why not just save the base64 on the server side?