module JwtNacl::Base64Url

Provide base64url encoding and decoding functions without padding, based upon standard base64 encoding and decoding functions that do use padding @see tools.ietf.org/html/rfc7515#appendix-C

Public Instance Methods

base64_padding_added(str) click to toggle source
# File lib/jwt_nacl/base64_url.rb, line 32
def base64_padding_added(str)
  mod = str.length % 4
  return str if mod == 0
  raise("Invalid base64 string") if mod == 1
  "#{str}#{"=" * (4 - mod)}"
end
base64_padding_removed(encoded) click to toggle source
# File lib/jwt_nacl/base64_url.rb, line 28
def base64_padding_removed(encoded)
  encoded.gsub(/[=]/, "")
end
decode(str) click to toggle source

@param str [String] encoded as url_encode64 @return [String] with trailing “=” padding added before decoding @example

Base64Url.decode("YmFy")
# => "bar"
# File lib/jwt_nacl/base64_url.rb, line 24
def decode(str)
  Base64.urlsafe_decode64(base64_padding_added(str))
end
encode(str) click to toggle source

@param str [String] @return [String] a urlsafe_encode64 string with all trailing “=” padding removed @example

Base64Url.encode("foo")
# => "Zm9v"
# File lib/jwt_nacl/base64_url.rb, line 15
def encode(str)
  base64_padding_removed(Base64.urlsafe_encode64(str))
end