加密模块的使用

python 的加密模块有5种,即hmac, md5, sha, mpz, rotor
前3种用法几乎一样,主要的函数是
new(msg) //创建一个加密的object
digest() //Return the digest of the strings passed to the update() method so far
hexdigest() //以hex码显示加密结果
update(msg) //m.update(a),m.update(b)结果为m.update(a+b)
copy()  //新建一个object,加密的串和copy的那个一样

eg:
import hmac
import md5
import sha
h = hmac.new ('python')
m = md5.new ('python')
s = sha.new ('python')
print h.hexdigest()
print m.hexdigest()
print s.hexdigest()

mpz: Deprecated since release 2.2. See the references at the end of this section for information about packages which provide similar functionality. This module will be removed in Python 2.3.
reference from book
因此就不记下它的用法了

rotor: Deprecated since release 2.3. The encryption algorithm is insecure. 好像也不太可靠
主要函数如下:
newrotor(
key[, numrotors])
Return a rotor object. key is a string containing the encryption key for the object; it can contain arbitrary binary data but not null bytes. The key will be used to randomly generate the rotor permutations and their initial positions. numrotors is the number of rotor permutations in the returned object; if it is omitted, a default value of 6 will be used.
Rotor objects have the following methods:
setkey(
key)
Sets the rotor's key to key. The key should not contain null bytes.
encrypt(
plaintext)
Reset the rotor object to its initial state and encrypt plaintext, returning a string containing the ciphertext. The ciphertext is always the same length as the original plaintext.
encryptmore(
plaintext)
Encrypt plaintext without resetting the rotor object, and return a string containing the ciphertext.
decrypt(
ciphertext)
Reset the rotor object to its initial state and decrypt ciphertext, returning a string containing the plaintext. The plaintext string will always be the same length as the ciphertext.
decryptmore(
ciphertext)
Decrypt ciphertext without resetting the rotor object, and return a string containing the plaintext.
An example usage:
>>> import rotor
>>> rt = rotor.newrotor('key', 12)
>>> rt.encrypt('bar')
'\xab4\xf3'
>>> rt.encryptmore('bar')
'\xef\xfd$'
>>> rt.encrypt('bar')
'\xab4\xf3'
>>> rt.decrypt('\xab4\xf3')
'bar'
>>> rt.decryptmore('\xef\xfd$')
'bar'
>>> rt.decrypt('\xef\xfd$')
'l(\xcd'
>>> del rt