lib/otp/base32.rb in otp-0.0.8 vs lib/otp/base32.rb in otp-0.0.9
- old
+ new
@@ -4,15 +4,15 @@
A B C D E F G H I J K L M N O P
Q R S T U V W X Y Z 2 3 4 5 6 7
)
DECODE_MAP = {
- ?A=>0, ?B=>1, ?C=>2, ?D=>3, ?E=>4, ?F=>5, ?G=>6, ?H=>7,
- ?I=>8, ?J=>9, ?K=>10, ?L=>11, ?M=>12, ?N=>13, ?O=>14, ?P=>15,
- ?Q=>16, ?R=>17, ?S=>18, ?T=>19, ?U=>20, ?V=>21, ?W=>22, ?X=>23,
- ?Y=>24, ?Z=>25, ?2=>26, ?3=>27, ?4=>28, ?5=>29, ?6=>30, ?7=>31,
- ?==>-1,
+ "A"=>0, "B"=>1, "C"=>2, "D"=>3, "E"=>4, "F"=>5, "G"=>6, "H"=>7,
+ "I"=>8, "J"=>9, "K"=>10, "L"=>11, "M"=>12, "N"=>13, "O"=>14, "P"=>15,
+ "Q"=>16, "R"=>17, "S"=>18, "T"=>19, "U"=>20, "V"=>21, "W"=>22, "X"=>23,
+ "Y"=>24, "Z"=>25, "2"=>26, "3"=>27, "4"=>28, "5"=>29, "6"=>30, "7"=>31,
+ "="=>-1,
}
DECODE_LENGTH = {
1 => 1, # 5 bits -> 1 byte (irregular)
2 => 1, # 10 bits -> 1 byte
@@ -26,11 +26,11 @@
module_function
def encode(bytes, padding: true)
return nil unless bytes
- pad = padding ? ?= : ""
+ pad = padding ? "=" : ""
ret = ""
bytes = bytes.dup.force_encoding("binary")
off = 0
while off < bytes.length
n = 0
@@ -66,10 +66,9 @@
off = chars.length
break
else
n <<= 5
n |= d
- l = ((i+1) * 5.0 / 8.0).floor
l = DECODE_LENGTH[i+1]
end
end
ret << (1..l).map{|i| (n >> 40 - i * 8) & 0xff }.pack("c*")
end