Created
March 14, 2014 19:37
-
-
Save ssut/9555137 to your computer and use it in GitHub Desktop.
Base64 impl written Ruby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def map | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" | |
end | |
def encode64 | |
self.bytes.to_a.map {|a|a.ord.to_s(2).rjust(8, '0')}.join.scan(/.{6}/).map {|a|map[a.to_i(2)]}.join | |
end | |
def decode64 | |
self.split('').map {|a|map.index(a)}.map {|a|a.to_s(2).rjust(6,'0')}.join.scan(/.{8}/).map{|a|a.to_i(2).chr}.join | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment