I'm a newbie that is studying about JWT using RS256 algorithm for signing and verification. I have drawn a diagram that represents my understanding of how RS256 works.
Basically, below is what I have by far:
- header and payload after being trimmed are both base64url encoded
- base64url encoded header and payload are hashed using SHA256
- 2 resulting hashes from above will be joined by a dot "." to produce M -> M = "hash_header.hash_payload"
- M (string) and a RSA generated private key will all be converted to binary form
- RSA receive message M (binary) and private key (binary) as input and produce an output C (binary)
- C (binary) is converted to hex and then base64url encoded to produce the final signature for the JWT token
Am I getting something wrong? I'm all ears for any comments!
Update 1
After having thoroughly read through the answer provided, I still couldn't grasp the overall picture of how RS256 should work. I appreciate the details but for a beginner like me without many resources to learn from, I just want to first understand the gist of the algorithm. Below is my revised diagram for how to create a JWT token and what I have got so far:
- Generate public and private key pair with OpenSSL for example
- Signature is generated using RS256 with BASE64URL(HEADER), BASE64URL(PAYLOAD) and RSA Private Key
- Public Key is included in Protected Header
- JWT in JWS format is formed by BASE64URL(UTF8(JWS Protected Header)) concat "." concat BASE64URL(JWS Payload) concat "." concat BASE64URL(JWS Signature)
Is this the correct interpretation? Much appreciation for your insight!
Update 2
Thanks to dave_thompson_085, I have modified the diagram and the result is as follows:
- Generate public and private key pair with OpenSSL for example
- Signature is generated using RS256 with BASE64URL(HEADER) period BASE64URL(PAYLOAD) period RSA Private Key
- Public Key ID (kid) is included in Protected Header
- JWT in JWS format is formed by BASE64URL(UTF8(JWS Protected Header)) concat "." concat BASE64URL(JWS Payload) concat "." concat BASE64URL(JWS Signature)