Module: RubyBase64
- Defined in:
- lib/ruby_base64.rb,
lib/ruby_base64/version.rb
Constant Summary collapse
- CharBase64 =
码表
{ 0 => "A", 17 => "R", 34 => "i", 51 => "z", 1 => "B", 18 => "S", 35 => "j", 52 => "0", 2 => "C", 19 => "T", 36 => "k", 53 => "1", 3 => "D", 20 => "U", 37 => "l", 54 => "2", 4 => "E", 21 => "V", 38 => "m", 55 => "3", 5 => "F", 22 => "W", 39 => "n", 56 => "4", 6 => "G", 23 => "X", 40 => "o", 57 => "5", 7 => "H", 24 => "Y", 41 => "p", 58 => "6", 8 => "I", 25 => "Z", 42 => "q", 59 => "7", 9 => "J", 26 => "a", 43 => "r", 60 => "8", 10 => "K", 27 => "b", 44 => "s", 61 => "9", 11 => "L", 28 => "c", 45 => "t", 62 => "+", 12 => "M", 29 => "d", 46 => "u", 63 => "/", 13 => "N", 30 => "e", 47 => "v", 64 => "=", 14 => "O", 31 => "f", 48 => "w", 15 => "P", 32 => "g", 49 => "x", 16 => "Q", 33 => "h", 50 => "y" }
- VERSION =
"0.0.1"
Class Method Summary collapse
Class Method Details
.decode64(str) ⇒ Object
解密
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ruby_base64.rb', line 72 def decode64(str) stra = str.split(//) t = 0 stra.each{|n| t += 1 if n == CharBase64[64] } t.times{stra.pop} strb = [] strc = "" dbase64 = CharBase64.invert stra.each {|n|strb << dbase64[n]} strb.collect! do |n| n.to_s(2).length != 6 ? "0"*(6 - n.to_s(2).length) + n.to_s(2) : n.to_s(2) end if t > 0 tt = strb[strb.size-1] t*2.times{tt.slice!(tt.size-1,1)} strb[strb.size-1] = tt end strb.each{|n|strc+=n} return [strc].pack("B*").delete("\000") end |
.encode64(str) ⇒ Object
加密
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ruby_base64.rb', line 35 def encode64(str) raise "参数不是一个字符串" unless str.kind_of?(String) tstr = str.unpack("B*") size = tstr[0].size/6 ysize = tstr[0].size%6 tstr2 = [] for i in 0...size tstr2 << tstr[0][i*6,6] end # 割断码 if tstr[0].size%6 != 0 tstr2 << tstr[0][size*6,ysize] + ("0"*(6-ysize)) ym = (6- ysize)/2 end # 补足码 tstr2.collect!{|n|CharBase64[eval("0b"+"00"+n)]} # 特殊码 if ysize != 0 case ym when 1 tstr2 << CharBase64[64] when 2 tstr2 << CharBase64[64] tstr2 << CharBase64[64] end end return tstr2.join end |