简体   繁体   中英

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.

单元测试

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:

使用Base64UrlEncoder

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM