Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 92 lines (83 sloc) 2.694 kb
8f21162 Richard Kilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues using...
richkilmer authored
1 #
4b368bd Watson stdlib: Deleted the whitespece of end of line, and fixed the petty typo.
Watson1978 authored
2 # = base64.rb: methods for base64-encoding and -decoding strings
8f21162 Richard Kilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues using...
richkilmer authored
3 #
4
5 # The Base64 module provides for the encoding (#encode64, #strict_encode64,
6 # #urlsafe_encode64) and decoding (#decode64, #strict_decode64,
7 # #urlsafe_decode64) of binary data using a Base64 representation.
8 #
9 # == Example
10 #
4b368bd Watson stdlib: Deleted the whitespece of end of line, and fixed the petty typo.
Watson1978 authored
11 # A simple encoding and decoding.
12 #
8f21162 Richard Kilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues using...
richkilmer authored
13 # require "base64"
14 #
15 # enc = Base64.encode64('Send reinforcements')
4b368bd Watson stdlib: Deleted the whitespece of end of line, and fixed the petty typo.
Watson1978 authored
16 # # -> "U2VuZCByZWluZm9yY2VtZW50cw==\n"
8f21162 Richard Kilmer bring lib up to r22701 (ruby 1.9.1_0 tag). there are build issues using...
richkilmer authored
17 # plain = Base64.decode64(enc)
18 # # -> "Send reinforcements"
19 #
20 # The purpose of using base64 to encode data is that it translates any
21 # binary data into purely printable characters.
22
23 module Base64
24 module_function
25
26 # Returns the Base64-encoded version of +bin+.
27 # This method complies with RFC 2045.
28 # Line feeds are added to every 60 encoded charactors.
29 #
30 # require 'base64'
31 # Base64.encode64("Now is the time for all good coders\nto learn Ruby")
32 #
33 # <i>Generates:</i>
34 #
35 # Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2RlcnMKdG8gbGVhcm4g
36 # UnVieQ==
37 def encode64(bin)
38 [bin].pack("m")
39 end
40
41 # Returns the Base64-decoded version of +str+.
42 # This method complies with RFC 2045.
43 # Characters outside the base alphabet are ignored.
44 #
45 # require 'base64'
46 # str = 'VGhpcyBpcyBsaW5lIG9uZQpUaGlzIG' +
47 # 'lzIGxpbmUgdHdvClRoaXMgaXMgbGlu' +
48 # 'ZSB0aHJlZQpBbmQgc28gb24uLi4K'
49 # puts Base64.decode64(str)
50 #
51 # <i>Generates:</i>
52 #
53 # This is line one
54 # This is line two
55 # This is line three
56 # And so on...
57 def decode64(str)
58 str.unpack("m").first
59 end
60
61 # Returns the Base64-encoded version of +bin+.
62 # This method complies with RFC 4648.
63 # No line feeds are added.
64 def strict_encode64(bin)
65 [bin].pack("m0")
66 end
67
68 # Returns the Base64-decoded version of +str+.
69 # This method complies with RFC 4648.
70 # ArgumentError is raised if +str+ is incorrectly padded or contains
71 # non-alphabet characters. Note that CR or LF are also rejected.
72 def strict_decode64(str)
73 str.unpack("m0").first
74 end
75
76 # Returns the Base64-encoded version of +bin+.
77 # This method complies with ``Base 64 Encoding with URL and Filename Safe
78 # Alphabet'' in RFC 4648.
79 # The alphabet uses '-' instead of '+' and '_' instead of '/'.
80 def urlsafe_encode64(bin)
81 strict_encode64(bin).tr("+/", "-_")
82 end
83
84 # Returns the Base64-decoded version of +str+.
85 # This method complies with ``Base 64 Encoding with URL and Filename Safe
86 # Alphabet'' in RFC 4648.
87 # The alphabet uses '-' instead of '+' and '_' instead of '/'.
88 def urlsafe_decode64(str)
89 strict_decode64(str.tr("-_", "+/"))
90 end
91 end
Something went wrong with that request. Please try again.