Created
March 2, 2015 21:45
-
-
Save seanmckaybeck/3e5471dc34a042788ca4 to your computer and use it in GitHub Desktop.
This file contains 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
''' | |
something something docstring | |
''' | |
from Crypto.Hash import SHA | |
BLOCK_SIZE = SHA.digest_size | |
O_CONST = 0x5c | |
I_CONST = 0x36 | |
def hmac_sha1(key, msg): | |
''' | |
Returns an HMAC of the key and msg | |
''' | |
if len(key) > BLOCK_SIZE: | |
key = SHA.new(data=key).hexdigest() | |
key = key + bytearray(BLOCK_SIZE - len(key)) | |
o_key_pad = bytearray([key[i] ^ O_CONST for i in range(BLOCK_SIZE)]) | |
i_key_pad = bytearray([key[i] ^ I_CONST for i in range(BLOCK_SIZE)]) | |
second_half = SHA.new(i_key_pad + msg).hexdigest() | |
return SHA.new(o_key_pad + second_half).hexdigest() | |
def working_hmac(key, msg): | |
import hmac | |
from hashlib import sha1 | |
return hmac.HMAC(key, msg, sha1).hexdigest() | |
def pycrypto_hmac(key, msg): | |
from Crypto.Hash import HMAC | |
return HMAC.new(key, msg=msg, digestmod=SHA).hexdigest() | |
if __name__ == '__main__': | |
print hmac_sha1('poop', 'here is a stinky message') | |
print working_hmac('poop', 'here is a stinky message') | |
print pycrypto_hmac('poop', 'here is a stinky message') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment