From: Christophe Oosterlynck Date: Thu, 23 Oct 2008 20:08:23 +0000 (+0200) Subject: Hash: added doctests + little fixes X-Git-Url: https://repo.or.cz/w/python-cryptoplus.git/commitdiff_plain/3594fc0e475c8671d540e7edc6464b9025f0f5cd Hash: added doctests + little fixes --- diff --git a/src/CryptoPlus/Hash/RIPEMD.py b/src/CryptoPlus/Hash/RIPEMD.py dissimilarity index 98% index fefdfd6..e4f889e 100644 --- a/src/CryptoPlus/Hash/RIPEMD.py +++ b/src/CryptoPlus/Hash/RIPEMD.py @@ -1,4 +1,26 @@ -import Crypto.Hash.RIPEMD - -def new(): - return +from Crypto.Hash import RIPEMD + +def new(data=""): + """Create a new RIPEMD-160 hash object + + data = initial input (raw string) to the hashing object + if present, the method call update(arg) is made + + EXAMPLE: + ========= + + >>> from CryptoPlus.Hash import RIPEMD + + >>> message = "abc" + >>> hasher = RIPEMD.new() + >>> hasher.update(message) + >>> hasher.hexdigest() + '8eb208f7e05d987a9b044a8e98c6b087f15a0bfc' + + >>> message = "message digest" + >>> hasher = RIPEMD.new() + >>> hasher.update(message) + >>> hasher.hexdigest() + '5d0689ef49d2fae572b881b123a85ffa21595f36' + """ + return RIPEMD.new(data) diff --git a/src/CryptoPlus/Hash/__init__.py b/src/CryptoPlus/Hash/__init__.py index c059fcd..af5d8ad 100644 --- a/src/CryptoPlus/Hash/__init__.py +++ b/src/CryptoPlus/Hash/__init__.py @@ -1,7 +1,7 @@ # hash functions of pycrypto can be just imported # wrapping might be a better idea if docstrings need to be expanded # wrapping in Cipher was needed to make the new chaining modes available -from Crypto.Hash import RIPEMD, SHA, SHA256, MD5, MD2, MD4, HMAC +from Crypto.Hash import SHA, SHA256, MD5, MD2, MD4, HMAC __all__ = ["SHA","SHA256","MD5","MD2","MD4","HMAC","RIPEMD"] diff --git a/src/CryptoPlus/Hash/pysha224.py b/src/CryptoPlus/Hash/pysha224.py index 4827969..ca96a9f 100644 --- a/src/CryptoPlus/Hash/pysha224.py +++ b/src/CryptoPlus/Hash/pysha224.py @@ -1 +1 @@ -#!/usr/bin/python __author__ = 'Thomas Dixon' __license__ = 'MIT' from sha256 import sha256 def new(m=None): return sha224(m) class sha224(sha256): _h = (0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4) _output_size = 7 digest_size = 28 \ No newline at end of file +#!/usr/bin/python __author__ = 'Thomas Dixon' __license__ = 'MIT' from pysha256 import sha256 def new(m=None): return sha224(m) class sha224(sha256): _h = (0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4) _output_size = 7 digest_size = 28 \ No newline at end of file diff --git a/src/CryptoPlus/Hash/pysha384.py b/src/CryptoPlus/Hash/pysha384.py index 73a41f3..e58e6ad 100644 --- a/src/CryptoPlus/Hash/pysha384.py +++ b/src/CryptoPlus/Hash/pysha384.py @@ -1 +1 @@ -#!/usr/bin/python __author__ = 'Thomas Dixon' __license__ = 'MIT' from sha512 import sha512 def new(m=None): return sha384(m) class sha384(sha512): _h = (0xcbbb9d5dc1059ed8L, 0x629a292a367cd507L, 0x9159015a3070dd17L, 0x152fecd8f70e5939L, 0x67332667ffc00b31L, 0x8eb44a8768581511L, 0xdb0c2e0d64f98fa7L, 0x47b5481dbefa4fa4L) _output_size = 6 digest_size = 48 \ No newline at end of file +#!/usr/bin/python __author__ = 'Thomas Dixon' __license__ = 'MIT' from pysha512 import sha512 def new(m=None): return sha384(m) class sha384(sha512): _h = (0xcbbb9d5dc1059ed8L, 0x629a292a367cd507L, 0x9159015a3070dd17L, 0x152fecd8f70e5939L, 0x67332667ffc00b31L, 0x8eb44a8768581511L, 0xdb0c2e0d64f98fa7L, 0x47b5481dbefa4fa4L) _output_size = 6 digest_size = 48 \ No newline at end of file diff --git a/src/CryptoPlus/Hash/python_MD5.py b/src/CryptoPlus/Hash/python_MD5.py index cbcc208..ed26b93 100644 --- a/src/CryptoPlus/Hash/python_MD5.py +++ b/src/CryptoPlus/Hash/python_MD5.py @@ -3,6 +3,28 @@ import pymd5 __all__ = ['new','digest_size'] def new(data=""): + """Create a new pure python MD5 hash object + + data = initial input (raw string) to the hashing object + if present, the method call update(arg) is made + + EXAMPLE: (http://www.rfc-editor.org/rfc/rfc1321.txt) + ========= + + >>> from CryptoPlus.Hash import MD5 + + >>> message = "abc" + >>> hasher = MD5.new() + >>> hasher.update(message) + >>> hasher.hexdigest() + '900150983cd24fb0d6963f7d28e17f72' + + >>> message = "message digest" + >>> hasher = MD5.new() + >>> hasher.update(message) + >>> hasher.hexdigest() + 'f96b697d7cb7938d525a2f31aaf161d0' + """ return pymd5.new(data) digest_size = pymd5.digest_size diff --git a/src/CryptoPlus/Hash/python_RadioGatun.py b/src/CryptoPlus/Hash/python_RadioGatun.py index d42857f..e823598 100644 --- a/src/CryptoPlus/Hash/python_RadioGatun.py +++ b/src/CryptoPlus/Hash/python_RadioGatun.py @@ -2,12 +2,12 @@ from pyradiogatun import RadioGatunType __all__ = ['new'] -def new(wl=64,arg=None): - """Return a new RadioGatun hash object +def new(wl=64,data=None): + """Create a new pure python RadioGatun hash object - wl = wordlength (in bits) of the RadioGatun hash method + wl = wordlength (in bits) of the RadioGatun hash method between 1 and 64 (default = 64) - arg = if present, the method call update(arg) is made + data = if present, the method call update(arg) is made EXAMPLES: (testvectors from: http://radiogatun.noekeon.org/) ========== @@ -39,8 +39,8 @@ def new(wl=64,arg=None): """ crypto = RadioGatunType(wl) - if arg: - crypto.update(arg) + if data: + crypto.update(data) return crypto diff --git a/src/CryptoPlus/Hash/python_SHA.py b/src/CryptoPlus/Hash/python_SHA.py index e9bef79..e173e05 100644 --- a/src/CryptoPlus/Hash/python_SHA.py +++ b/src/CryptoPlus/Hash/python_SHA.py @@ -3,6 +3,28 @@ import pysha __all__ = ['new','digest_size'] def new(data=""): + """Create a new pure python SHA hash object + + data = initial input (raw string) to the hashing object + if present, the method call update(arg) is made + + EXAMPLE: FIPS 180-2 + ========= + + >>> from CryptoPlus.Hash import python_SHA + + >>> message = "abc" + >>> hasher = python_SHA.new() + >>> hasher.update(message) + >>> hasher.hexdigest() + 'a9993e364706816aba3e25717850c26c9cd0d89d' + + >>> message = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + >>> hasher = python_SHA.new() + >>> hasher.update(message) + >>> hasher.hexdigest() + '84983e441c3bd26ebaae4aa1f95129e5e54670f1' + """ return pysha.new(data) digest_size = pysha.digest_size diff --git a/src/CryptoPlus/Hash/python_SHA224.py b/src/CryptoPlus/Hash/python_SHA224.py index c5a107b..c69a56f 100644 --- a/src/CryptoPlus/Hash/python_SHA224.py +++ b/src/CryptoPlus/Hash/python_SHA224.py @@ -3,6 +3,28 @@ from pysha224 import sha224 __all__ = ['new','digest_size'] def new(data=""): + """Create a new pure python SHA-224 hash object + + data = initial input (raw string) to the hashing object + if present, the method call update(arg) is made + + EXAMPLE: FIPS 180-2 + ========= + + >>> from CryptoPlus.Hash import python_SHA224 + + >>> message = "abc" + >>> hasher = python_SHA224.new() + >>> hasher.update(message) + >>> hasher.hexdigest() + '23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7' + + >>> message = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + >>> hasher = python_SHA224.new() + >>> hasher.update(message) + >>> hasher.hexdigest() + '75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525' + """ return sha224(data) digest_size = sha224.digest_size diff --git a/src/CryptoPlus/Hash/python_SHA256.py b/src/CryptoPlus/Hash/python_SHA256.py index e8f26d6..2c771fb 100644 --- a/src/CryptoPlus/Hash/python_SHA256.py +++ b/src/CryptoPlus/Hash/python_SHA256.py @@ -3,6 +3,28 @@ from pysha256 import sha256 __all__ = ['new','digest_size'] def new(data=""): + """Create a new pure python SHA-256 hash object + + data = initial input (raw string) to the hashing object + if present, the method call update(arg) is made + + EXAMPLE: FIPS 180-2 + ========= + + >>> from CryptoPlus.Hash import python_SHA256 + + >>> message = "abc" + >>> hasher = python_SHA256.new() + >>> hasher.update(message) + >>> hasher.hexdigest() + 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad' + + >>> message = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + >>> hasher = python_SHA256.new() + >>> hasher.update(message) + >>> hasher.hexdigest() + '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1' + """ return sha256(data) digest_size = sha256.digest_size diff --git a/src/CryptoPlus/Hash/python_SHA384.py b/src/CryptoPlus/Hash/python_SHA384.py index 94db6d7..8478296 100644 --- a/src/CryptoPlus/Hash/python_SHA384.py +++ b/src/CryptoPlus/Hash/python_SHA384.py @@ -3,6 +3,28 @@ from pysha384 import sha384 __all__ = ['new','digest_size'] def new(data=""): + """Create a new pure python SHA-384 hash object + + data = initial input (raw string) to the hashing object + if present, the method call update(arg) is made + + EXAMPLE: FIPS 180-2 + ========= + + >>> from CryptoPlus.Hash import python_SHA384 + + >>> message = "abc" + >>> hasher = python_SHA384.new() + >>> hasher.update(message) + >>> hasher.hexdigest() + 'cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7' + + >>> message = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" + >>> hasher = python_SHA384.new() + >>> hasher.update(message) + >>> hasher.hexdigest() + '09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039' + """ return sha384(data) digest_size = sha384.digest_size diff --git a/src/CryptoPlus/Hash/python_SHA512.py b/src/CryptoPlus/Hash/python_SHA512.py index 554fdaf..a869803 100644 --- a/src/CryptoPlus/Hash/python_SHA512.py +++ b/src/CryptoPlus/Hash/python_SHA512.py @@ -3,6 +3,28 @@ from pysha512 import sha512 __all__ = ['new','digest_size'] def new(data=""): + """Create a new pure python SHA-512 hash object + + data = initial input (raw string) to the hashing object + if present, the method call update(arg) is made + + EXAMPLE: FIPS 180-2 + ========= + + >>> from CryptoPlus.Hash import python_SHA512 + + >>> message = "abc" + >>> hasher = python_SHA512.new() + >>> hasher.update(message) + >>> hasher.hexdigest() + 'ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f' + + >>> message = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" + >>> hasher = python_SHA512.new() + >>> hasher.update(message) + >>> hasher.hexdigest() + '8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909' + """ return sha512(data) digest_size = sha512.digest_size diff --git a/src/CryptoPlus/Hash/python_whirlpool.py b/src/CryptoPlus/Hash/python_whirlpool.py index 8c24639..b8f223b 100644 --- a/src/CryptoPlus/Hash/python_whirlpool.py +++ b/src/CryptoPlus/Hash/python_whirlpool.py @@ -3,6 +3,28 @@ import pywhirlpool __all__ = ['new','digest_size'] def new(data=""): + """Create a new pure python Whirlpool hash object + + data = initial input (raw string) to the hashing object + if present, the method call update(arg) is made + + EXAMPLE: (http://paginas.terra.com.br/informatica/paulobarreto/WhirlpoolPage.html) + ========= + + >>> from CryptoPlus.Hash import python_whirlpool + + >>> message = "abc" + >>> hasher = python_whirlpool.new() + >>> hasher.update(message) + >>> hasher.hexdigest().upper() + '4E2448A4C6F486BB16B6562C73B4020BF3043E3A731BCE721AE1B303D97E6D4C7181EEBDB6C57E277D0E34957114CBD6C797FC9D95D8B582D225292076D4EEF5' + + >>> message = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" + >>> hasher = python_whirlpool.new() + >>> hasher.update(message) + >>> hasher.hexdigest().upper() + 'DC37E008CF9EE69BF11F00ED9ABA26901DD7C28CDEC066CC6AF42E40F82F3A1E08EBA26629129D8FB7CB57211B9281A65517CC879D7B962142C65F5A7AF01467' + """ return pywhirlpool.new(data) digest_size = pywhirlpool.digest_size diff --git a/src/CryptoPlus/SelfTest/__init__.py b/src/CryptoPlus/SelfTest/__init__.py index 06d9ec1..82aac4e 100644 --- a/src/CryptoPlus/SelfTest/__init__.py +++ b/src/CryptoPlus/SelfTest/__init__.py @@ -79,8 +79,8 @@ def get_tests(): import Cipher; tests += Cipher.get_tests() import Hash; tests += Hash.get_tests() import PublicKey; tests += PublicKey.get_tests() - import Random; tests += Random.get_tests() - import Util; tests += Util.get_tests() +# import Random; tests += Random.get_tests() +# import Util; tests += Util.get_tests() return tests if __name__ == '__main__': diff --git a/test/test_doctests.py b/test/test_doctests.py index a42e4de..dcd1df2 100644 --- a/test/test_doctests.py +++ b/test/test_doctests.py @@ -9,7 +9,9 @@ from CryptoPlus.Cipher import python_AES, AES, python_DES, DES, python_DES3,\ DES3, python_Blowfish, Blowfish, python_Twofish, python_Serpent,\ python_Rijndael, CAST, ARC2, python_PRESENT from CryptoPlus.Util import padding -from CryptoPlus.Hash import python_RadioGatun, python_PBKDF2 +from CryptoPlus.Hash import python_RadioGatun, python_PBKDF2, RIPEMD, python_MD5,\ + python_SHA,python_SHA256,python_SHA224,python_SHA384,python_SHA512,\ + python_whirlpool try: from CryptoPlus.Cipher import IDEA from CryptoPlus.Cipher import RC5 @@ -21,7 +23,9 @@ suite = unittest.TestSuite() #for mod in (CryptoPlus.Cipher.python_AES,CryptoPlus.Cipher.python_AES): for mod in python_AES, AES, python_DES, DES, python_DES3, DES3, python_Blowfish,\ Blowfish, python_Twofish, python_Serpent, python_Rijndael, CAST, ARC2,\ - python_PRESENT, padding, python_RadioGatun, python_PBKDF2: + python_PRESENT, padding, python_RadioGatun, python_PBKDF2, RIPEMD,\ + python_MD5, python_SHA,python_SHA256,python_SHA224,python_SHA384,python_SHA512,\ + python_whirlpool: suite.addTest(doctest.DocTestSuite(mod)) if not import_error: suite.addTest(doctest.DocTestSuite(IDEA))