@@ -15,8 +15,8 @@ CryptoJWT deals with keys by defining 4 'layers'.
15
15
a number of formats and can export a key as a JWK _.
16
16
3. A :py:class: `cryptojwt.key_bundle.KeyBundle ` keeps track of a set of
17
17
keys that has the same origin. Like being part of a JWKS _.
18
- 4 A :py:class: `cryptojwt.key_jar.KeyJar ` lastly is there to sort the keys
19
- by who owns them .
18
+ 4. A :py:class: `cryptojwt.key_jar.KeyJar ` lastly is there to sort the keys
19
+ by their owners/issuers .
20
20
21
21
22
22
I will not describe who to deal with keys in layer 1, that is done best by
@@ -25,8 +25,54 @@ cryptography_. So, I'll start at layer 2.
25
25
JSON Web Key (JWK)
26
26
------------------
27
27
28
+ Let us start with you not having any key at all and you want to create a
29
+ signed JSON Web Token (JWS _).
30
+ What to do ?
31
+
32
+ Well if you know what kind of key you want, if it is a asymmetric key you can
33
+ use one of the provided factory methods.
34
+
35
+ RSA
36
+ :py:func: `cryptojwt.jwk.rsa.new_rsa_key `
37
+ Elliptic Curve:
38
+ :py:func: `cryptojwt.jwk.ec.new_ec_key `
39
+
40
+
41
+ As an example::
42
+
43
+ >>> from cryptojwt.jwk.rsa import new_rsa_key
44
+ >>> rsa_key = new_rsa_key()
45
+ >>> type(rsa_key)
46
+ <class 'cryptojwt.jwk.rsa.RSAKey'>
47
+
48
+
49
+ If you want a symmetric key you only need some sort of "secure random"
50
+ mechanism. You can use this to acquire a byte array of the appropriate length
51
+ (e.g. 32 bytes for AES256), which can be used as a key.
52
+
53
+ If you already has a key, like if you have a PEM encoded private RSA key in
54
+ a file on your machine you can load it this way::
55
+
56
+ >>> from cryptojwt.jwk.rsa import RSAKey
57
+ >>> rsa_key = RSAKey().load('key.pem')
58
+ >>> rsa_key.has_private_key()
59
+ True
60
+
61
+ If you have a PEM encoded X.509 certificate you may want to grab the public
62
+ RSA key from you could do like this::
63
+
64
+ >>> from cryptojwt.jwk.rsa import import_rsa_key_from_cert_file
65
+ >>> from cryptojwt.jwk.rsa import RSAKey
66
+ >>> _key = import_rsa_key_from_cert_file('cert.pem')
67
+ >>> rsa_key = RSAKey(pub_key=_key)
68
+ >>> rsa_key.has_private_key()
69
+ False
70
+ >>> rsa_key.public_key()
71
+ <cryptography.hazmat.backends.openssl.rsa._RSAPublicKey object at 0x1036b1f60>
72
+
28
73
29
74
30
75
.. _cryptography : https://cryptography.io/en/latest/
31
76
.. _JWK : https://tools.ietf.org/html/rfc7517
32
77
.. _JWKS : https://tools.ietf.org/html/rfc7517#section-5
78
+ .. _JWS : https://tools.ietf.org/html/rfc7515
0 commit comments