← Lessons/Encoding, Hashing, HMAC & Encryption
← Encoding, Hashing, HMAC & Encryption

Convert to base64url

Learn

base64url: the variant JWTs use

+ and / mean something in URLs, and = means something in query strings. So JWTs (and many web formats) use base64url:

  • + becomes -, and / becomes _
  • trailing = padding is removed
printf '>>>???' | base64                                 # Pj4+Pz8/
printf '>>>???' | base64 | tr '+/' '-_' | tr -d '='      # Pj4-Pz8_

tr '+/' '-_' maps characters one-to-one (+→-, /→_), and tr -d '=' deletes the padding. In the JWT lessons you'll write this as a reusable function.

JWTs use base64url. Encode the 6 bytes >>>??? with plain base64 (you'll get +, / and no padding), then print the base64url form: +→-, /→_, and any = removed.

$ bash solution.sh
Loading...

Output

Run your solution to see its output here.