PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/pycrypto/pct-speedtest.py

https://gitlab.com/Smileyt/KomodoEdit
Python | 220 lines | 149 code | 35 blank | 36 comment | 23 complexity | c522a49e3f88189cea8d4754a02b67f9 MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # pct-speedtest.py: Speed test for the Python Cryptography Toolkit
  5. #
  6. # Written in 2009 by Dwayne C. Litzenberger <dlitz@dlitz.net>
  7. #
  8. # ===================================================================
  9. # The contents of this file are dedicated to the public domain. To
  10. # the extent that dedication to the public domain is not available,
  11. # everyone is granted a worldwide, perpetual, royalty-free,
  12. # non-exclusive license to exercise all rights associated with the
  13. # contents of this file for any purpose whatsoever.
  14. # No rights are reserved.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. # SOFTWARE.
  24. # ===================================================================
  25. import time
  26. import os
  27. import sys
  28. from Crypto.PublicKey import RSA
  29. from Crypto.Cipher import AES, ARC2, ARC4, Blowfish, CAST, DES3, DES, XOR
  30. from Crypto.Hash import MD2, MD4, MD5, SHA256, SHA
  31. try:
  32. from Crypto.Hash import RIPEMD
  33. except ImportError: # Some builds of PyCrypto don't have the RIPEMD module
  34. RIPEMD = None
  35. class Benchmark:
  36. def __init__(self):
  37. self.__random_data = None
  38. def random_keys(self, bytes):
  39. """Return random keys of the specified number of bytes.
  40. If this function has been called before with the same number of bytes,
  41. cached keys are used instead of randomly generating new ones.
  42. """
  43. return self.random_blocks(bytes, 10**5) # 100k
  44. def random_blocks(self, bytes_per_block, blocks):
  45. bytes = bytes_per_block * blocks
  46. data = self.random_data(bytes)
  47. retval = []
  48. for i in xrange(blocks):
  49. p = i * bytes_per_block
  50. retval.append(data[p:p+bytes_per_block])
  51. return retval
  52. def random_data(self, bytes):
  53. if self.__random_data is None:
  54. self.__random_data = self._random_bytes(bytes)
  55. return self.__random_data
  56. elif bytes == len(self.__random_data):
  57. return self.__random_data
  58. elif bytes < len(self.__random_data):
  59. return self.__random_data[:bytes]
  60. else:
  61. self.__random_data += self._random_bytes(bytes - len(self.__random_data))
  62. return self.__random_data
  63. def _random_bytes(self, b):
  64. return os.urandom(b)
  65. def announce_start(self, test_name):
  66. sys.stdout.write("%s: " % (test_name,))
  67. sys.stdout.flush()
  68. def announce_result(self, value, units):
  69. sys.stdout.write("%.2f %s\n" % (value, units))
  70. sys.stdout.flush()
  71. def test_pubkey_setup(self, pubkey_name, module, key_bytes):
  72. self.announce_start("%s pubkey setup" % (pubkey_name,))
  73. keys = self.random_keys(key_bytes)[:5]
  74. t0 = time.time()
  75. for k in keys:
  76. module.generate(key_bytes*8)
  77. t = time.time()
  78. pubkey_setups_per_second = len(keys) / (t - t0)
  79. self.announce_result(pubkey_setups_per_second, "Keys/sec")
  80. def test_key_setup(self, cipher_name, module, key_bytes, mode):
  81. self.announce_start("%s key setup" % (cipher_name,))
  82. # Generate random keys for use with the tests
  83. keys = self.random_keys(key_bytes)
  84. # Perform key setups
  85. if mode is None:
  86. t0 = time.time()
  87. for k in keys:
  88. module.new(k)
  89. t = time.time()
  90. else:
  91. t0 = time.time()
  92. for k in keys:
  93. module.new(k, module.MODE_ECB)
  94. t = time.time()
  95. key_setups_per_second = len(keys) / (t - t0)
  96. self.announce_result(key_setups_per_second/1000, "kKeys/sec")
  97. def test_encryption(self, cipher_name, module, key_bytes, mode):
  98. self.announce_start("%s encryption" % (cipher_name,))
  99. # Generate random keys for use with the tests
  100. rand = self.random_data(key_bytes + module.block_size)
  101. key, iv = rand[:key_bytes], rand[key_bytes:]
  102. blocks = self.random_blocks(16384, 1000)
  103. if mode is None:
  104. cipher = module.new(key)
  105. else:
  106. cipher = module.new(key, mode, iv)
  107. # Perform encryption
  108. t0 = time.time()
  109. for b in blocks:
  110. cipher.encrypt(b)
  111. t = time.time()
  112. encryption_speed = (len(blocks) * len(blocks[0])) / (t - t0)
  113. self.announce_result(encryption_speed / 10**6, "MBps")
  114. def test_hash_small(self, hash_name, module):
  115. self.announce_start("%s (%d-byte inputs)" % (hash_name, module.digest_size))
  116. blocks = self.random_blocks(module.digest_size, 10000)
  117. # Initialize hashes
  118. t0 = time.time()
  119. for b in blocks:
  120. module.new(b).digest()
  121. t = time.time()
  122. hashes_per_second = len(blocks) / (t - t0)
  123. self.announce_result(hashes_per_second / 1000, "kHashes/sec")
  124. def test_hash_large(self, hash_name, module):
  125. self.announce_start("%s (single large input)" % (hash_name,))
  126. blocks = self.random_blocks(16384, 10000)
  127. # Perform hashing
  128. t0 = time.time()
  129. h = module.new()
  130. for b in blocks:
  131. h.update(b)
  132. h.digest()
  133. t = time.time()
  134. hash_speed = len(blocks) * len(blocks[0]) / (t - t0)
  135. self.announce_result(hash_speed / 10**6, "MBps")
  136. def run(self):
  137. pubkey_specs = [
  138. ("RSA(1024)", RSA, 1024/8),
  139. ("RSA(2048)", RSA, 2048/8),
  140. ("RSA(4096)", RSA, 4096/8),
  141. ]
  142. block_specs = [
  143. ("DES", DES, 8),
  144. ("DES3", DES3, 24),
  145. ("AES128", AES, 16),
  146. ("AES192", AES, 24),
  147. ("AES256", AES, 32),
  148. ("Blowfish(256)", Blowfish, 32),
  149. ("CAST(40)", CAST, 5),
  150. ("CAST(80)", CAST, 10),
  151. ("CAST(128)", CAST, 16),
  152. ]
  153. stream_specs = [
  154. ("ARC2(128)", ARC2, 16),
  155. ("ARC4(128)", ARC4, 16),
  156. ("XOR(24)", XOR, 3),
  157. ("XOR(256)", XOR, 32),
  158. ]
  159. hash_specs = [
  160. ("MD2", MD2),
  161. ("MD4", MD4),
  162. ("MD5", MD5),
  163. ("SHA", SHA),
  164. ("SHA256", SHA256),
  165. ]
  166. if RIPEMD is not None:
  167. hash_specs += [("RIPEMD", RIPEMD)]
  168. for pubkey_name, module, key_bytes in pubkey_specs:
  169. self.test_pubkey_setup(pubkey_name, module, key_bytes)
  170. for cipher_name, module, key_bytes in block_specs:
  171. self.test_key_setup(cipher_name, module, key_bytes, module.MODE_CBC)
  172. self.test_encryption("%s-CBC" % (cipher_name,), module, key_bytes, module.MODE_CBC)
  173. self.test_encryption("%s-CFB" % (cipher_name,), module, key_bytes, module.MODE_CFB)
  174. self.test_encryption("%s-PGP" % (cipher_name,), module, key_bytes, module.MODE_PGP)
  175. self.test_encryption("%s-OFB" % (cipher_name,), module, key_bytes, module.MODE_OFB)
  176. for cipher_name, module, key_bytes in stream_specs:
  177. self.test_key_setup(cipher_name, module, key_bytes, None)
  178. self.test_encryption(cipher_name, module, key_bytes, None)
  179. for hash_name, module in hash_specs:
  180. self.test_hash_small(hash_name, module)
  181. self.test_hash_large(hash_name, module)
  182. if __name__ == '__main__':
  183. Benchmark().run()
  184. # vim:set ts=4 sw=4 sts=4 expandtab: