Created
March 5, 2013 07:22
-
-
Save nekoya/5088603 to your computer and use it in GitHub Desktop.
Python benchmark (mcrypt, Crypto)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from benchmarker import Benchmarker | |
import base64 | |
import mcrypt | |
from Crypto.Cipher import Blowfish | |
key = 'thisiskey' | |
iv = '123abc45' | |
plain_text = 'hello world\0\0\0\0\0' | |
encrypted = 'VFkA5lnNeEc0R8HeA5/7ng==' | |
mcipher = mcrypt.MCRYPT('blowfish', 'cbc') | |
mcipher.init(key, iv) | |
def mcrypt_enc(): | |
cipher = mcrypt.MCRYPT('blowfish', 'cbc') | |
cipher.init(key, iv) | |
assert base64.b64encode(cipher.encrypt(plain_text)) == encrypted | |
def mcrypt_dec(): | |
cipher = mcrypt.MCRYPT('blowfish', 'cbc') | |
cipher.init(key, iv) | |
assert cipher.decrypt(base64.b64decode(encrypted)) == plain_text | |
def mcrypt_enc_reinit(): | |
mcipher.reinit() | |
assert base64.b64encode(mcipher.encrypt(plain_text)) == encrypted | |
def mcrypt_dec_reinit(): | |
mcipher.reinit() | |
assert mcipher.decrypt(base64.b64decode(encrypted)) == plain_text | |
def crypto_enc(): | |
cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv) | |
assert base64.b64encode(cipher.encrypt(plain_text)) == encrypted | |
def crypto_dec(): | |
cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv) | |
assert cipher.decrypt(base64.b64decode(encrypted)) == plain_text | |
width = 40 | |
loop = 3000 | |
def bench_gmtime(): | |
with Benchmarker(width=width, loop=loop) as bm: | |
for _ in bm('mcrypt_enc'): | |
mcrypt_enc() | |
for _ in bm('mcrypt_dec'): | |
mcrypt_dec() | |
for _ in bm('mcrypt_enc_reinit'): | |
mcrypt_enc_reinit() | |
for _ in bm('mcrypt_dec_reinit'): | |
mcrypt_dec_reinit() | |
for _ in bm('crypto_enc'): | |
crypto_enc() | |
for _ in bm('crypto_dec'): | |
crypto_dec() | |
if __name__ == '__main__': | |
bench_gmtime() |
Author
nekoya
commented
Mar 5, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment