3 from cryptography
import x509
4 from cryptography.exceptions
import InvalidSignature
5 from cryptography.hazmat.backends
import default_backend
6 from cryptography.hazmat.primitives
import serialization
7 from cryptography.hazmat.primitives
import hashes
8 from cryptography.hazmat.primitives
import hmac
9 from cryptography.hazmat.primitives.asymmetric
import padding
10 from cryptography.hazmat.primitives.ciphers
import Cipher
11 from cryptography.hazmat.primitives.ciphers
import algorithms
12 from cryptography.hazmat.primitives.ciphers
import modes
16 _, ext = os.path.splitext(path)
17 with open(path,
"rb")
as f:
19 return x509.load_pem_x509_certificate(f.read(), default_backend())
21 return x509.load_der_x509_certificate(f.read(), default_backend())
27 return x509.load_der_x509_certificate(data, default_backend())
31 _, ext = os.path.splitext(path)
32 with open(path,
"rb")
as f:
34 return serialization.load_pem_private_key(f.read(), password=
None, backend=default_backend())
36 return serialization.load_der_private_key(f.read(), password=
None, backend=default_backend())
40 if certificate
is None:
42 return certificate.public_bytes(serialization.Encoding.DER)
46 signer = private_key.signer(
51 return signer.finalize()
55 verifier = certificate.public_key().verifier(
64 ciphertext = public_key.encrypt(
67 mgf=padding.MGF1(algorithm=hashes.SHA256()),
68 algorithm=hashes.SHA256(),
75 ciphertext = public_key.encrypt(
78 mgf=padding.MGF1(algorithm=hashes.SHA1()),
79 algorithm=hashes.SHA1(),
86 ciphertext = public_key.encrypt(
94 text = private_key.decrypt(
97 mgf=padding.MGF1(algorithm=hashes.SHA1()),
98 algorithm=hashes.SHA1(),
105 text = private_key.decrypt(
113 return Cipher(algorithms.AES(key), modes.CBC(init_vec), default_backend())
117 encryptor = cipher.encryptor()
118 return encryptor.update(data) + encryptor.finalize()
122 decryptor = cipher.decryptor()
123 return decryptor.update(data) + decryptor.finalize()
127 hasher = hmac.HMAC(key, hashes.SHA1(), backend=default_backend())
128 hasher.update(message)
129 return hasher.finalize()
133 return hashes.SHA1.digest_size
138 Derive one or more keys from secret and seed. 139 (See specs part 6, 6.7.5 and RFC 2246 - TLS v1.0) 140 Lengths of keys will match sizes argument 148 while len(result) < full_size:
150 result +=
hmac_sha1(secret, accum + seed)
154 parts.append(result[:size])
155 result = result[size:]
160 parts = [
"{0}={1}".format(attr.oid._name, attr.value)
for attr
in name]
161 return ', '.join(parts)
166 Convert x509 certificate to human-readable string 168 if cert.subject == cert.issuer:
169 issuer =
' (self-signed)' 173 return "{0}{1}, {2} - {3}".format(
x509_name_to_string(cert.subject), issuer, cert.not_valid_before, cert.not_valid_after)
176 if __name__ ==
"__main__":
182 from IPython
import embed
def encrypt_basic256(public_key, data)
def der_from_x509(certificate)
def p_sha1(secret, seed, sizes=())
def encrypt_rsa15(public_key, data)
def load_certificate(path)
def decrypt_rsa_oaep(private_key, data)
def cipher_encrypt(cipher, data)
def hmac_sha1(key, message)
def verify_sha1(certificate, data, signature)
def cipher_aes_cbc(key, init_vec)
def sign_sha1(private_key, data)
def cipher_decrypt(cipher, data)
def decrypt_rsa15(private_key, data)
def encrypt_rsa_oaep(public_key, data)
def x509_name_to_string(name)
def load_private_key(path)