# Getting the key used for HMAC given the hash and the plaintext?

I'm working on a little exercise to understand the drawbacks of poor library choices and I've been presented with the following scenario.

The HMAC function in python is being used (md5). I know the message being hashed as well as well as the resulting HMAC for this message. The key being used is generated by random.getrandbits(128).

The only thing I can think of is to put the random.getrandbits(128) in some kind of while loop and generate HMACs for the message until I find a match with the given HMAC.

Is there any way I can extract the key being used by the hmac function knowing the message, the function used to generate keys and the message digest, that does not involve brute-force?

• Is random the random module, which uses the non-cryptographic Mersenne Twister as a PRNG? Or is it an instance of SystemRandom, which uses /dev/urandom? – knbk Nov 4 '17 at 1:07
• I've been looking into it and I've discovered that random does not refer to /dev/urandom. It's the other one that does indeed use non-cryptographic Mersenne Twister as a PRNG. – JohnnyHunter Nov 4 '17 at 1:10