How to encryption a string with SHA256 with Python

2 Answers

0 votes
import hashlib 
  
s = "Python Programming"

hs = hashlib.sha256(s.encode()) 
  
print(hs.hexdigest()) 



'''
run:

d15647d90cc9f8e341146110bf5058773171688b237d2f9b5704399a0b17c8bd

'''

 



answered Apr 10, 2019 by avibootz
0 votes
import hashlib

def encrypt_string(s):
    # Encode the input string to bytes
    encoded_string = s.encode()
    
    # Create a SHA-256 hash object
    sha256_hash = hashlib.sha256()
    
    # Update the hash object with the encoded string
    sha256_hash.update(encoded_string)
    
    # Get the hexadecimal representation of the hash
    encrypted_string = sha256_hash.hexdigest()
    
    return encrypted_string

s = "Python Programming"

encrypted_string = encrypt_string(s)

print(encrypted_string)



 
'''
run:
 
d15647d90cc9f8e341146110bf5058773171688b237d2f9b5704399a0b17c8bd

'''

 



answered Dec 26, 2024 by avibootz

Related questions

2 answers 360 views
1 answer 167 views
1 answer 326 views
1 answer 304 views
304 views asked Aug 8, 2020 by avibootz
...