Skip to content

Instantly share code, notes, and snippets.

@mithrandi
Created March 30, 2016 15:34
Show Gist options
  • Save mithrandi/85257ca7cd6305cd7b30ae0a6ca811d2 to your computer and use it in GitHub Desktop.
Save mithrandi/85257ca7cd6305cd7b30ae0a6ca811d2 to your computer and use it in GitHub Desktop.
Cryptography X509 builder benchmark
from __future__ import print_function
import time
from datetime import datetime, timedelta
from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.x509.oid import NameOID
key = rsa.generate_private_key(
public_exponent=65537, key_size=2048, backend=default_backend())
name = x509.Name([
x509.NameAttribute(NameOID.COMMON_NAME, u'example.com')])
def chained():
return (
x509.CertificateBuilder()
.subject_name(name)
.issuer_name(name)
.not_valid_before(datetime.now() - timedelta(seconds=3600))
.not_valid_after(datetime.now() + timedelta(seconds=3600))
.serial_number(1)
.public_key(key.public_key())
.sign(
private_key=key,
algorithm=hashes.SHA256(),
backend=default_backend()))
def unchained():
return (
x509.CertificateBuilder(
subject_name=name,
issuer_name=name,
not_valid_before=datetime.now() - timedelta(seconds=3600),
not_valid_after=datetime.now() + timedelta(seconds=3600),
serial_number=1,
public_key=key.public_key())
.sign(
private_key=key,
algorithm=hashes.SHA256(),
backend=default_backend()))
def bench(f, calibration_time=0.1):
start = time.time()
n = 0
while time.time() < start + calibration_time:
f()
n += 1
iterations = int(n / calibration_time * 5)
start = time.time()
for _ in xrange(iterations):
f()
end = time.time()
return (end - start) / iterations * 1000, iterations / (end - start)
def main(s):
if s == '0':
print('Chained style: {}'.format(bench(chained)))
elif s == '1':
print('Unhained style: {}'.format(bench(unchained)))
if __name__ == '__main__':
import sys
main(*sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment