Created
March 9, 2016 20:58
-
-
Save f0t0n/9a5abb1df0bea9bd2e95 to your computer and use it in GitHub Desktop.
Measure hmac encoding performance Base64 vs hex digest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import hmac | |
import string | |
from hashlib import sha256 | |
UTF8 = 'utf-8' | |
MESSAGE = '1457369891.672671' | |
SECRET_KEY = 'my secret key' | |
def create_hmac(): | |
new_hmac = hmac.new(bytes(SECRET_KEY, UTF8), digestmod=sha256) | |
new_hmac.update(bytes(MESSAGE, UTF8)) | |
return new_hmac | |
def base64_digest(): | |
return base64.b64encode(create_hmac().digest()) | |
def hex_digest(): | |
return create_hmac().hexdigest() | |
if __name__ == '__main__': | |
from timeit import timeit | |
print(timeit('base64_digest()', number=100000, | |
setup='from __main__ import base64_digest')) | |
print(timeit('hex_digest()', number=100000, | |
setup='from __main__ import hex_digest')) | |
# Got approximately next output: | |
# 1.235968765002326 | |
# 1.0631242269882932 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment