module ZBase32
Constants
- CHARSET
- MASKS
- MASKS2
- VERSION
- ZB2N
Public Instance Methods
decode(bytes)
click to toggle source
# File lib/zbase32.rb, line 18 def decode(bytes) join_string(bytes.downcase.split('').map { |s| ZB2N[s] }) end
encode(bytes)
click to toggle source
# File lib/zbase32.rb, line 9 def encode(bytes) ret = "" (split_string(bytes)).each do |part| raise "There is no #{part}" unless part < 32 ret = ret + CHARSET[part] end ret end
Private Instance Methods
join_string(output)
click to toggle source
# File lib/zbase32.rb, line 50 def join_string output length = 5 * output.size ret = Array.new((length / 8.0).ceil, 0) part = 0 ; chunk = 0 suboffset = 0 ; offset = 0 n = 0 ; q = 0 while (q < length) do offset = (q / 8).to_i suboffset = q % 8 part = output[n] chunk = (part << suboffset ) & MASKS[suboffset] ret[offset] |= chunk suboffset = suboffset - 4 if suboffset >= 0 ret[offset+1] |= (part >> (4-suboffset) ) & MASKS2[suboffset] end n = n + 1 q = q + 5 end string = ret.map(&:chr).join('') string.chomp!("\0") string end
split_string(string)
click to toggle source
# File lib/zbase32.rb, line 24 def split_string string output = [] ; chunk = 0 part = 0 ; offset = 0 suboffset = 0 ; q = 0 length = 8 * string.length while (q < length) do offset = (q / 8).to_i suboffset = q % 8 part = string[offset, 1][0].ord chunk = (part & MASKS[suboffset]) >> suboffset suboffset = suboffset - 4 if suboffset >= 0 if ((q + 5) > length) part = 0 else part = string[offset+1, 1][0].ord end chunk |= (part & MASKS2[suboffset]) << (4 - suboffset) end output.push chunk q = q + 5 end output end