mirror of
https://github.com/danbulant/jose
synced 2026-05-25 13:01:49 +00:00
When creating a Buffer from a string, this encoding will also correctly accept "URL and Filename Safe Alphabet".
53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
const { JOSEInvalidEncoding } = require('../errors')
|
|
|
|
const b64uRegExp = /^[a-zA-Z0-9_-]*$/
|
|
|
|
const fromBase64 = (base64) => {
|
|
return base64.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
|
|
}
|
|
|
|
const encode = (input, encoding = 'utf8') => {
|
|
return fromBase64(Buffer.from(input, encoding).toString('base64'))
|
|
}
|
|
|
|
const encodeBuffer = (buf) => {
|
|
return fromBase64(buf.toString('base64'))
|
|
}
|
|
|
|
const decodeToBuffer = (input) => {
|
|
if (!b64uRegExp.test(input)) {
|
|
throw new JOSEInvalidEncoding('input is not a valid base64url encoded string')
|
|
}
|
|
|
|
return Buffer.from(input, 'base64')
|
|
}
|
|
|
|
const decode = (input, encoding = 'utf8') => {
|
|
return decodeToBuffer(input).toString(encoding)
|
|
}
|
|
|
|
const b64uJSON = {
|
|
encode: (input) => {
|
|
return encode(JSON.stringify(input))
|
|
},
|
|
decode: (input, encoding = 'utf8') => {
|
|
return JSON.parse(decode(input, encoding))
|
|
}
|
|
}
|
|
|
|
b64uJSON.decode.try = (input, encoding = 'utf8') => {
|
|
try {
|
|
return b64uJSON.decode(input, encoding)
|
|
} catch (err) {
|
|
return decode(input, encoding)
|
|
}
|
|
}
|
|
|
|
const encodeBN = (bn) => encodeBuffer(bn.toBuffer())
|
|
|
|
module.exports.decode = decode
|
|
module.exports.decodeToBuffer = decodeToBuffer
|
|
module.exports.encode = encode
|
|
module.exports.encodeBuffer = encodeBuffer
|
|
module.exports.JSON = b64uJSON
|
|
module.exports.encodeBN = encodeBN
|