本文整理汇总了Python中Crypto.Hash.MD5属性的典型用法代码示例。如果您正苦于以下问题:Python Hash.MD5属性的具体用法?Python Hash.MD5怎么用?Python Hash.MD5使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类Crypto.Hash
的用法示例。
在下文中一共展示了Hash.MD5属性的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testEncryptDecrypt1
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import MD5 [as 别名]
def testEncryptDecrypt1(self):
# Helper function to monitor what's requested from RNG
global asked
def localRng(N):
global asked
asked += N
return self.rng(N)
# Verify that OAEP is friendly to all hashes
for hashmod in (MD2,MD5,SHA1,SHA256,RIPEMD):
# Verify that encrypt() asks for as many random bytes
# as the hash output size
asked = 0
pt = self.rng(40)
self.key1024._randfunc = localRng
cipher = PKCS.new(self.key1024, hashmod)
ct = cipher.encrypt(pt)
self.assertEqual(cipher.decrypt(ct), pt)
self.failUnless(asked > hashmod.digest_size)
示例2: testEncryptDecrypt2
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import MD5 [as 别名]
def testEncryptDecrypt2(self):
# Helper function to monitor what's requested from RNG
global asked
def localRng(N):
global asked
asked += N
return self.rng(N)
# Verify that OAEP is friendly to all hashes
for hashmod in (MD2,MD5,SHA1,SHA256,RIPEMD160):
# Verify that encrypt() asks for as many random bytes
# as the hash output size
asked = 0
pt = self.rng(40)
cipher = PKCS.new(self.key1024, hashmod, randfunc=localRng)
ct = cipher.encrypt(pt)
self.assertEqual(cipher.decrypt(ct), pt)
self.assertEqual(asked, hashmod.digest_size)
示例3: get_tests
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import MD5 [as 别名]
def get_tests(config={}):
global test_data
from Crypto.Hash import HMAC, MD5, SHA as SHA1, SHA256
from common import make_mac_tests
hashmods = dict(MD5=MD5, SHA1=SHA1, SHA256=SHA256, default=None)
try:
from Crypto.Hash import SHA224, SHA384, SHA512
hashmods.update(dict(SHA224=SHA224, SHA384=SHA384, SHA512=SHA512))
test_data += hashlib_test_data
except ImportError:
import sys
sys.stderr.write("SelfTest: warning: not testing HMAC-SHA224/384/512 (not available)\n")
return make_mac_tests(HMAC, "HMAC", test_data, hashmods)
示例4: test3
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import MD5 [as 别名]
def test3(self):
# Verify that hmac_hash_module works like prf
password = b("xxx")
salt = b("yyy")
for hashmod in (MD5, SHA1, SHA224, SHA256, SHA384, SHA512):
pr1 = PBKDF2(password, salt, 16, 100,
prf=lambda p, s: HMAC.new(p,s,hashmod).digest())
pr2 = PBKDF2(password, salt, 16, 100, hmac_hash_module=hashmod)
self.assertEqual(pr1, pr2)
示例5: encode
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import MD5 [as 别名]
def encode(data, marker, passphrase=None, randfunc=None):
"""Encode a piece of binary data into PEM format.
:Parameters:
data : byte string
The piece of binary data to encode.
marker : string
The marker for the PEM block (e.g. "PUBLIC KEY").
Note that there is no official master list for all allowed markers.
Still, you can refer to the OpenSSL_ source code.
passphrase : byte string
If given, the PEM block will be encrypted. The key is derived from
the passphrase.
randfunc : callable
Random number generation function; it accepts an integer N and returns
a byte string of random data, N bytes long. If not given, a new one is
instantiated.
:Returns:
The PEM block, as a string.
.. _OpenSSL: http://cvs.openssl.org/fileview?f=openssl/crypto/pem/pem.h&v=1.66.2.1.4.2
"""
if randfunc is None:
randfunc = get_random_bytes
out = "-----BEGIN %s-----\n" % marker
if passphrase:
# We only support 3DES for encryption
salt = randfunc(8)
key = PBKDF1(passphrase, salt, 16, 1, MD5)
key += PBKDF1(key + passphrase, salt, 8, 1, MD5)
objenc = DES3.new(key, DES3.MODE_CBC, salt)
out += "Proc-Type: 4,ENCRYPTED\nDEK-Info: DES-EDE3-CBC,%s\n\n" %\
tostr(hexlify(salt).upper())
# Encrypt with PKCS#7 padding
data = objenc.encrypt(pad(data, objenc.block_size))
# Each BASE64 line can take up to 64 characters (=48 bytes of data)
# b2a_base64 adds a new line character!
chunks = [tostr(b2a_base64(data[i:i + 48]))
for i in range(0, len(data), 48)]
out += "".join(chunks)
out += "-----END %s-----" % marker
return out
示例6: prf_numbytes
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import MD5 [as 别名]
def prf_numbytes(self, secret, label, random, numbytes):
hs = (len(secret)+1)/2
s1 = secret[:hs]
s2 = secret[-hs:]
#print "randlen=",len(random)
#print "hs=",hs
#print "s1=",s1
#print "s2=",s2
#print "label+random=",label+random
#print "label=",label
#1) compute P_md5(secret_part_1, label+random)
md5_hmac=''
block=HMAC.new(key=s1,
msg=label+random,
digestmod=MD5).digest()
while len(md5_hmac)<numbytes:
md5_hmac += HMAC.new(key=s1,
msg=block+label+random,
digestmod=MD5).digest()
block = HMAC.new(key=s1,
msg=block,
digestmod=MD5).digest()
#print [ "%.2x"%ord(i) for i in md5_hmac]
md5_hmac=md5_hmac[:numbytes]
# sha stuff
sha_hmac=''
block=HMAC.new(key=s2,
msg=label+random,
digestmod=SHA).digest()
while len(sha_hmac)<numbytes:
sha_hmac += HMAC.new(key=s2,
msg=block+label+random,
digestmod=SHA).digest()
block = HMAC.new(key=s2,
msg=block,
digestmod=SHA).digest()
#print [ "%.2x"%ord(i) for i in sha_hmac]
# XOR both strings
sha_hmac=sha_hmac[:numbytes]
m = array.array("B",md5_hmac)
s = array.array("B",sha_hmac)
for i in xrange(numbytes):
m[i] ^= s[i]
#print "%0.2x"%m[i],
return m.tostring()
'''
data = ''
for block in self.prf(secret,label,seed):
data +=block
if len(data)>=numbytes:
return data[:numbytes]
'''