標籤:his sha-1 nts 無法 -- microsoft exce mic ade
#python3.4
hashlib module - A common interface to many hash functions.
hash.digest() - Return the digest of the data passed to the update() method so far. This is a bytes object of size digest_size which may contain bytes in the whole range from 0 to 255.
hash.hexdigest() - Like digest() except the digest is returned as a string object of double length, containing only hexadecimal digits. This may be used to exchange the value safely in email or other non-binary environments.
注1:python 3 由於預設是 Unicode 因此 update(arg) 中的 arg如果是字串時,需要用b開頭,例如 b"hello world!!"。
注2:MD5為例,及校正和為128bit資料,對應16Byte 資料。但這個16Byte無法用ASCII直接表示(儲存、通訊等)。
因此 使用 hexdigest 將 128bit分為 32個4bit資料,4bit資料用hex(0~9,A~F)的ASCII表示。這樣對很多通訊協定就好用了,也風方便 人來閱讀及比較。
MD5 -- 128bit,SHA-1 -- 160bit, SHA-256 -- 256bit, SHA-512 -- 512bit
舉例:
>>>import hashlib
>>>hashlib.md5(b"hello").digest()
b‘][email protected]*\xbcK*v\xb9q\x9d\x91\x10\x17\xc5\x92‘
>>>hashlib.md5(b"hello").hexdigest()
‘5d41402abc4b2a76b9719d911017c592‘
1、將 hexdigest拆分如下:
5d 41 40 2a bc 4b 2a 76 b9 71 9d 91 10 17 c5 92
2、每2為hex轉換為 hex( 其中 大於0x80的無法轉換,0x0~0x7F中有些也需要轉移。可得
b ‘ ] A @ * \xbc K * v \xb9 q \x9d \x91 \x10 \x17 \xc5 \x92。 和 digest( 就對起來了)。
Python Hashlib筆記