Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate the same signature as the JWT.IO website does?

The JWT.IO website has a debugger for creating and validating JSON Web Token.

When I copy and paste my secret string into the VERIFY SIGNATURE block, I can see that it generates a signature.

JWT.IO

I scroll down the page a little bit and found the .NET solution implemented by Microsoft. After downloading, I add my unit test to generate the signature manually.

Unit Test

However, the signature generated by the JWT.IO website is slightly different from the one generated by my unit test.

Secrect string: "THIS IS A SECRET STRING WHICH CAN BE SHARED BETWEEN THE SERVICES"
Unit test signature: "B0d57pcHoDiTDE/98dyrMx9HoFKGLOcM094eYBgJqHo="
JWT.IO    signature:  B0d57pcHoDiTDE_98dyrMx9HoFKGLOcM094eYBgJqHo

I notice that the JWT.IO signature string is URL encoding safe, but the unit test signature is not.

How do I generate the same signature as the JWT.IO website does?

UPDATE

The accepted answer below pointed me to the class Base64UrlEncoder. I have modified my unit test to generate the exact same signature as JWT.IO web site does:

Use Base64UrlEncoder

like image 576
Believe2014 Avatar asked Jul 04 '16 19:07

Believe2014


1 Answers

@Believe2014 It seems that the jwt.io website does not have some options for specifying the base64 encoding with which way of URLEncoding to generate the same signature as your unit test.

However, according to the section 4 & 5 of RFC 4648, the differences between both signatures are using different characters for byte 62 & 63 and whether omits padding characters =.

So for converting the signature generated by jwt.io into yours, just using / instead of _, using + instead of -, and adding the padding characters = at the end of signature.

like image 78
Peter Pan Avatar answered Sep 27 '22 19:09

Peter Pan