base64Url
This performs base64 URL encoding often used with JWT
Created by: hackvertor
Installed 1 times
Category: Convert
Created on: Thursday, October 10, 2024 at 11:56:59 AM
Updated on: Thursday, October 10, 2024 at 11:56:59 AM
Tag arguments
[]
Code
class base64Url {
encode(input) {
return btoa(input)
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=+$/, "");
}
decode(input) {
input = input.replace(/-/g, "+").replace(/_/g, "/");
const pad = input.length % 4 === 0 ? 0 : 4 - (input.length % 4);
input += "=".repeat(pad);
return atob(input);
}
matches(input) {
const base64urlRegex = /^[A-Za-z0-9_-]+$/;
const result = base64urlRegex.exec(input);
return result;
}
}