Home python3 recursive function of n * (b(a))
Reply: 0

python3 recursive function of n * (b(a))

user1121 Published in August 15, 2018, 8:38 pm

I'm trying to write a function that would recursively hash a key for n times, alternating between sha224 and sha256. Each iteration would be hash_256(hash_224)--a hash256 for the hash224 of the key--so that it would yield n * (hash_256(hash_224)). However, I'm new to coding and can't figure out how to write a recursive function with these parameters.

import hashlib

def shasum(key, n):
key = str(key).encode('utf-8')

hash_a = hashlib.sha224(key).hexdigest().encode('utf-8'))
hash_b = hashlib.sha256(hash_a).hexdigest()

if n == 0 or 1:
return hash_b #one iteration of 256(224)
return n-1
return hash_b #stuck here

Edited: now it behaves like a number generator. What's wrong?

import hashlib

 n = 0

def sha480(seed):
hashed_224 = str(hashlib.sha224(seed)).encode('utf-8')
hashed_256 = hashlib.sha256(hashed_224).hexdigest()
hashed_480 = str(hashed_256)
print("hash: " + hashed_480)

def repeater(key, n):
if n == 0:
 return key
 seed = str(key).encode('utf-8')
while n > 0:
return sha480(repeater(seed, n-1))

repeater('what', 2)
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308533 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO