Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Base64#urlsafe_decode64
urlsafe_decode64(str)
lib/base64.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.3.0/lib/base64.rb, line 351
def urlsafe_decode64(str)
# NOTE: RFC 4648 does say nothing about unpadded input, but says that
# "the excess pad characters MAY also be ignored", so it is inferred that
# unpadded input is also acceptable.
if !str.end_with?("=") && str.length % 4 != 0
str = str.ljust((str.length + 3) & ~3, "=")
str.tr!("-_", "+/")
else
str = str.tr("-_", "+/")
end
strict_decode64(str)
end
Returns the decoding of an RFC-4648-compliant Base64-encoded string str:
str may not contain non-Base64 characters; see Encoding Character Set above:
Example
Base64.urlsafe_decode64('+') # Raises ArgumentError. Base64.urlsafe_decode64('/') # Raises ArgumentError. Base64.urlsafe_decode64("\n") # Raises ArgumentError.
Padding in str, if present, must be correct: see Padding, above:
Example
Base64.urlsafe_decode64("MDEyMzQ1Njc") # => "01234567" Base64.urlsafe_decode64("MDEyMzQ1Njc=") # => "01234567" Base64.urlsafe_decode64("MDEyMzQ1Njc==") # Raises ArgumentError.