Python Hashlib筆記

來源:互聯網
上載者:User

標籤: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筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.