Python common encryption module (hashlib, salts of confusion hashlib, hmac)

1, Hash

2,hmac

 

hashlib module

hashlib python provides a module, the hash algorithm may be encrypted value transmitted

>>> dir(hashlib)
['__all__', '__builtin_constructor_cache', '__builtins__', '__cached__', '__doc__', '__file__', '__get_builtin_constructor', '__loader__', '__name__', '__package__', '__spec__', '_hashlib', 'algorithms_available', 'algorithms_guaranteed', 'blake2b', 'blake2s', 'md5', 'new', 'pbkdf2_hmac', 'scrypt', 'sha1', 'sha224', 'sha256', 'sha384', 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512', 'sha512', 'shake_128', 'shake_256']
>>>

>>> help(hashlib.md5)
Help on built-in function openssl_md5 in module _hashlib:

openssl_md5(...)
    Returns a md5 hash object; optionally initialized with a string

>>>

 

The built-in function modules _hashlib openssl_md5 help:

openssl_md5 (...) returns the md5 hash object;

String may be selected

>>>

 

Which we only use md5 encryption algorithm demo:

MD5-based encryption:

import hashlib   #导入模块

passwd = "redhat"   #密码字符
md5passwd = hashlib.md5()        #初始化MD5  
md5passwd.update(passwd.encode('utf-8'))      #hashlib只能接受比特b数据,这里先转码#输出散列值

print(md5passwd.hexdigest())        #输出散列值,也就是转换成16进制字符串

 

Salts of confusion with the MD5 of:

import hashlib   #导入模块

passwd = "redhat"   #密码字符
salt = "^*F&DB#@%&)(*%$#@"

md5passwd = hashlib.md5()
md5passwd.update(passwd.encode('utf-8'))
md5passwd.update(salt.encode('utf-8'))     #对原MD5值进行二次加密(盐值混淆)

print(md5passwd.hexdigest())    #输出散列值,也就是转换成16进制字符串

 

hmac module

>>> import hmac
>>> dir(hmac)
['HMAC', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_hashlib', '_hashopenssl', '_openssl_md_meths', '_warnings', 'compare_digest', 'digest', 'digest_size', 'new', 'trans_36', 'trans_5C']
>>>

Here we only learn a new method

 

hmac new encryption method

>>> help(hmac.new)
Help on function new in module hmac:

new(key, msg=None, digestmod=None)
    Create a new hashing object and return it.

    key: The starting key for the hash.
    msg: if available, will immediately be hashed into the object's starting
    state.

    You can now feed arbitrary strings into the object using its update()
    method, and can ask for the hash value at any time by calling its digest()
    method.

>>>

 

>>> help(hmac.new)

Help hmac module new features

new (key, msg = None, digestmod = "default MD5") to create a new hash object and return it.

key: a hash of the start key.

msg: If available, the hash will immediately start state of the object.

You can now use its update () method to input any string objects, and may at any time by calling its digest () method to request a hash value.

>>>

import hmac     #导入模块
 
passwd = b"redhat"              #密码字符         
passwd2 = b"^*F&DB#@%&)(*%$#@"  #混淆参数 

hmacpasswd = hmac.new(passwd,passwd2,digestmod = 'MD5')  
#方法后必须加上默认使用的加密方式,这里的hmac是一个较为复杂的混合加码方法,安全系数相较于hashlib有很大的提升

print(hmacpasswd.hexdigest())    #输出散列值,也就是十六进制字符串

 

 

Published 35 original articles · won praise 36 · views 6092

Guess you like

Origin blog.csdn.net/Alexz__/article/details/104533212