& hashlib module random module

random module
1, random.randrange (1, 10) : returns a random number between 1 and 10, excluding 10
2, the random.randint (1,10): returns a random number between 1 and 10, comprising 10
. 3, random.randrange (0, 100, 2): randomly select the even-numbered 0-100
4, random.random (): returns a random float
5, random.choice ( "kldajkkef * & @ hds!") : returns a given data set of random characters
6, random.sample ( 'dkfjkjksjj', 3): selecting a specific number of characters from the plurality of characters
exercise: write a 4-digit random codes:

import string
print(''.join(random.sample(string.ascii_uppercase + string.ascii_lowercase + string.digits, 4)))

hashlib module
for encryption related operations, in use hashlib 3.x instead md5 sha modules and modules, providing the main SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm

Import hashlib
 # MD5 
m = hashlib.md5 () 
m.update (B " the Hello " ) 
m.update (B " It apos Me " )
 # binary hash value returned 
Print (m.digest ()) # B '] \ XDE \ XB4 {/ \ x92Z \ XD0 \ XBF $ \ x9cR \ xe3Br \ x8a ' 
# returns the hash value in hexadecimal 
Print (m.hexdigest ()) # 5ddeb47b2f925ad0bf249c52e342728a 

# SHA1 
S1 = hashlib.sha1 () 
s1.update ( " Xu " .encode ( " UTF-. 8 " )) 
Print (s1.hexdigest ())# '2383e706924bd9292154c9aaa0e716f1f4f020f5'

# sha256
s2 = hashlib.sha256()
s2.update("xu".encode("utf-8"))
print(s2.hexdigest())
# '9c2f138690fca4890c3c4a6691610fbbbdf32091cc001f7355cfdf574baa52b9' # sha512 s3 = hashlib.sha512() s3.update("xu".encode("utf-8")) print(s3.hexdigest()) #'16929f53cd2401662aa6cbef7e076911dc1d570250c6516c5e2ff3fbaea8151b8322521029250eaedd05c977d017572aefb5b642429e39cac9ee6f516f4cc76a'

 

Guess you like

Origin www.cnblogs.com/zrxu/p/11586713.html