diff --git a/lib/base64.rb b/lib/base64.rb index 5c8df841f1..6b049982cf 100644 --- a/lib/base64.rb +++ b/lib/base64.rb @@ -82,8 +82,14 @@ module Base64 # You can remove the padding by setting +padding+ as false. def urlsafe_encode64(bin, padding: true) str = strict_encode64(bin) + unless padding + if str.end_with?("==") + str.delete_suffix!("==") + elsif str.end_with?("=") + str.chop! + end + end str.tr!("+/", "-_") - str.delete!("=") unless padding str end