forked from jwt/ruby-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64.rb
More file actions
33 lines (28 loc) · 835 Bytes
/
base64.rb
File metadata and controls
33 lines (28 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen_string_literal: true
require 'base64'
module JWT
# Base64 encoding and decoding
# @api private
class Base64
class << self
# Encode a string with URL-safe Base64 complying with RFC 4648 (not padded).
# @api private
def url_encode(str)
::Base64.urlsafe_encode64(str, padding: false)
end
# Encode a string with Base64 complying with RFC 4648 (padded).
# @api private
def strict_encode(str)
::Base64.strict_encode64(str)
end
# Decode a string with URL-safe Base64 complying with RFC 4648.
# @api private
def url_decode(str)
::Base64.urlsafe_decode64(str)
rescue ArgumentError => e
raise unless e.message == 'invalid base64'
raise Base64DecodeError, 'Invalid base64 encoding'
end
end
end
end