base32h 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/base32h.rb +49 -19
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc91506f58aea74f8356374108a71bc367c03e9730bfb0fa29101472c0b506f3
|
4
|
+
data.tar.gz: a00a043947aeb4cd6a8f4abbb332f1110515da60d664ede1fd1bb972d4317f70
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c19d762b0ceaac4274fc54e77e7d01a5db96118f1f86f1a3f8e3ea0def3111cdef18e2ed5ffe6aa053250d2fb8463cce829eb01d869ee11999f8f07059fbae9
|
7
|
+
data.tar.gz: fd14eda07b8c3832b239aba0159c4af7f66cffcde005d150785da211fd5b60e4c5ebffb17cae859be522559946aa05c1369541cbd3d0b7f45c999573045c471f
|
data/lib/base32h.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
##
|
2
2
|
# This module provides Base32H encoding and decoding functions.
|
3
3
|
module Base32H
|
4
|
-
VERSION = "0.1.
|
4
|
+
VERSION = "0.1.1"
|
5
5
|
|
6
6
|
extend self
|
7
7
|
|
8
8
|
##
|
9
|
-
# The complete list of Base32H digits, as an array of strings
|
9
|
+
# The complete list of Base32H digits, as an array of strings. The
|
10
10
|
# index of the array is the numeric value of the digit(s) in the
|
11
11
|
# string at that index. The first character in each string is the
|
12
12
|
# "canonical" digit (i.e. the one that Base32H-conformant encoders
|
@@ -15,10 +15,10 @@ module Base32H
|
|
15
15
|
# Base32H-conformant decoders *must* accept and correctly decode to
|
16
16
|
# that value).
|
17
17
|
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
18
|
+
# @example
|
19
|
+
# Base32H.digits[27] #=> 'VvUu'
|
20
|
+
#
|
21
|
+
# @return [Array<String>] the full list of digits
|
22
22
|
def digits
|
23
23
|
['0Oo',
|
24
24
|
'1Ii',
|
@@ -54,11 +54,16 @@ module Base32H
|
|
54
54
|
'Zz']
|
55
55
|
end
|
56
56
|
|
57
|
+
# @!group Encoders
|
58
|
+
|
57
59
|
##
|
58
|
-
# :category: Encoding Digit
|
59
|
-
#
|
60
60
|
# Encodes an integer between 0 and 31 (inclusive) to its Base32H
|
61
|
-
# representation.
|
61
|
+
# representation. Returns +nil+ for input values outside that
|
62
|
+
# range.
|
63
|
+
#
|
64
|
+
# @param d [#to_i] the integer value of the requested digit
|
65
|
+
#
|
66
|
+
# @return [String, NilClass] the resulting digit
|
62
67
|
def encode_digit(d)
|
63
68
|
d = d.to_i
|
64
69
|
return nil unless (0..31).include? d
|
@@ -66,9 +71,11 @@ module Base32H
|
|
66
71
|
end
|
67
72
|
|
68
73
|
##
|
69
|
-
# :category: Encoding Numeric
|
70
|
-
#
|
71
74
|
# Encodes an integer to its Base32H representation.
|
75
|
+
#
|
76
|
+
# @param int [#to_i] the integer to encode
|
77
|
+
#
|
78
|
+
# @return [String] the encoded number
|
72
79
|
def encode(int)
|
73
80
|
rem = int.to_i.abs
|
74
81
|
out = []
|
@@ -81,9 +88,11 @@ module Base32H
|
|
81
88
|
end
|
82
89
|
|
83
90
|
##
|
84
|
-
# :category: Encoding Binary
|
85
|
-
#
|
86
91
|
# Encodes a (binary) string to its Base32H representation.
|
92
|
+
#
|
93
|
+
# @param bin [String] the string/binary to encode
|
94
|
+
#
|
95
|
+
# @return [String] the encoded binary
|
87
96
|
def encode_bin(bin)
|
88
97
|
data = bin.unpack('C*')
|
89
98
|
extra = data.length % 5
|
@@ -96,19 +105,28 @@ module Base32H
|
|
96
105
|
out.join ''
|
97
106
|
end
|
98
107
|
|
108
|
+
# @!endgroup
|
109
|
+
|
110
|
+
# @!group Decoders
|
111
|
+
|
99
112
|
##
|
100
|
-
# :category: Decoding Digit
|
101
|
-
#
|
102
113
|
# Decodes a single Base32H digit to its integer representation.
|
114
|
+
# Returns nil if the input is not a Base32H digit.
|
115
|
+
#
|
116
|
+
# @param d [#to_s] the digit to decode
|
117
|
+
#
|
118
|
+
# @return [Integer] the digit's value
|
103
119
|
def decode_digit(d)
|
104
120
|
d = d.to_s
|
105
121
|
digits.find_index {|i| i.include? d}
|
106
122
|
end
|
107
123
|
|
108
124
|
##
|
109
|
-
# :category: Decoding Numeric
|
110
|
-
#
|
111
125
|
# Decodes a Base32H number to its integer representation.
|
126
|
+
#
|
127
|
+
# @param str [#to_s] the number to decode
|
128
|
+
#
|
129
|
+
# @return [Integer] the decoded integer
|
112
130
|
def decode(str)
|
113
131
|
res = str.to_s.chars.reverse.reduce({acc: 0, exp: 0}) do |state, char|
|
114
132
|
digit = decode_digit(char)
|
@@ -123,9 +141,11 @@ module Base32H
|
|
123
141
|
end
|
124
142
|
|
125
143
|
##
|
126
|
-
# :category: Decoding Binary
|
127
|
-
#
|
128
144
|
# Decodes a Base32H binary into a string of packed unsigned bytes.
|
145
|
+
#
|
146
|
+
# @param str [String] the binary to decode
|
147
|
+
#
|
148
|
+
# @return [String] the decoded binary
|
129
149
|
def decode_bin(str)
|
130
150
|
data = str.chars.reject {|c| decode_digit(c).nil?}
|
131
151
|
extra = data.length % 8
|
@@ -138,6 +158,8 @@ module Base32H
|
|
138
158
|
out.pack('C*')
|
139
159
|
end
|
140
160
|
|
161
|
+
# @!endgroup
|
162
|
+
|
141
163
|
private
|
142
164
|
|
143
165
|
def u40_to_bytes(int)
|
@@ -161,12 +183,20 @@ module Base32H
|
|
161
183
|
end
|
162
184
|
|
163
185
|
class Integer
|
186
|
+
##
|
187
|
+
# Encodes the integer to its Base32H numeric representation.
|
188
|
+
#
|
189
|
+
# @return [String] the integer's Base32H representation
|
164
190
|
def to_base32h
|
165
191
|
Base32H.encode(self)
|
166
192
|
end
|
167
193
|
end
|
168
194
|
|
169
195
|
class String
|
196
|
+
##
|
197
|
+
# Encodes the string to its Base32H binary representation.
|
198
|
+
#
|
199
|
+
# @return [String] the string's Base32H representation
|
170
200
|
def to_base32h
|
171
201
|
Base32H.encode_bin(self)
|
172
202
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: base32h
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan S. Northrup ("RyNo")
|
@@ -39,6 +39,7 @@ licenses:
|
|
39
39
|
metadata:
|
40
40
|
homepage_uri: https://base32h.github.io
|
41
41
|
source_code_uri: https://github.com/base32h/base32h.rb
|
42
|
+
documentation_uri: https://rubydoc.info/gems/base32h/Base32H
|
42
43
|
post_install_message:
|
43
44
|
rdoc_options: []
|
44
45
|
require_paths:
|