winn j
winn j

Reputation: 452

How to use a python loop to iterate over hash multiple times?

I have created a python loop that should be passing the output of the digest back into the msg portion of the digest command, however i am ending up with exact same has no matter how many times i run the loop

from __future__ import print_function;
import hmac;
import hashlib;
import base64;

mydigest = base64.b64encode(hmac.new(b"salt", msg="mymessage", digestmod=hashlib.sha256).digest()).decode();

for x in range(0, 10000):
  mydigest = base64.b64encode(hmac.new(b"salt", msg="(mydigest)", digestmod=hashlib.sha256).digest()).decode();
  print (mydigest);

Upvotes: 0

Views: 965

Answers (1)

Ilya V. Schurov
Ilya V. Schurov

Reputation: 8067

You probably want

mydigest = base64.b64encode(hmac.new(b"salt", msg=mydigest, digestmod=hashlib.sha256).digest()).decode();

There's no string interpolation in "ordinary" strings in Python (strings with interpolation — the so-called f-strings — recently appeared in Python 3.6), so "(mydigest)" is a fixed string that has nothing with variable mydigest.

Upvotes: 2

Related Questions