Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
python: base 64 encode the sha1 hash of a string
>>> import base64
>>> import hashlib
>>> base64.b64encode(hashlib.sha1("test").digest())
'qUqP5cyxm6YcTAhz05Hph5gvu9M='
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Jul 16, 2015

This helped me alot! Thanks mate.

@ark4n631

This comment has been minimized.

Copy link

@ark4n631 ark4n631 commented Apr 5, 2016

Thanks!

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Sep 20, 2016

import base64
import hashlib
user_input = input("Enter string to encode")
result = base64.b64encode(hashlib.sha1(user_input))
print(result)

Error
TypeError: Unicode-objects must be encoded before hashing

@brockcooper

This comment has been minimized.

Copy link

@brockcooper brockcooper commented Sep 30, 2016

you need to encode the string first with something like str.encode(user_input).

Something like this just worked for me:

import base64
import hashlib

user_input = input("Enter string to encode")
result = base64.b64encode(hashlib.sha1(str.encode(user_input)))
print(result)

@wozniak

This comment has been minimized.

Copy link

@wozniak wozniak commented Jan 1, 2017

I was wondering how to use hashlib.sha***() with variables!!
Thx a lot!

@lemontree863

This comment has been minimized.

Copy link

@lemontree863 lemontree863 commented Nov 6, 2019

thanks a lot!

@ignazioc

This comment has been minimized.

Copy link

@ignazioc ignazioc commented Aug 27, 2020

nice, that's what I was looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment