@@ -9,3 +9,52 @@ or Message Authentication Codes (MACs) using JSON-based data structures.
99It's assumed that you know all you need to know about key handling if not
1010please spend some time reading keyhandling _ .
1111
12+ When it comes to JWS there are basically 2 things you want to be able to do: sign some data and verify that a
13+ signature over some data is correct. I'll deal with them in that order.
14+
15+ Signing a document
16+ ------------------
17+
18+ There are few steps you have to go through. Let us start with an example and then break it into its parts::
19+
20+ >>> from cryptojwt.jwk.hmac import SYMKey
21+ >>> from cryptojwt.jws.jws import JWS
22+
23+ >>> key = SYMKey(key=b'My hollow echo chamber', alg="HS512")
24+ >>> payload = "Please take a moment to register today"
25+ >>> _signer = JWS(payload, alg="HS512")
26+ >>> _jws = _signer.sign_compact([key])
27+
28+ The steps:
29+
30+ 1. You need keys, one of more. If you provide more then one the software will pick one that has all the necessary
31+ qualifications. The keys *MUST * be instances of :py:class: `cryptojwt.jwk.JWK ` or of sub classes of that class.
32+ 2. You need the information that are to be signed. It must be in the form of a string.
33+ 3. You initiate the signer, providing it with the message and other needed information.
34+ 4. You sign using the compact or the JSON method as described in section 7 of RFC7515 _ .
35+
36+
37+ Verifying a signature
38+ ---------------------
39+
40+ Verifying a signature works like this::
41+
42+ >>> from cryptojwt.jwk.hmac import SYMKey
43+ >>> from cryptojwt.jws.jws import JWS
44+
45+ >>> key = SYMKey(key=b'My hollow echo chamber', alg="HS512")
46+ >>> _verifier = JWS(alg="HS512")
47+ >>> _msg = _verifier.verify_compact([key])
48+ >>> print(_msg)
49+ "Please take a moment to register today"
50+
51+ The steps:
52+
53+ 1. As with signing, you need a set of keys that can be used to verify the signature. If you provider more then
54+ one possible, then the default is to use then one by one until one works or the list is empty.
55+ 2. Initiate the verifier. If you have a reason to expect that a particular signing algorithm is to be used you
56+ should give that information to the verifier as shown here. If you don't know you can leave it out.
57+ 3. Verify, using the compact or JSON method.
58+
59+
60+ .. _RFC7515 : https://tools.ietf.org/html/rfc7515
0 commit comments