Skip to content

Commit b97f4e0

Browse files
committed
Working on documentation.
1 parent b38ae7e commit b97f4e0

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

doc/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CryptoJWT is supposed to provide you (a Python programmer) with all you need,
1313
keyhandling
1414
jws
1515
jwe
16-
source
16+
source/index.rst
1717

1818
Indices and tables
1919
==================

doc/keyhandling.rst

+48-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ CryptoJWT deals with keys by defining 4 'layers'.
1515
a number of formats and can export a key as a JWK_.
1616
3. A :py:class:`cryptojwt.key_bundle.KeyBundle` keeps track of a set of
1717
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.
2020

2121

2222
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.
2525
JSON Web Key (JWK)
2626
------------------
2727

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+
2873

2974

3075
.. _cryptography: https://cryptography.io/en/latest/
3176
.. _JWK: https://tools.ietf.org/html/rfc7517
3277
.. _JWKS: https://tools.ietf.org/html/rfc7517#section-5
78+
.. _JWS: https://tools.ietf.org/html/rfc7515

src/cryptojwt/jwk/rsa.py

+11
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,17 @@ def __eq__(self, other):
500500

501501

502502
def new_rsa_key(key_size=2048, kid='', use='', public_exponent=65537):
503+
"""
504+
Creates a new RSA key pair and wraps it in a
505+
:py:class:`cryptojwt.jwk.rsa.RSAKey` instance
506+
507+
:param key_size: The size of the key
508+
:param kid: The key ID
509+
:param use: What the is supposed to be used for. 2 choices 'sig'/'enc'
510+
:param public_exponent: The value of the public exponent.
511+
:return: A :py:class:`cryptojwt.jwk.rsa.RSAKey` instance
512+
"""
513+
503514
_key = rsa.generate_private_key(public_exponent=public_exponent,
504515
key_size=key_size,
505516
backend=default_backend())

0 commit comments

Comments
 (0)