How do you decode with hmac?

Posted on 2010-11-23
Last Modified: 2012-05-10

How do you decode with hmac?
I can encode with this function below.. but decode function does not work

def hmac_encode(key, str):
     return, str, hashlib.sha1).digest().encode('base64')[:-1]
def hmac_decode(key, str):
     return, str, hashlib.sha1).digest().decode('base64')[:-1]


Question by:jamie_lynn
  • 3
  • 2
LVL 28

Assisted Solution

pepr earned 500 total points
ID: 34208895
Hi Jamie, hmac does something else than you expect It can take many kilobytes of the input string.  It returns a fixed lenght digest that is a kind of fingerprint of the input string.  This way, you can check whether (say) downloaded data (i.e. the input string) was not changed during the transmission.  You simply use the same algorithm and the same key and (hopefully) the same input string, and you have to get the same digest (the fingerprint).  If you get something else, then the data is not identical to what was sent.

In other way, it is a one-way function that returns a signature of the data.  It is not a kind of compression or reversable encoding.

The methods .encode() and .decode() do simply convert the string (bytes) signature to some other string.  You can also use .hexdigest() to have the signature expressed as nicely printable characters.

Try this:
import hashlib
import hmac

k = '0000'
s = 'xxxx string'
h =, s, hashlib.sha1)
print h.digest()
print h.hexdigest()

Open in new window

See the funny characters as the result of .digest() -- the bytes are interpreted as characters by the print command.  The .hexdigest() shows what the bytes are -- each couple of hex digits describes one byte from the .digest(). It shows the following on my computer:


Author Comment

ID: 34210847
Hi pepr,

Oh, then this isn't what i want. This works for password (encoding one way), but i am obfuscating my emails addresses too. So I need a way to decode the data.
What is a good way to enocde and decode emails in databases?

LVL 28

Accepted Solution

pepr earned 500 total points
ID: 34211028
You probably should search for encryption/decryption algorithms.  The standard documentation points indirectly to PyCrypto that can be downloaded from PyPI (see  The homepage of the package is  The principles (how to work with it) are described at  You probably want to use one of the encryption algorithms (  There is a short example that uses DES algorithm -- it should be OK for you.  However, AES is more recommended these days.

Basically, there are two ways used to encrypt/decrypt the data.  The simpler one uses a single key (must be kept secret) that is used to both encrypt and then decrypt the message.  The more sofisticated uses two keys: one is used for encryption and the other for decryption. One of them may be made public, the other must be kept secret by the owner of the keys. The two approaches are often combined to get better performance.
LVL 28

Expert Comment

ID: 34211040

Author Closing Comment

ID: 34211056
Thanks pepr. I'll use the single key since my app is the only one encrypting and decrypting the data

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question