aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-02-13 12:31:30 +0000
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-02-13 12:31:30 +0000
commit6b6680945ed3274cddbc34fdfd410d74081a3e94 (patch)
treea6fbc45f4dc61849dd61e5c62d3c358bd7ad85ab
parentb4974e71dcb32d430d7d686c5de247218991ec6c (diff)
downloadruby-6b6680945ed3274cddbc34fdfd410d74081a3e94.tar.gz
* lib/base64.rb: make urlsafe mode user-friendly.
* lib/base64.rb (Base64.urlsafe_encode64): a new option "padding" to suppress the padding character ("="). * lib/base64.rb (Base64.urlsafe_decode64): now it accepts not only correctly-padded input but also unpadded input. [Feature #10740][ruby-core:67570] * test/base64/test_base64.rb: Test for above git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49585 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog13
-rw-r--r--NEWS6
-rw-r--r--lib/base64.rb21
-rw-r--r--test/base64/test_base64.rb14
4 files changed, 51 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index e93cc122c2..d14807b474 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+Fri Feb 13 21:16:00 2015 Yusuke Endoh <mame@tsg.ne.jp>
+
+ * lib/base64.rb: make urlsafe mode user-friendly.
+
+ * lib/base64.rb (Base64.urlsafe_encode64): a new option "padding" to
+ suppress the padding character ("=").
+
+ * lib/base64.rb (Base64.urlsafe_decode64): now it accepts not only
+ correctly-padded input but also unpadded input.
+ [Feature #10740][ruby-core:67570]
+
+ * test/base64/test_base64.rb: Test for above
+
Fri Feb 13 14:19:06 2015 SHIBATA Hiroshi <shibata.hiroshi@gmail.com>
* ext/json: merge upstream from flori/json
diff --git a/NEWS b/NEWS
index 13185d19c0..7c472298f9 100644
--- a/NEWS
+++ b/NEWS
@@ -38,6 +38,12 @@ with all sufficient information, see the ChangeLog file.
* lib/drb/drb.rb
* removed unused argument. https://github.com/ruby/ruby/pull/515
+* lib/base64.rb
+ * Base64.urlsafe_encode64: added a "padding" option to suppress
+ the padding character ("=").
+ * Base64.urlsafe_decode64: now it accepts not only correctly-padded
+ input but also unpadded input.
+
=== Built-in global variables compatibility issues
=== C API updates
diff --git a/lib/base64.rb b/lib/base64.rb
index 98829f0d96..30304cd188 100644
--- a/lib/base64.rb
+++ b/lib/base64.rb
@@ -77,15 +77,30 @@ module Base64
# This method complies with ``Base 64 Encoding with URL and Filename Safe
# Alphabet'' in RFC 4648.
# The alphabet uses '-' instead of '+' and '_' instead of '/'.
- def urlsafe_encode64(bin)
- strict_encode64(bin).tr("+/", "-_")
+ # Note that the result can still contain '='.
+ # You can remove the padding by setting "padding" as false.
+ def urlsafe_encode64(bin, padding: true)
+ str = strict_encode64(bin).tr("+/", "-_")
+ str = str.delete("=") unless padding
+ str
end
# Returns the Base64-decoded version of +str+.
# This method complies with ``Base 64 Encoding with URL and Filename Safe
# Alphabet'' in RFC 4648.
# The alphabet uses '-' instead of '+' and '_' instead of '/'.
+ #
+ # The padding character is optional.
+ # This method accepts both correctly-padded and unpadded input.
+ # Note that it still rejects incorrectly-padded input.
def urlsafe_decode64(str)
- strict_decode64(str.tr("-_", "+/"))
+ # 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.
+ str = str.tr("-_", "+/")
+ if !str.end_with?("=") && str.length % 4 != 0
+ str = str.ljust((str.length + 3) & ~3, "=")
+ end
+ strict_decode64(str)
end
end
diff --git a/test/base64/test_base64.rb b/test/base64/test_base64.rb
index c5e61b31ac..c2cd60adcd 100644
--- a/test/base64/test_base64.rb
+++ b/test/base64/test_base64.rb
@@ -87,6 +87,13 @@ class TestBase64 < Test::Unit::TestCase
assert_equal("_-8=", Base64.urlsafe_encode64("\xff\xef"))
end
+ def test_urlsafe_encode64_unpadded
+ assert_equal("", Base64.urlsafe_encode64("", padding: false))
+ assert_equal("AA", Base64.urlsafe_encode64("\0", padding: false))
+ assert_equal("AAA", Base64.urlsafe_encode64("\0\0", padding: false))
+ assert_equal("AAAA", Base64.urlsafe_encode64("\0\0\0", padding: false))
+ end
+
def test_urlsafe_decode64
assert_equal("", Base64.urlsafe_decode64(""))
assert_equal("\0", Base64.urlsafe_decode64("AA=="))
@@ -97,4 +104,11 @@ class TestBase64 < Test::Unit::TestCase
assert_equal("\377\377\377", Base64.urlsafe_decode64("____"))
assert_equal("\xff\xef", Base64.urlsafe_decode64("_+8="))
end
+
+ def test_urlsafe_decode64_unpadded
+ assert_equal("\0", Base64.urlsafe_decode64("AA"))
+ assert_equal("\0\0", Base64.urlsafe_decode64("AAA"))
+ assert_equal("\0\0\0", Base64.urlsafe_decode64("AAAA"))
+ assert_raise(ArgumentError) { Base64.urlsafe_decode64("AA=") }
+ end
end