Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a string output from int(hashlib.sha256(string.encode()).hexdigest)?

I am trying to solve a question that requires me to define a function that takes a positive integer T as an input and outputs a string so that

int(hashlib.sha256(string.encode()).hexdigest) = < T

is qualified. However, I do not understand how Python can output a string. I have thought of generating random strings or using str() but it seems unreasonable.

How will I be able to solve this question? Thank you very much.

like image 443
Eungda Avatar asked Oct 23 '25 12:10

Eungda


1 Answers

hashlib wont work like that hashlib.sha256() is a class you need to update with string value for gettin hashed hex value and you cant convert hex value with just int() you need to add base parameter as 16

usage:

import hashlib
string="dumb"
hs=hashlib.sha256() # hs is element the hashlib.sha256() class
hs.update(string.encode()) # update hs with a string value
#now you can get hashed hex value
hs_hex=hs.hexdigest() #string hex hash value
hs_int=int(hs_hex,16) #int hash value
hs_int_str=str(hs_int) #string int hash value
like image 137
Rifat Alptekin Çetin Avatar answered Oct 25 '25 06:10

Rifat Alptekin Çetin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!