File tree Expand file tree Collapse file tree 4 files changed +92
-0
lines changed Expand file tree Collapse file tree 4 files changed +92
-0
lines changed Original file line number Diff line number Diff line change
1
+ """This module provides cryptographic elements needed by saml2."""
Original file line number Diff line number Diff line change
1
+ """This module provides methods for asymmetric cryptography."""
2
+
3
+ import cryptography .hazmat .backends as _backends
4
+ import cryptography .hazmat .primitives .asymmetric as _asymmetric
5
+ import cryptography .hazmat .primitives .hashes as _hashes
6
+ import cryptography .hazmat .primitives .serialization as _serialization
7
+
8
+
9
+ def load_pem_private_key (data , password ):
10
+ """Load RSA PEM certificate."""
11
+ key = _serialization .load_pem_private_key (
12
+ data , password , _backends .default_backend ())
13
+ return key
14
+
15
+
16
+ def key_sign (rsakey , message , digest ):
17
+ """Sign the given message with the RSA key."""
18
+ padding = _asymmetric .padding .PKCS1v15 ()
19
+ signature = rsakey .sign (message , padding , digest )
20
+ return signature
21
+
22
+
23
+ def key_verify (rsakey , signature , message , digest ):
24
+ """Verify the given signature with the RSA key."""
25
+ padding = _asymmetric .padding .PKCS1v15 ()
26
+ if isinstance (rsakey , _asymmetric .rsa .RSAPrivateKey ):
27
+ rsakey = rsakey .public_key ()
28
+
29
+ try :
30
+ rsakey .verify (signature , message , padding , digest )
31
+ except Exception as e :
32
+ return False
33
+ else :
34
+ return True
35
+
36
+
37
+ hashes = _hashes
Original file line number Diff line number Diff line change
1
+ """This module provides methods for PKI operations."""
2
+
3
+ import cryptography .hazmat .backends as _backends
4
+ import cryptography .x509 as _x509
5
+
6
+
7
+ def load_pem_x509_certificate (data ):
8
+ """Load X.509 PEM certificate."""
9
+ return _x509 .load_pem_x509_certificate (data , _backends .default_backend ())
Original file line number Diff line number Diff line change
1
+ """This module provides methods for symmetric cryptography.
2
+
3
+ The default symmetric cryptography method used is Fernet by the cryptography
4
+ library. Reference: https://cryptography.io/en/latest/fernet/
5
+ """
6
+
7
+ import cryptography .fernet as _fernet
8
+
9
+
10
+ class Default (object ):
11
+ """The default symmetric cryptography method."""
12
+
13
+ @staticmethod
14
+ def generate_key ():
15
+ """Return a key suitable for use by this method.
16
+
17
+ :return: byte data representing the encyption/decryption key
18
+ """
19
+ key = _fernet .Fernet .generate_key ()
20
+ return key
21
+
22
+ def __init__ (self , key = None ):
23
+ """Initialize this method by optionally providing a key.
24
+
25
+ :param key: byte data representing the encyption/decryption key
26
+ """
27
+ self ._symmetric = _fernet .Fernet (key or self .__class__ .generate_key ())
28
+
29
+ def encrypt (self , plaintext ):
30
+ """Encrypt the given plaintext.
31
+
32
+ :param plaintext: byte data representing the plaintext
33
+ :return: byte data representing the ciphertext
34
+ """
35
+ ciphertext = self ._symmetric .encrypt (plaintext )
36
+ return ciphertext
37
+
38
+ def decrypt (self , ciphertext ):
39
+ """Decrypt the given ciphertext.
40
+
41
+ :param ciphertext: byte data representing the ciphertext
42
+ :return: byte data representing the plaintext
43
+ """
44
+ plaintext = self ._symmetric .decrypt (ciphertext )
45
+ return plaintext
You can’t perform that action at this time.
0 commit comments