base32-alphabets 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/{HISTORY.md → CHANGELOG.md} +0 -0
- data/Manifest.txt +4 -2
- data/README.md +13 -0
- data/Rakefile +1 -1
- data/lib/base32-alphabets.rb +14 -2
- data/lib/base32-alphabets/base.rb +78 -35
- data/lib/base32-alphabets/base32.rb +48 -7
- data/lib/base32-alphabets/crockford.rb +10 -6
- data/lib/base32-alphabets/electrologica.rb +43 -40
- data/lib/base32-alphabets/kai.rb +12 -42
- data/lib/base32-alphabets/version.rb +1 -1
- data/test/test_base32_crockford.rb +4 -9
- data/test/test_base32_electrologica.rb +53 -0
- data/test/{test_base32.rb → test_base32_kai.rb} +18 -9
- data/test/test_bytes.rb +160 -0
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 94a33c87036d36e7280aae81da210d4155b20b54
|
4
|
+
data.tar.gz: 6b1c22db5cb74881d9948cc23269c67678f9946b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c98cf85b86052006d7928c5c271f03d30a24e55f90eabd73302d712536088ad39101e6b4b9352ac09be447ee1bebdae91d520b8237b727c4799a1d5507eb16f
|
7
|
+
data.tar.gz: 697155fa0c6bf2c12b04021959e224970f12200ef3baaa545d199126ff028e536a7a0c9b2e771532ec328ffbb92fd317ef8c3c6ec86bc30830096e3f484a7f87
|
data/{HISTORY.md → CHANGELOG.md}
RENAMED
File without changes
|
data/Manifest.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
CHANGELOG.md
|
2
2
|
LICENSE.md
|
3
3
|
Manifest.txt
|
4
4
|
README.md
|
@@ -11,5 +11,7 @@ lib/base32-alphabets/electrologica.rb
|
|
11
11
|
lib/base32-alphabets/kai.rb
|
12
12
|
lib/base32-alphabets/version.rb
|
13
13
|
test/helper.rb
|
14
|
-
test/test_base32.rb
|
15
14
|
test/test_base32_crockford.rb
|
15
|
+
test/test_base32_electrologica.rb
|
16
|
+
test/test_base32_kai.rb
|
17
|
+
test/test_bytes.rb
|
data/README.md
CHANGED
@@ -66,6 +66,8 @@ pp str == str2
|
|
66
66
|
# => true
|
67
67
|
pp Kai.fmt( str2 )
|
68
68
|
# => "aaaa 7885 22f2 agff 1661 7755 e979 2441 6667 7664 a9aa cfff"
|
69
|
+
pp Kai.fmt( hex ) # all-in-one "shortcut" for Kai.fmt( Kai.encode( hex ))
|
70
|
+
# => "aaaa 7885 22f2 agff 1661 7755 e979 2441 6667 7664 a9aa cfff"
|
69
71
|
|
70
72
|
hex2 = Kai.decode( str2 ) ## text to (binary) number
|
71
73
|
pp hex
|
@@ -74,6 +76,11 @@ pp hex2
|
|
74
76
|
# => 512955438081049600613224346938352058409509756310147795204209859701881294
|
75
77
|
pp hex == hex2
|
76
78
|
# => true
|
79
|
+
|
80
|
+
pp = Kai.bytes( hex )
|
81
|
+
# => [9,9,9,9,6,7,7,4,1,1,14,1,9,15,14,14,0,5,5,0,6,6,4,4,13,8,6,8,1,3,3,0,5,5,5,6,6,5,5,3,9,8,9,9,11,14,14,14]
|
82
|
+
pp = Kai.bytes( str ) # or from a kai string (auto-decodes to hex first)
|
83
|
+
# => [9,9,9,9,6,7,7,4,1,1,14,1,9,15,14,14,0,5,5,0,6,6,4,4,13,8,6,8,1,3,3,0,5,5,5,6,6,5,5,3,9,8,9,9,11,14,14,14]
|
77
84
|
```
|
78
85
|
|
79
86
|
or
|
@@ -87,8 +94,14 @@ pp str
|
|
87
94
|
# => "aaaa788522f2agff16617755e979244166677664a9aacfff"
|
88
95
|
pp Base32.fmt( str )
|
89
96
|
# => "aaaa 7885 22f2 agff 1661 7755 e979 2441 6667 7664 a9aa cfff"
|
97
|
+
pp Base32.fmt( hex ) # all-in-one "shortcut" for Base32.fmt( Base32.encode( hex ))
|
98
|
+
# => "aaaa 7885 22f2 agff 1661 7755 e979 2441 6667 7664 a9aa cfff"
|
90
99
|
pp = Base32.decode( str ) ## text to (binary) number
|
91
100
|
# => 512955438081049600613224346938352058409509756310147795204209859701881294
|
101
|
+
pp = Base32.bytes( hex )
|
102
|
+
# => [9,9,9,9,6,7,7,4,1,1,14,1,9,15,14,14,0,5,5,0,6,6,4,4,13,8,6,8,1,3,3,0,5,5,5,6,6,5,5,3,9,8,9,9,11,14,14,14]
|
103
|
+
pp = Base32.bytes( str ) # or from a kai string (auto-decodes to hex first)
|
104
|
+
# => [9,9,9,9,6,7,7,4,1,1,14,1,9,15,14,14,0,5,5,0,6,6,4,4,13,8,6,8,1,3,3,0,5,5,5,6,6,5,5,3,9,8,9,9,11,14,14,14]
|
92
105
|
```
|
93
106
|
|
94
107
|
|
data/Rakefile
CHANGED
data/lib/base32-alphabets.rb
CHANGED
@@ -22,8 +22,20 @@ Crockford = Base32::Crockford
|
|
22
22
|
Electrologica = Base32::Electrologica
|
23
23
|
|
24
24
|
|
25
|
-
def encode32(
|
26
|
-
|
25
|
+
def encode32( num_or_bytes )
|
26
|
+
Base32.encode( num_or_bytes )
|
27
|
+
end
|
28
|
+
|
29
|
+
def decode32( str )
|
30
|
+
Base32.decode( str )
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
## -- add bytes32 - why? why not?
|
35
|
+
##
|
36
|
+
## def bytes32( num_or_str )
|
37
|
+
## Base32.bytes( num_or_str )
|
38
|
+
## end
|
27
39
|
|
28
40
|
|
29
41
|
|
@@ -7,57 +7,100 @@
|
|
7
7
|
module Base32
|
8
8
|
class Base
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
## puts " mod=#{mod} == #{klass::ALPHABET[mod]}"
|
17
|
-
buf = klass::ALPHABET[mod] + buf
|
18
|
-
## puts "buf=#{buf}"
|
19
|
-
num = (num - mod)/BASE
|
10
|
+
def self.bytes( num_or_str )
|
11
|
+
if num_or_str.is_a? String
|
12
|
+
str = num_or_str
|
13
|
+
num = decode( str )
|
14
|
+
else # assume number
|
15
|
+
num = num_or_str
|
20
16
|
end
|
21
|
-
|
17
|
+
Base32._bytes( num )
|
22
18
|
end
|
23
19
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
num = 0
|
32
|
-
str.reverse.each_char.with_index do |char,index|
|
33
|
-
code = klass::NUMBER[char]
|
34
|
-
raise ArgumentError, "Value passed not a valid base32 string - >#{char}< not found in alphabet" if code.nil?
|
35
|
-
num += code * (BASE**(index))
|
20
|
+
# Converts a base10 integer to a base32 string.
|
21
|
+
def self.encode( num_or_bytes )
|
22
|
+
if num_or_bytes.is_a? Array
|
23
|
+
bytes = num_or_bytes
|
24
|
+
else
|
25
|
+
num = num_or_bytes
|
26
|
+
bytes = Base32._bytes( num )
|
36
27
|
end
|
37
|
-
|
28
|
+
_encode( bytes )
|
38
29
|
end
|
39
30
|
|
31
|
+
def self._encode( bytes )
|
32
|
+
bytes.reduce( String.new ) do |buf, byte|
|
33
|
+
buf << alphabet[byte]
|
34
|
+
buf
|
35
|
+
end
|
36
|
+
end
|
40
37
|
|
38
|
+
def self.fmt( str_or_num_or_bytes, group: 4, sep: ' ' )
|
39
|
+
if str_or_num_or_bytes.is_a? String
|
40
|
+
str = str_or_num_or_bytes
|
41
|
+
else ## assume number
|
42
|
+
## str_or_num_or_bytes.is_a?(Integer) ||
|
43
|
+
## str_or_num_or_bytes.is_a?(Bignum) ||
|
44
|
+
## str_or_num_or_bytes.is_a?(Fixnum)
|
45
|
+
num_or_bytes = str_or_num_or_bytes
|
46
|
+
str = encode( num_or_bytes ) ## auto-encode (shortcut)
|
47
|
+
end
|
48
|
+
_fmt( str, group: group, sep: sep )
|
49
|
+
end
|
41
50
|
|
42
|
-
|
43
|
-
|
44
|
-
## note: allow spaces or dashes (-); remove them all first
|
45
|
-
str = str.tr( ' -', '' )
|
51
|
+
def self._fmt( str, group: 4, sep: ' ' )
|
52
|
+
str = _clean( str )
|
46
53
|
|
47
54
|
## format in groups of four (4) separated by space
|
48
55
|
## e.g. ccac7787fa7fafaa16467755f9ee444467667366cccceede
|
49
56
|
## : ccac 7787 fa7f afaa 1646 7755 f9ee 4444 6766 7366 cccc eede
|
50
|
-
str.reverse.gsub( /(.{
|
57
|
+
str.reverse.gsub( /(.{#{group}})/, "\\1#{sep}" ).reverse.sub( /^#{sep}/, '' )
|
51
58
|
end
|
52
59
|
|
53
60
|
|
61
|
+
# Converts a base32 string to a base10 integer.
|
62
|
+
def self.decode( str_or_bytes )
|
63
|
+
if str_or_bytes.is_a? Array
|
64
|
+
bytes = str_or_bytes
|
65
|
+
else ## assume string
|
66
|
+
str = str_or_bytes
|
67
|
+
bytes = _decode( str )
|
68
|
+
end
|
69
|
+
Base32._pack( bytes )
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
def self._decode( str )
|
74
|
+
str = _clean( str )
|
75
|
+
str.each_char.reduce([]) do |bytes,char|
|
76
|
+
byte = number[char]
|
77
|
+
raise ArgumentError, "Value passed not a valid base32 string - >#{char}< not found in alphabet" if byte.nil?
|
78
|
+
bytes << byte
|
79
|
+
bytes
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def self._clean( str )
|
84
|
+
## note: remove space ( ), dash (-), slash (/) for now as "allowed / supported" separators
|
85
|
+
str.tr( ' -/', '' )
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
########################
|
91
|
+
## (private) helpers
|
92
|
+
def self._build_binary
|
93
|
+
## e.g. '00000', '00001', '00010', '00011', etc.
|
94
|
+
number.each.reduce({}) do |h, (char,index)|
|
95
|
+
h[char] = '%05b' % index
|
96
|
+
h
|
97
|
+
end
|
98
|
+
end
|
54
99
|
|
55
|
-
|
56
|
-
##
|
57
|
-
|
58
|
-
|
59
|
-
# puts "#{char} => #{index} #{'%05b' % index}"
|
60
|
-
h[char] = '%05b' % index
|
100
|
+
def self._build_code
|
101
|
+
## e.g. '00', '01', '02', '03', '04', etc.
|
102
|
+
number.each.reduce({}) do |h, (char,index)|
|
103
|
+
h[char] = '%02d' % index
|
61
104
|
h
|
62
105
|
end
|
63
106
|
end
|
@@ -49,13 +49,54 @@ module Base32 ## Base32 (2^5 - 5-bits)
|
|
49
49
|
|
50
50
|
|
51
51
|
|
52
|
-
def self.encode(
|
53
|
-
|
54
|
-
|
52
|
+
def self.encode( num_or_bytes, klass: configuration.format )
|
53
|
+
klass.encode( num_or_bytes )
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.decode( str_or_bytes, klass: configuration.format )
|
57
|
+
klass.decode( str_or_bytes )
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.fmt( str_or_num_or_bytes, klass: configuration.format, group: 4, sep: ' ' )
|
61
|
+
klass.fmt( str_or_num_or_bytes, group: group, sep: sep )
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.bytes( num_or_str, klass: configuration.format )
|
65
|
+
klass.bytes( num_or_str )
|
66
|
+
end
|
55
67
|
|
56
|
-
## encoding alphabet - letter-to-number by index / array
|
57
|
-
def self.alphabet( klass: configuration.format ) klass::ALPHABET; end
|
58
|
-
## decoding number-to-letter mapping / hash
|
59
|
-
def self.number( klass: configuration.format ) klass::NUMBER; end
|
60
68
|
|
69
|
+
####
|
70
|
+
# (private) helper - note: leading underscore in name e.g. _bytes
|
71
|
+
def self._bytes( num )
|
72
|
+
b = []
|
73
|
+
while num >= BASE
|
74
|
+
mod = num % BASE
|
75
|
+
b << mod
|
76
|
+
num = (num - mod) / BASE
|
77
|
+
end
|
78
|
+
b << num
|
79
|
+
b = b.reverse
|
80
|
+
b
|
81
|
+
end
|
82
|
+
|
83
|
+
def self._pack( bytes )
|
84
|
+
num = 0
|
85
|
+
bytes.reverse.each_with_index do |byte,index|
|
86
|
+
num += byte * (BASE**(index))
|
87
|
+
end
|
88
|
+
num
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
## encoding alphabet - letter-to-number by index / array
|
94
|
+
def self.alphabet( klass: configuration.format ) klass.alphabet; end
|
95
|
+
|
96
|
+
## decoding letter-to-number mapping / hash
|
97
|
+
def self.number( klass: configuration.format ) klass.number; end
|
98
|
+
## decoding letter-to-code mapping / hash
|
99
|
+
def self.code( klass: configuration.format ) klass.code; end
|
100
|
+
## decoding letter-to-binary mapping / hash
|
101
|
+
def self.binary( klass: configuration.format ) klass.binary; end
|
61
102
|
end # module Base32
|
@@ -11,12 +11,13 @@ class Crockford < Base
|
|
11
11
|
ALPHABET = %w[ 0 1 2 3 4 5 6 7
|
12
12
|
8 9 a b c d e f
|
13
13
|
g h j k m n p q
|
14
|
-
r s t v w x y z]
|
14
|
+
r s t v w x y z ]
|
15
15
|
|
16
|
-
def self.
|
16
|
+
def self.alphabet() ALPHABET; end
|
17
17
|
|
18
18
|
|
19
|
-
|
19
|
+
|
20
|
+
NUMBER = {
|
20
21
|
'0' => 0, 'o' => 0, 'O'=> 0,
|
21
22
|
'1' => 1, 'l' => 1, 'L'=> 1, 'i'=> 1, 'I' => 1,
|
22
23
|
'2' => 2,
|
@@ -50,16 +51,19 @@ NUMBER = { ## rename INTEGER /INT - why? why not??
|
|
50
51
|
'y' => 30, 'Y' => 30,
|
51
52
|
'z' => 31, 'Z' => 31
|
52
53
|
}
|
53
|
-
|
54
|
-
def self.decode( str ) super( str, klass: self ); end
|
54
|
+
def self.number() NUMBER; end
|
55
55
|
|
56
56
|
|
57
57
|
## simple hash map (helper) for conversion to binary string
|
58
|
-
BINARY =
|
58
|
+
BINARY = _build_binary()
|
59
|
+
CODE = _build_code()
|
59
60
|
|
60
61
|
## add shortcuts (convenience) aliases
|
61
62
|
BIN = BINARY
|
62
63
|
NUM = NUMBER
|
63
64
|
|
65
|
+
def self.code() CODE; end
|
66
|
+
def self.binary() BINARY; end
|
67
|
+
|
64
68
|
end # class Crockford
|
65
69
|
end # module Base32
|
@@ -1,33 +1,17 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
module Base32
|
4
|
-
class Electrologica
|
4
|
+
class Electrologica < Base ## Base32 (2^5 - 5-bits)
|
5
5
|
|
6
6
|
ALPHABET = %w[ 00 01 02 03 04 05 06 07
|
7
7
|
08 09 10 11 12 13 14 15
|
8
8
|
16 17 18 19 20 21 22 23
|
9
9
|
24 25 26 27 28 29 30 31 ]
|
10
10
|
|
11
|
-
|
12
|
-
def self.encode( num )
|
13
|
-
buf = String.new
|
14
|
-
while num >= BASE
|
15
|
-
# puts "num=#{num}"
|
16
|
-
mod = num % BASE
|
17
|
-
# puts " mod=#{mod} == #{ALPHABET[mod]}"
|
18
|
-
buf = "-" + ALPHABET[mod] + buf ## note: add - separator
|
19
|
-
# puts "buf=#{buf}"
|
20
|
-
num = (num - mod)/BASE
|
21
|
-
end
|
22
|
-
ALPHABET[num] + buf
|
23
|
-
end
|
11
|
+
def self.alphabet() ALPHABET; end ## add alpha / char aliases - why? why not?
|
24
12
|
|
25
|
-
## Note:
|
26
|
-
## for decoding allow (misspelled) l/L for 1
|
27
|
-
## and (misspelled) 0 for o/O - why? why not?
|
28
|
-
## and UPPERCASE letters - why? why not?
|
29
13
|
|
30
|
-
NUMBER = {
|
14
|
+
NUMBER = {
|
31
15
|
'00' => 0, '0' => 0,
|
32
16
|
'01' => 1, '1' => 1,
|
33
17
|
'02' => 2, '2' => 2,
|
@@ -62,40 +46,59 @@ class Electrologica ## Base32 (2^5 - 5-bits)
|
|
62
46
|
'31' => 31,
|
63
47
|
}
|
64
48
|
|
49
|
+
def self.number() NUMBER; end
|
50
|
+
|
51
|
+
BINARY = _build_binary()
|
52
|
+
CODE = _build_code()
|
65
53
|
|
66
54
|
## add shortcuts (convenience) aliases
|
67
|
-
|
55
|
+
BIN = BINARY
|
68
56
|
NUM = NUMBER
|
69
57
|
|
70
|
-
|
71
|
-
def self.
|
72
|
-
|
73
|
-
|
74
|
-
str = str.tr( ' /', '-' )
|
75
|
-
str = str.gsub( /-{2,}/, '-' ) ## fold more than one dash into one
|
58
|
+
def self.code() CODE; end
|
59
|
+
def self.binary() BINARY; end
|
60
|
+
|
61
|
+
|
76
62
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
63
|
+
# Converts a base10 integer to a base32 string.
|
64
|
+
def self._encode( bytes )
|
65
|
+
bytes.each_with_index.reduce(String.new) do |buf, (byte,i)|
|
66
|
+
buf << "-" if i > 0 ## add separator (-) EXCEPT for first char
|
67
|
+
buf << alphabet[byte]
|
68
|
+
buf
|
82
69
|
end
|
83
|
-
num
|
84
70
|
end
|
85
71
|
|
72
|
+
def self._fmt( str, group: 4, sep: ' ' )
|
73
|
+
## todo/fix: check sep - MUST be space () or slash (/) for now!!!!!
|
86
74
|
|
87
|
-
|
88
|
-
## note: allow spaces or slash (/) for dashes (-)
|
89
|
-
str = str.strip ## remove leading and trailing spaces (first)
|
90
|
-
str = str.tr( ' /', '-' )
|
91
|
-
str = str.gsub( /-{2,}/, '-' ) ## fold more than one dash into one
|
75
|
+
str = _clean( str )
|
92
76
|
|
93
77
|
## format in groups of four (4) separated by space
|
94
|
-
## e.g.
|
95
|
-
## :
|
78
|
+
## e.g. 09-09-09-09-06-07-07-04-01-01-14-01-09-15-14-14-00-05-05-00
|
79
|
+
## : 09-09-09-09 06-07-07-04 01-01-14-01 09-15-14-14 00-05-05-00
|
96
80
|
|
97
81
|
## note: use reverse - if not divided by four that leading slice gets cut short
|
98
|
-
str.split('-').reverse.each_slice(
|
82
|
+
str.split('-').reverse.each_slice( group ).map { |slice| slice.reverse.join( '-' ) }.reverse.join( sep )
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
# Converts a base32 string to a base10 integer.
|
87
|
+
def self._decode( str )
|
88
|
+
str = _clean( str )
|
89
|
+
str.split('-').reduce([]) do |bytes,char|
|
90
|
+
byte = number[char]
|
91
|
+
raise ArgumentError, "Value passed not a valid base32 string - >#{char}< not found in alphabet" if byte.nil?
|
92
|
+
bytes << byte
|
93
|
+
bytes
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def self._clean( str )
|
98
|
+
## note: allow spaces or slash (/) for dashes (-)
|
99
|
+
str = str.strip ## remove leading and trailing spaces (first)
|
100
|
+
str = str.tr( ' /', '-' )
|
101
|
+
str = str.gsub( /-{2,}/, '-' ) ## fold more than one dash into one
|
99
102
|
end
|
100
103
|
|
101
104
|
end # class Electrologica
|
data/lib/base32-alphabets/kai.rb
CHANGED
@@ -8,21 +8,24 @@
|
|
8
8
|
|
9
9
|
module Base32
|
10
10
|
class Kai < Base
|
11
|
+
|
11
12
|
# See https://en.wikipedia.org/wiki/Base58
|
12
13
|
## Note: aplpabet used for encoding
|
13
14
|
ALPHABET = %w[ 1 2 3 4 5 6 7 8
|
14
15
|
9 a b c d e f g
|
15
16
|
h i j k m n o p
|
16
|
-
q r s t u v w x]
|
17
|
+
q r s t u v w x ]
|
18
|
+
|
19
|
+
def self.alphabet() ALPHABET; end ## add alpha / char aliases - why? why not?
|
20
|
+
|
17
21
|
|
18
|
-
def self.encode( num ) super( num, klass: self ); end
|
19
22
|
|
20
23
|
## Note:
|
21
24
|
## for decoding allow (misspelled) l/L for 1
|
22
25
|
## and (misspelled) 0 for o/O - why? why not?
|
23
26
|
## and UPPERCASE letters - why? why not?
|
24
27
|
|
25
|
-
NUMBER = {
|
28
|
+
NUMBER = {
|
26
29
|
'1' => 0, 'l' => 0, 'L'=> 0,
|
27
30
|
'2' => 1,
|
28
31
|
'3' => 2,
|
@@ -57,52 +60,19 @@ NUMBER = { ## rename INTEGER /INT - why? why not??
|
|
57
60
|
'x' => 31, 'X' => 31
|
58
61
|
}
|
59
62
|
|
60
|
-
def self.
|
63
|
+
def self.number() NUMBER; end ## add num alias - why? why not?
|
61
64
|
|
62
65
|
|
63
|
-
=begin
|
64
|
-
BINARY = {
|
65
|
-
'1' => '00000', # 0
|
66
|
-
'2' => '00001', # 1
|
67
|
-
'3' => '00010', # 2
|
68
|
-
'4' => '00011', # 3
|
69
|
-
'5' => '00100', # 4
|
70
|
-
'6' => '00101', # 5
|
71
|
-
'7' => '00110', # 6
|
72
|
-
'8' => '00111', # 7
|
73
|
-
'9' => '01000', # 8
|
74
|
-
'a' => '01001', # 9
|
75
|
-
'b' => '01010', # 10
|
76
|
-
'c' => '01011', # 11
|
77
|
-
'd' => '01100', # 12
|
78
|
-
'e' => '01101', # 13
|
79
|
-
'f' => '01110', # 14
|
80
|
-
'g' => '01111', # 15
|
81
|
-
'h' => '10000', # 16
|
82
|
-
'i' => '10001', # 17
|
83
|
-
'j' => '10010', # 18
|
84
|
-
'k' => '10011', # 19
|
85
|
-
'm' => '10100', # 20
|
86
|
-
'n' => '10101', # 21
|
87
|
-
'o' => '10110', # 22
|
88
|
-
'p' => '10111', # 23
|
89
|
-
'q' => '11000', # 24
|
90
|
-
'r' => '11001', # 25
|
91
|
-
's' => '11010', # 26
|
92
|
-
't' => '11011', # 27
|
93
|
-
'u' => '11100', # 28
|
94
|
-
'v' => '11101', # 29
|
95
|
-
'w' => '11110', # 30
|
96
|
-
'x' => '11111' # 31
|
97
|
-
}
|
98
|
-
=end
|
99
|
-
|
100
66
|
## simple hash map (helper) for conversion to binary string
|
101
|
-
BINARY =
|
67
|
+
BINARY = _build_binary()
|
68
|
+
CODE = _build_code()
|
102
69
|
|
103
70
|
## add shortcuts (convenience) aliases
|
104
71
|
BIN = BINARY
|
105
72
|
NUM = NUMBER
|
106
73
|
|
74
|
+
def self.code() CODE; end
|
75
|
+
def self.binary() BINARY; end ## add bin alias - why? why not?
|
76
|
+
|
107
77
|
end # class Kai
|
108
78
|
end # module Base32
|
@@ -47,15 +47,10 @@ class TestBase32Crockford < MiniTest::Test
|
|
47
47
|
assert_raises(ArgumentError) { Base32::Crockford.decode("'+?") }
|
48
48
|
end
|
49
49
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
assert_equal "
|
54
|
-
assert_equal "0-01-6J",
|
55
|
-
Base32::Crockford.encode(1234, :length => 5, :split => 2)
|
56
|
-
assert_equal "00-010",
|
57
|
-
Base32::Crockford.encode(32, :length => 5, :split => 3)
|
50
|
+
def test_group_and_hyphens
|
51
|
+
assert_equal "16j", Base32::Crockford.fmt( 1234, group: 5)
|
52
|
+
assert_equal "1-6j", Base32::Crockford.fmt( 1234, group: 2, sep: '-')
|
53
|
+
assert_equal "10", Base32::Crockford.fmt( 32, group: 3)
|
58
54
|
end
|
59
|
-
=end
|
60
55
|
|
61
56
|
end # class TestBase32Crockford
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
###
|
4
|
+
# to run use
|
5
|
+
# ruby -I ./lib -I ./test test/test_base32_electrologica.rb
|
6
|
+
|
7
|
+
|
8
|
+
require 'helper'
|
9
|
+
|
10
|
+
|
11
|
+
class TestBase32Electrologia < MiniTest::Test
|
12
|
+
|
13
|
+
|
14
|
+
def test_electrologica
|
15
|
+
|
16
|
+
## Kitty #1001
|
17
|
+
## see https://cryptokittydex.com/kitties/1001
|
18
|
+
|
19
|
+
binary = 0b0000000000000000010010100101001010010011000111001110010000001000010111000001010010111101110011100000000101001010000000110001100010000100011010100000110010000000100011000110000000101001010010100110001100010100101000110100101000010010100101011011100111001110 # binary
|
20
|
+
hex = 0x00004a52931ce4085c14bdce014a0318846a0c808c60294a6314a34a_1295_b9ce # hex
|
21
|
+
|
22
|
+
el = "09-09-09-09-06-07-07-04-01-01-14-01-09-15-14-14-00-05-05-00-06-06-04-04-13-08-06-08-01-03-03-00-05-05-05-06-06-05-05-03-09-08-09-09-11-14-14-14"
|
23
|
+
el_fmt = "09-09-09-09 06-07-07-04 01-01-14-01 09-15-14-14 00-05-05-00 06-06-04-04 13-08-06-08 01-03-03-00 05-05-05-06 06-05-05-03 09-08-09-09 11-14-14-14"
|
24
|
+
el_fmt_ii = "09-09-09-09/06-07-07-04/01-01-14-01/09-15-14-14/00-05-05-00/06-06-04-04/13-08-06-08/01-03-03-00/05-05-05-06/06-05-05-03/09-08-09-09/11-14-14-14"
|
25
|
+
|
26
|
+
assert_equal binary, hex
|
27
|
+
|
28
|
+
|
29
|
+
Base32.format = :electrologica
|
30
|
+
el2 = Base32.encode( hex )
|
31
|
+
pp el
|
32
|
+
pp el2
|
33
|
+
|
34
|
+
assert_equal el, el2
|
35
|
+
assert_equal el_fmt, Base32::Electrologica.fmt( el2 )
|
36
|
+
assert_equal el_fmt_ii, Base32::Electrologica.fmt( el2, sep: '/' )
|
37
|
+
|
38
|
+
assert_equal el_fmt, Base32::Electrologica.fmt( hex, group: 4 )
|
39
|
+
assert_equal el_fmt_ii, Base32::Electrologica.fmt( hex, group: 4, sep: '/' )
|
40
|
+
|
41
|
+
|
42
|
+
hex2 = Base32.decode( el2 )
|
43
|
+
pp hex2
|
44
|
+
assert_equal hex, hex2
|
45
|
+
|
46
|
+
el3 = Base32::Electrologica.encode( hex )
|
47
|
+
hex3 = Base32::Electrologica.decode( el3 )
|
48
|
+
assert_equal hex, hex3
|
49
|
+
assert_equal el, el3
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
end # class TestBase32Electrologia
|
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
###
|
4
4
|
# to run use
|
5
|
-
# ruby -I ./lib -I ./test test/
|
5
|
+
# ruby -I ./lib -I ./test test/test_base32_kai.rb
|
6
6
|
|
7
7
|
|
8
8
|
require 'helper'
|
9
9
|
|
10
10
|
|
11
|
-
class
|
11
|
+
class TestBase32Kai < MiniTest::Test
|
12
12
|
|
13
13
|
|
14
14
|
def test_kai
|
@@ -19,7 +19,9 @@ def test_kai
|
|
19
19
|
binary = 0b0000000000000000010010100101001010010011000111001110010000001000010111000001010010111101110011100000000101001010000000110001100010000100011010100000110010000000100011000110000000101001010010100110001100010100101000110100101000010010100101011011100111001110 # binary
|
20
20
|
hex = 0x00004a52931ce4085c14bdce014a0318846a0c808c60294a6314a34a_1295_b9ce # hex
|
21
21
|
|
22
|
-
kai_fmt
|
22
|
+
kai_fmt = "aaaa 7885 22f2 agff 1661 7755 e979 2441 6667 7664 a9aa cfff"
|
23
|
+
kai_fmt_ii = "aaaa/7885/22f2/agff/1661/7755/e979/2441/6667/7664/a9aa/cfff"
|
24
|
+
kai_fmt8 = "aaaa7885 22f2agff 16617755 e9792441 66677664 a9aacfff"
|
23
25
|
kai = kai_fmt.gsub( ' ', '' )
|
24
26
|
|
25
27
|
assert_equal binary, hex
|
@@ -30,22 +32,29 @@ def test_kai
|
|
30
32
|
pp hex
|
31
33
|
|
32
34
|
Base32.format = :kai
|
33
|
-
kai2
|
35
|
+
kai2 = Base32.encode( hex )
|
34
36
|
pp kai
|
35
37
|
pp kai2
|
36
38
|
|
37
39
|
assert_equal kai, kai2
|
38
|
-
assert_equal kai_fmt,
|
40
|
+
assert_equal kai_fmt, Base32::Kai.fmt( kai2 )
|
41
|
+
assert_equal kai_fmt_ii, Base32::Kai.fmt( kai2, sep: '/' )
|
42
|
+
assert_equal kai_fmt8, Base32::Kai.fmt( kai2, group: 8 )
|
43
|
+
|
44
|
+
assert_equal kai_fmt, Base32::Kai.fmt( hex, group: 4 )
|
45
|
+
assert_equal kai_fmt_ii, Base32::Kai.fmt( hex, group: 4, sep: '/' )
|
46
|
+
assert_equal kai_fmt8, Base32::Kai.fmt( hex, group: 8 )
|
39
47
|
|
40
48
|
|
41
49
|
hex2 = Base32.decode( kai2 )
|
42
50
|
pp hex2
|
43
51
|
assert_equal hex, hex2
|
44
52
|
|
45
|
-
kai3
|
46
|
-
hex3
|
47
|
-
assert_equal hex,
|
48
|
-
assert_equal kai,
|
53
|
+
kai3 = Base32::Kai.encode( hex )
|
54
|
+
hex3 = Base32::Kai.decode( kai3 )
|
55
|
+
assert_equal hex, hex3
|
56
|
+
assert_equal kai, kai3
|
57
|
+
|
49
58
|
|
50
59
|
puts "kai.length: #{kai.length}" ## 48
|
51
60
|
puts " first: #{kai[0]}"
|
data/test/test_bytes.rb
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
###
|
4
|
+
# to run use
|
5
|
+
# ruby -I ./lib -I ./test test/test_bytes.rb
|
6
|
+
|
7
|
+
|
8
|
+
require 'helper'
|
9
|
+
|
10
|
+
|
11
|
+
class TestBytes < MiniTest::Test
|
12
|
+
|
13
|
+
|
14
|
+
def hex
|
15
|
+
## Kitty #1001
|
16
|
+
## see https://cryptokittydex.com/kitties/1001
|
17
|
+
0x00004a52931ce4085c14bdce014a0318846a0c808c60294a6314a34a_1295_b9ce # hex
|
18
|
+
end
|
19
|
+
|
20
|
+
def bytes
|
21
|
+
[9,9,9,9,6,7,7,4,1,1,14,1,9,15,14,14,0,5,5,0,6,6,4,4,13,8,6,8,1,3,3,0,5,5,5,6,6,5,5,3,9,8,9,9,11,14,14,14]
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
def test_bytes_kai
|
26
|
+
kai_fmt = "aaaa 7885 22f2 agff 1661 7755 e979 2441 6667 7664 a9aa cfff"
|
27
|
+
kai_fmt2 = "AAAA 7885 22F2 AGFF 1661 7755 E979 2441 6667 7664 A9AA CFFF"
|
28
|
+
kai = kai_fmt.gsub( ' ', '' )
|
29
|
+
|
30
|
+
Base32.format = :kai
|
31
|
+
|
32
|
+
pp bytes2 = Base32._bytes( hex )
|
33
|
+
pp bytes3 = Base32.bytes( hex )
|
34
|
+
pp bytes4 = Base32.bytes( kai )
|
35
|
+
pp bytes5 = Base32.bytes( kai_fmt )
|
36
|
+
pp bytes6 = Base32.bytes( kai_fmt2 )
|
37
|
+
|
38
|
+
pp bytes7 = Base32::Kai.bytes( hex )
|
39
|
+
pp bytes8 = Base32::Kai.bytes( kai )
|
40
|
+
pp bytes9 = Base32::Kai.bytes( kai_fmt )
|
41
|
+
pp bytes10 = Base32::Kai.bytes( kai_fmt2 )
|
42
|
+
|
43
|
+
assert_equal 48, bytes.size
|
44
|
+
assert_equal bytes, bytes2
|
45
|
+
assert_equal bytes, bytes3
|
46
|
+
assert_equal bytes, bytes4
|
47
|
+
assert_equal bytes, bytes5
|
48
|
+
assert_equal bytes, bytes6
|
49
|
+
assert_equal bytes, bytes7
|
50
|
+
assert_equal bytes, bytes8
|
51
|
+
assert_equal bytes, bytes9
|
52
|
+
assert_equal bytes, bytes10
|
53
|
+
|
54
|
+
## try encode bytes
|
55
|
+
assert_equal kai, Base32.encode( bytes )
|
56
|
+
assert_equal kai_fmt, Base32.fmt( bytes )
|
57
|
+
|
58
|
+
assert_equal kai, Base32::Kai.encode( bytes )
|
59
|
+
assert_equal kai_fmt, Base32::Kai.fmt( bytes )
|
60
|
+
|
61
|
+
## try decode/pack bytes
|
62
|
+
hex = Base32::Kai.decode( kai )
|
63
|
+
assert_equal hex, Base32._pack( bytes )
|
64
|
+
assert_equal Base32._bytes( hex ), Base32._bytes( Base32._pack( bytes ))
|
65
|
+
assert_equal bytes, Base32._bytes( hex )
|
66
|
+
|
67
|
+
assert_equal hex, Base32.decode( bytes )
|
68
|
+
assert_equal hex, Base32::Kai.decode( bytes )
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
def test_bytes_crockford
|
73
|
+
crockford_fmt = "9999 6774 11e1 9fee 0550 6644 d868 1330 5556 6553 9899 beee"
|
74
|
+
crockford_fmt2 = "9999 6774 11E1 9FEE 0550 6644 D868 1330 5556 6553 9899 BEEE"
|
75
|
+
crockford = crockford_fmt.gsub( ' ', '' )
|
76
|
+
|
77
|
+
Base32.format = :crockford
|
78
|
+
|
79
|
+
pp bytes2 = Base32._bytes( hex )
|
80
|
+
pp bytes3 = Base32.bytes( hex )
|
81
|
+
pp bytes4 = Base32.bytes( crockford )
|
82
|
+
pp bytes5 = Base32.bytes( crockford_fmt )
|
83
|
+
pp bytes6 = Base32.bytes( crockford_fmt2 )
|
84
|
+
|
85
|
+
pp bytes7 = Base32::Crockford.bytes( hex )
|
86
|
+
pp bytes8 = Base32::Crockford.bytes( crockford )
|
87
|
+
pp bytes9 = Base32::Crockford.bytes( crockford_fmt )
|
88
|
+
pp bytes10 = Base32::Crockford.bytes( crockford_fmt2 )
|
89
|
+
|
90
|
+
assert_equal 48, bytes.size
|
91
|
+
assert_equal bytes, bytes2
|
92
|
+
assert_equal bytes, bytes3
|
93
|
+
assert_equal bytes, bytes4
|
94
|
+
assert_equal bytes, bytes5
|
95
|
+
assert_equal bytes, bytes6
|
96
|
+
assert_equal bytes, bytes7
|
97
|
+
assert_equal bytes, bytes8
|
98
|
+
assert_equal bytes, bytes9
|
99
|
+
assert_equal bytes, bytes10
|
100
|
+
|
101
|
+
## try encode bytes
|
102
|
+
assert_equal crockford, Base32.encode( bytes )
|
103
|
+
assert_equal crockford_fmt, Base32.fmt( bytes )
|
104
|
+
|
105
|
+
assert_equal crockford, Base32::Crockford.encode( bytes )
|
106
|
+
assert_equal crockford_fmt, Base32::Crockford.fmt( bytes )
|
107
|
+
|
108
|
+
## try decode/pack bytes
|
109
|
+
hex = Base32::Crockford.decode( crockford )
|
110
|
+
assert_equal hex, Base32._pack( bytes )
|
111
|
+
assert_equal Base32._bytes( hex ), Base32._bytes( Base32._pack( bytes ))
|
112
|
+
assert_equal bytes, Base32._bytes( hex )
|
113
|
+
|
114
|
+
assert_equal hex, Base32.decode( bytes )
|
115
|
+
assert_equal hex, Base32::Crockford.decode( bytes )
|
116
|
+
end
|
117
|
+
|
118
|
+
|
119
|
+
def test_bytes_electrologica
|
120
|
+
el_fmt = "09-09-09-09 06-07-07-04 01-01-14-01 09-15-14-14 00-05-05-00 06-06-04-04 13-08-06-08 01-03-03-00 05-05-05-06 06-05-05-03 09-08-09-09 11-14-14-14"
|
121
|
+
el = "09-09-09-09-06-07-07-04-01-01-14-01-09-15-14-14-00-05-05-00-06-06-04-04-13-08-06-08-01-03-03-00-05-05-05-06-06-05-05-03-09-08-09-09-11-14-14-14"
|
122
|
+
|
123
|
+
Base32.format = :electrologica
|
124
|
+
|
125
|
+
pp bytes2 = Base32._bytes( hex )
|
126
|
+
pp bytes3 = Base32.bytes( hex )
|
127
|
+
pp bytes4 = Base32.bytes( el )
|
128
|
+
pp bytes5 = Base32.bytes( el_fmt )
|
129
|
+
|
130
|
+
pp bytes6 = Base32::Electrologica.bytes( hex )
|
131
|
+
pp bytes7 = Base32::Electrologica.bytes( el )
|
132
|
+
pp bytes8 = Base32::Electrologica.bytes( el_fmt )
|
133
|
+
|
134
|
+
assert_equal 48, bytes.size
|
135
|
+
assert_equal bytes, bytes2
|
136
|
+
assert_equal bytes, bytes3
|
137
|
+
assert_equal bytes, bytes4
|
138
|
+
assert_equal bytes, bytes5
|
139
|
+
assert_equal bytes, bytes6
|
140
|
+
assert_equal bytes, bytes7
|
141
|
+
assert_equal bytes, bytes8
|
142
|
+
|
143
|
+
## try encode bytes
|
144
|
+
assert_equal el, Base32.encode( bytes )
|
145
|
+
assert_equal el_fmt, Base32.fmt( bytes )
|
146
|
+
|
147
|
+
assert_equal el, Base32::Electrologica.encode( bytes )
|
148
|
+
assert_equal el_fmt, Base32::Electrologica.fmt( bytes )
|
149
|
+
|
150
|
+
## try decode/pack bytes
|
151
|
+
hex = Base32::Electrologica.decode( el )
|
152
|
+
assert_equal hex, Base32._pack( bytes )
|
153
|
+
assert_equal Base32._bytes( hex ), Base32._bytes( Base32._pack( bytes ))
|
154
|
+
assert_equal bytes, Base32._bytes( hex )
|
155
|
+
|
156
|
+
assert_equal hex, Base32.decode( bytes )
|
157
|
+
assert_equal hex, Base32::Electrologica.decode( bytes )
|
158
|
+
end
|
159
|
+
|
160
|
+
end # class TestBytes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: base32-alphabets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rdoc
|
@@ -44,12 +44,12 @@ email: wwwmake@googlegroups.com
|
|
44
44
|
executables: []
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files:
|
47
|
-
-
|
47
|
+
- CHANGELOG.md
|
48
48
|
- LICENSE.md
|
49
49
|
- Manifest.txt
|
50
50
|
- README.md
|
51
51
|
files:
|
52
|
-
-
|
52
|
+
- CHANGELOG.md
|
53
53
|
- LICENSE.md
|
54
54
|
- Manifest.txt
|
55
55
|
- README.md
|
@@ -62,8 +62,10 @@ files:
|
|
62
62
|
- lib/base32-alphabets/kai.rb
|
63
63
|
- lib/base32-alphabets/version.rb
|
64
64
|
- test/helper.rb
|
65
|
-
- test/test_base32.rb
|
66
65
|
- test/test_base32_crockford.rb
|
66
|
+
- test/test_base32_electrologica.rb
|
67
|
+
- test/test_base32_kai.rb
|
68
|
+
- test/test_bytes.rb
|
67
69
|
homepage: https://github.com/cryptocopycats/base32-alphabets
|
68
70
|
licenses:
|
69
71
|
- Public Domain
|