Python 3暗号化(hashlib/hmac)
6127 ワード
Python 3暗号化(hashlib/hmac)
本論文はLuzhuoによって作成され、転送はこの情報を保持してください。
以下のコードはPython 3.6.1を例にとってLess is more!hashlib:不可逆暗号hmac:不可逆キーペア方式暗号化base 64:可逆暗号化
hashlib
本論文はLuzhuoによって作成され、転送はこの情報を保持してください。
以下のコードはPython 3.6.1を例にとってLess is more!hashlib:不可逆暗号hmac:不可逆キーペア方式暗号化base 64:可逆暗号化
hashlib
#!/usr/bin/env python
# coding=utf-8
__author__ = 'Luzhuo'
__date__ = '2017/5/19'
# hash_demo.py Hash ( )
# : MD5, SHA1 SHA224 SHA256 SHA384 SHA512
import hashlib
def hash_demo():
m = hashlib.md5()
m.update(b"hello")
m.update(b"world!") # = hello + world!
hash_hex = hashlib.sha3_512(b"luzhuo.me").hexdigest()
print(m.digest_size)
print(m.digest()) # hash
print(m.hexdigest()) # hash
print(hash_hex)
#
hash_bytes = hashlib.pbkdf2_hmac('sha256', b'luzhuo.me', b'80', 100000)
print(hash_bytes)
def hash_func():
# hashlib.new(name[, data]) // hashlib( ), name= , data:
hash = hashlib.new('ripemd160', b'luzhuo.me')
#
dics = hashlib.algorithms_guaranteed # hash
dics = hashlib.algorithms_available # Python hash , new() ,
# hashlib.pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None) // hash_name:hash , password: , salt: , iterations: , dklen:
hash_bytes = hashlib.pbkdf2_hmac('sha256', b'luzhuo.me', b'80', 100000)
# hash
num = hash.digest_size # hash
num = hash.block_size # hash
strs = hash.name # hash , new()
hash.update(b"data") # hash.update(a) hash.update(b) == hash.update(a+b)
hash_bytes = hash.digest() # hash
hash_str = hash.hexdigest() # 16 hash
hash = hash.copy() # hash
if __name__ == "__main__":
hash_demo()
# hash_func()
hmac#!/usr/bin/env python
# coding=utf-8
__author__ = 'Luzhuo'
__date__ = '2017/5/19'
# hmac_demo.py HMAC
# hashlib key
import hmac
def hmac_demo():
#
h = hmac.new(b"net")
h.update(b"luzhuo.me")
h_str = h.hexdigest()
print(h_str)
#
boolean = hmac.compare_digest(h_str, hmac.new(b"net", b"luzhuo.me").hexdigest())
print(boolean)
def hmac_func():
# key ,
# hmac.new(key, msg=None, digestmod=None) // hmac , key: , msg:update(msg), digestmod:hash ( hashlib.new())( md5)
hc = hmac.new(b"key")
# hmac
hc.update(b"msg") # hc.update(a) hc.update(b) == hc.update(a+b)
hash_bytes = hc.digest() # hash
hash_str = hc.hexdigest() # 16 hash
hc = hc.copy() # hmac
num = hc.digest_size # hash
num = hc.block_size # hash
strs = hc.name # hash
# hmac.compare_digest(a, b) // hash , : str / bytes-like object, ( : , a==b)
boolean = hmac.compare_digest(hmac.new(b"net", b"luzhuo.me").digest(), hmac.new(b"net", b"luzhuo.me").digest())
if __name__ == "__main__":
hmac_demo()
# hmac_func()