base58-alphabets 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 000b32a445c54f6eb556e6b6c3804bfa2d4df87c1caef2ff001b10a714b38403
4
- data.tar.gz: 2984225c57d1514eb716eaddfb07d66079ba7edcfacf9f722ecfcf22e84015e0
3
+ metadata.gz: 8735c482bb9af50045ac3bff88b5418b6e5960e03ea08c58f683063ffefae1ee
4
+ data.tar.gz: 5125cf91554db05f2036f6c9405c36bcaf87de991f9738f255a63228e916aa9d
5
5
  SHA512:
6
- metadata.gz: 9a71c550deb538e9928a743103435b5ac7c4f544e7d806529a84a52d4e76ff9ee49ccab0d33602a5deff93511dc92dede5e954697aa9cce59eef4c9da3c118dd
7
- data.tar.gz: 579c12f8837066b4f4265df52ff36108341a6d290a60d152a195d532171b7fa92cc0d2da58039cd1ce4babe5ab1ac90e85865619cf01ce251193d55cd5b16ead
6
+ metadata.gz: fc8da02281febbb8cea46a5c9f7b7c304fcfbff15ea22568195ee12f2d577870e8d2dabf358475de7b36b4e427a720b41e198ca965de2b9e4c1c1de1e4c44dd0
7
+ data.tar.gz: e5eb2757f961cdb42b5dc8b38796740ee9347ba9d0dcf49244c385468bf3ab8e51ad74a3e3793083961117a6bcf71a0ba61117231bff2a449a431469d5afd695
@@ -6,6 +6,9 @@ lib/base58-alphabets.rb
6
6
  lib/base58-alphabets/base.rb
7
7
  lib/base58-alphabets/base58.rb
8
8
  lib/base58-alphabets/bitcoin.rb
9
+ lib/base58-alphabets/flickr.rb
9
10
  lib/base58-alphabets/version.rb
10
11
  test/helper.rb
11
12
  test/test_base58_bitcoin.rb
13
+ test/test_base58_flickr.rb
14
+ test/test_bytes.rb
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Base58 Encoding / Decoding
2
2
 
3
- Encode / decode numbers with Bitcoin or Flickr base58 notation / alphabet
3
+ Encode / decode numbers, hex or binary strings (incl. leading zeros) with Bitcoin or Flickr base58 notation / alphabet
4
4
 
5
5
 
6
6
  * home :: [github.com/rubycoco/blockchain](https://github.com/rubycoco/blockchain)
@@ -11,31 +11,22 @@ Encode / decode numbers with Bitcoin or Flickr base58 notation / alphabet
11
11
 
12
12
 
13
13
 
14
- ## What's Base 58?
15
-
14
+ ## Base58 Alphabets
16
15
 
17
- > Similar to Base64, but modified to avoid both non-alphanumeric
18
- > characters (`+` and `/`) and letters which might look ambiguous
19
- > when printed (`0` - zero, `I` - capital i, `O` - capital o and `l` - lower case L).
20
- > Satoshi Nakamoto invented the base58 encoding scheme when creating bitcoin.
21
- > Some messaging and social media systems line break on non-alphanumeric
22
- > strings. This is avoided by not using URI reserved characters such as `+`.
23
- >
24
- > (Source: [Base58 @ Wikipedia](https://en.wikipedia.org/wiki/Binary-to-text_encoding#Base58))
16
+ [Bitcoin](#bitcoin)
17
+ [Flickr](#flickr)
25
18
 
26
19
 
27
20
 
28
- ## Bitcoin
21
+ ### Bitcoin
29
22
 
30
23
  The bitcoin notation / alphabet (`123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz`)
31
24
 
25
+ The order of it's 58 characters is numeric, uppercase-alpha, lowercase-alpha.
32
26
 
33
- Triva Quiz: What characters (digits/letters) are
34
- missing in the base 56 alphabets?
35
27
 
36
- - `0` (Zero), `O` (Upper-O), `I` (Upper-I), `l` (Lower-L)
37
28
 
38
- Why use base56 (and not "standard" base64)?
29
+ Why use base58 (and not "standard" base64)?
39
30
 
40
31
  ```
41
32
  // - Don't want 0OIl characters that look the same in some fonts and
@@ -49,7 +40,7 @@ Why use base56 (and not "standard" base64)?
49
40
 
50
41
 
51
42
 
52
- ### Bitcoin (Base58) Notation
43
+ #### Bitcoin (Base58) Notation
53
44
 
54
45
  |Num |Character |Num |Character |Num |Character |Num |Character|
55
46
  |----:|----------:|----:|----------:|----:|----------:|----:|--------:|
@@ -69,7 +60,159 @@ Why use base56 (and not "standard" base64)?
69
60
  | 52 | **u** | 53 | **v** | 54 | **w** | 55 | **x** |
70
61
  | 56 | **y** | 57 | **z** |
71
62
 
63
+ Note: `0` (Zero), `O` (Upper-O), `I` (Upper-I), `l` (Lower-L) - these four characters (digits/letters) are
64
+ missing in the base 58 alphabets.
65
+
66
+
67
+ **Usage**
68
+
69
+
70
+ Encode / Decode Numbers
71
+
72
+ ```ruby
73
+ require 'base58-alphabets'
74
+
75
+ Base58.encode_num( 100 ) #=> "2j"
76
+ Base58.encode_num( 123456789 ) #=> "BukQL"
77
+
78
+ Base58.decode_num( "2j") #=> 100
79
+ Base58.decode_num( "BukQL" ) #=> 123456789
80
+
81
+ # or
82
+
83
+ Base58::Bitcoin.encode_num( 100 ) #=> "2j"
84
+ Base58::Bitcoin.encode_num( 123456789 ) #=> "BukQL"
85
+
86
+ Base58::Bitcoin.decode_num( "2j") #=> 100
87
+ Base58::Bitcoin.decode_num( "BukQL" ) #=> 123456789
88
+ ```
89
+
90
+ Encode / Decode Hex Strings
91
+
92
+ ```ruby
93
+ Base58.encode_hex( "626262" ) #=> "a3gV"
94
+ # Note: The `0x` or `0X` hex prefix is optional.
95
+ Base58.encode_hex( "0x626262" ) #=> "a3gV"
96
+ Base58.encode_hex( "0X626262" ) #=> "a3gV"
97
+ Base58.encode_hex( "516b6fcd0f" ) #=> "ABnLTmg"
98
+
99
+ Base58.decode_hex( "a3gV" ) #=> "626262"
100
+ Base58.decode_hex( "ABnLTmg" ) #=> "516b6fcd0f"
101
+
102
+ # or
103
+
104
+ Base58::Bitcoin.encode_hex( "626262" ) #=> "a3gV"
105
+ Base58::Bitcoin.encode_hex( "516b6fcd0f" ) #=> "ABnLTmg"
106
+
107
+ Base58::Bitcoin.decode_hex( "a3gV" ) #=> "626262"
108
+ Base58::Bitcoin.decode_hex( "ABnLTmg" ) #=> "516b6fcd0f"
109
+ ```
110
+
111
+
112
+ What about leading zeros?
113
+
114
+ Yes, if you use a hex or binary string - the leading zero bytes
115
+ will get encoded / decoded (converted from `00` to `1` and back):
116
+
117
+ ```ruby
118
+ Base58.encode_hex( "00000000000000000000" ) #=> "1111111111"
119
+ Base58.encode_hex( "00000000000000000000123456789abcdef0" ) #=> "111111111143c9JGph3DZ"
120
+
121
+ Base58.decode_hex( "1111111111" ) #=> "00000000000000000000"
122
+ Base58.decode_hex( "111111111143c9JGph3DZ" ) #=> "00000000000000000000123456789abcdef0"
123
+ ```
124
+
125
+
126
+
127
+ Encode / Decode Bin(ary) Strings
128
+
129
+ ```ruby
130
+ Base58.encode_bin( "\xCE\xE99\x86".b ) #=> "6Hknds"
131
+
132
+ Base58.decode_bin( "6Hknds" ) #=> "\xCE\xE99\x86"
133
+
134
+ # or
135
+
136
+ Base58::Bitcoin.encode_bin( "\xCE\xE99\x86".b ) #=> "6Hknds"
137
+
138
+ Base58::Bitcoin.decode_bin( "6Hknds" ) #=> "\xCE\xE99\x86"
139
+ ```
140
+
141
+
142
+
143
+
144
+ ### Flickr
145
+
146
+ The flickr notation / alphabet (`123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ`)
147
+
148
+ The order of it's 58 characters is numeric, lowercase-alpha, uppercase-alpha.
149
+
150
+
151
+
152
+ Encode / Decode Numbers
153
+
154
+ ```ruby
155
+ Base58.format = :flickr # switch to flickr alphabet (default is bitcoin)
156
+
157
+ Base58.encode_num( 12345 ) #=> "4ER"
158
+
159
+ Base58.decode_num( "4ER" ) #=> 12345
160
+
161
+ # or
162
+
163
+ Base58::Flickr.encode_num( 12345 ) #=> "4ER"
164
+
165
+ Base58::Flickr.decode_num( "4ER" ) #=> 12345
166
+ ```
167
+
168
+
169
+ That's it.
170
+
171
+
172
+
173
+ ## What's Base 58?
174
+
175
+ The number of characters you are left with when you use
176
+ all the characters in the alphanumeric alphabet,
177
+ but remove all the easily mistakable characters like `0`, `O`, `l` and `I`
178
+ is... 58.
179
+
180
+ alphanumeric = `0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz` (62 characters)
181
+
182
+ base58 = ` 123456789ABCDEFGH JKLMN PQRSTUVWXYZabcdefghijk mnopqrstuvwxyz` (58 characters)
183
+
184
+
185
+
186
+ From the Wikipedia:
187
+
188
+ > Similar to Base64, but modified to avoid both non-alphanumeric
189
+ > characters (`+` and `/`) and letters which might look ambiguous
190
+ > when printed (`0` - zero, `I` - capital i, `O` - capital o and `l` - lower case L).
191
+ > Satoshi Nakamoto invented the base58 encoding scheme when creating bitcoin.
192
+ > Some messaging and social media systems line break on non-alphanumeric
193
+ > strings. This is avoided by not using URI reserved characters such as `+`.
194
+ >
195
+ > (Source: [Base58 @ Wikipedia](https://en.wikipedia.org/wiki/Binary-to-text_encoding#Base58))
196
+
197
+
198
+
199
+ Why use base58?
200
+
201
+ The more characters you have in your base, the less you will need to use to represent big numbers.
202
+ The bigger your base, the shorter your "number". Example:
203
+
204
+ ``` ruby
205
+ base10( 9999 ) #=> 9999 - decimal
206
+ base16( 9999 ) #=> 270f - hexadecimal
207
+ base58( 9999 ) #=> 3yQ
208
+ ```
209
+
210
+
211
+ ## Install
212
+
213
+ Just install the gem:
72
214
 
215
+ $ gem install base58-alphabets
73
216
 
74
217
 
75
218
 
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ Hoe.spec 'base58-alphabets' do
5
5
 
6
6
  self.version = Base58::VERSION
7
7
 
8
- self.summary = "base58-alphabets - base58 encoding / decoding with bitcoin or flickr notation / alphabet"
8
+ self.summary = "base58-alphabets - base58 encoding / decoding numbers, hex or binary strings (incl. leading zeros) with bitcoin or flickr notation / alphabet"
9
9
  self.description = summary
10
10
 
11
11
  self.urls = { home: 'https://github.com/rubycoco/blockchain' }
@@ -8,6 +8,7 @@ require 'base58-alphabets/version' # note: let version always go first
8
8
 
9
9
  require 'base58-alphabets/base'
10
10
  require 'base58-alphabets/bitcoin'
11
+ require 'base58-alphabets/flickr'
11
12
  require 'base58-alphabets/base58'
12
13
 
13
14
 
@@ -5,37 +5,115 @@
5
5
  module Base58
6
6
  class Base
7
7
 
8
- # Converts a base10 integer to a base58 string.
9
- def self.encode( num_or_bytes )
10
- if num_or_bytes.is_a?( Array )
11
- bytes = num_or_bytes
12
- else
13
- num = num_or_bytes
14
- bytes = Base58._bytes( num )
15
- end
8
+ BASE = 58 # ALPHABET.length == 58 ## 58 chars/letters/digits
9
+
10
+ ## check if it is a hex (string)
11
+ ## - allow optiona 0x or 0X and allow abcdef and ABCDEF
12
+ HEX_RE = /\A(?:0x)?[0-9a-f]+\z/i
13
+
14
+
16
15
 
17
- bytes.reduce( String.new ) do |buf, byte|
16
+ # Converts a base10 integer to a base58 string.
17
+ def self.encode_num( num ) ## num_to_base58 / int_to_base58
18
+ _bytes( num ).reduce( String.new ) do |buf, byte|
18
19
  buf << alphabet[byte]
19
20
  buf
20
21
  end
21
22
  end
22
23
 
23
24
 
25
+ # Converts binary string into its Base58 representation.
26
+ # If string is empty returns an empty string.
27
+ def self.encode_bin( data ) ## todo/check: add alias such as bin_to_base58 / data_to_base58 - why? why not?
28
+ leading_zeroes = 0
29
+ num = 0
30
+ base = 1
31
+ data.bytes.reverse_each do |byte|
32
+ if byte == 0
33
+ leading_zeroes += 1
34
+ else
35
+ leading_zeroes = 0
36
+ num += base*byte
37
+ end
38
+ base *= 256
39
+ end
40
+
41
+ (alphabet[0]*leading_zeroes) + encode_num( num )
42
+ end
43
+
44
+
45
+ # Converts hex string into its Base58 representation.
46
+ def self.encode_hex( str )
47
+ ## todo/check: allow empty string ("") - why? why not?
48
+ raise ArgumentError, "expected hex string (0-9a-f) - got >#{str}< - can't pack string; sorry" unless str =~ HEX_RE || str.empty?
49
+
50
+ str = _strip0x( str ) ## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!!
51
+ encode_bin( [str].pack('H*') )
52
+ end
53
+
54
+
55
+
56
+
24
57
  # Converts a base58 string to a base10 integer.
25
- def self.decode( str_or_bytes )
26
- if str_or_bytes.is_a?( Array )
27
- bytes = str_or_bytes
28
- else ## assume string
29
- str = str_or_bytes
30
- bytes = str.each_char.reduce([]) do |bytes,char|
31
- byte = number[char]
32
- raise ArgumentError, "Value passed not a valid base58 string - >#{char}< not found in alphabet" if byte.nil?
33
- bytes << byte
34
- bytes
35
- end
58
+ def self.decode_num( str ) ## todo/check: add alias base58_to_num / base58_to_int
59
+ bytes = str.each_char.reduce([]) do |bytes,char|
60
+ byte = number[char]
61
+ raise ArgumentError, "Value passed not a valid base58 string - >#{char}< not found in alphabet" if byte.nil?
62
+ bytes << byte
63
+ bytes
64
+ end
65
+ _pack( bytes )
66
+ end
67
+
68
+
69
+ def self.decode_bin( str )
70
+ num = decode_num( str )
71
+
72
+ ## use base 256 for characters to binary conversion!!!
73
+ data = _bytes( num, base: 256 ).pack( 'C*' )
74
+
75
+ ## check for leading zeros
76
+ str.bytes.each do |byte|
77
+ break if byte != alphabet[0].ord
78
+ data = "\x00" + data
36
79
  end
37
- Base58._pack( bytes )
80
+ data
81
+ end
82
+
83
+ def self.decode_hex( str )
84
+ decode_bin( str ).unpack( 'H*' )[0]
38
85
  end
39
86
 
87
+
88
+
89
+
90
+ ####
91
+ # (private) helper - note: leading underscore in name e.g. _bytes
92
+ def self._bytes( num, base: BASE ) ## num_to_bytes
93
+ ## note: 0 leads to empty [] and NOT [0] !!!!
94
+ b = []
95
+ while num > 0
96
+ num, mod = num.divmod( base )
97
+ b << mod
98
+ end
99
+ b = b.reverse
100
+ b
101
+ end
102
+
103
+
104
+ def self._pack( bytes ) ## bytes_to_num
105
+ num = 0
106
+ bytes.reverse.each_with_index do |byte,index|
107
+ num += byte * (BASE**(index))
108
+ end
109
+ num
110
+ end
111
+
112
+
113
+ def self._strip0x( str ) ## todo/check: add alias e.g. strip_hex_prefix or such - why? why not?
114
+ (str[0,2] == '0x' || str[0,2] == '0X') ? str[2..-1] : str
115
+ end
116
+
117
+
40
118
  end # class Base
41
119
  end # module Base58
@@ -1,12 +1,10 @@
1
1
  module Base58
2
2
 
3
- BASE = 58 # ALPHABET.length == 58 ## 58 chars/letters/digits
4
-
5
3
  class Configuration
6
4
 
7
5
  MAPPING = {
8
6
  bitcoin: Bitcoin,
9
- ## flickr: Flickr,
7
+ flickr: Flickr,
10
8
  }
11
9
 
12
10
  attr_reader :format
@@ -44,38 +42,15 @@ module Base58
44
42
 
45
43
 
46
44
 
47
- def self.encode( num_or_bytes, klass: configuration.format )
48
- klass.encode( num_or_bytes )
49
- end
50
-
51
- def self.decode( str_or_bytes, klass: configuration.format )
52
- klass.decode( str_or_bytes )
53
- end
54
-
55
-
45
+ def self.encode_num( num, klass: configuration.format ) klass.encode_num( num ); end
46
+ def self.encode_bin( data, klass: configuration.format ) klass.encode_bin( data ); end
47
+ def self.encode_hex( str, klass: configuration.format ) klass.encode_hex( str ); end
56
48
 
49
+ def self.decode_num( str, klass: configuration.format ) klass.decode_num( str ); end
50
+ def self.decode_bin( str, klass: configuration.format ) klass.decode_bin( str ); end
51
+ def self.decode_hex( str, klass: configuration.format ) klass.decode_hex( str ); end
57
52
 
58
- ####
59
- # (private) helper - note: leading underscore in name e.g. _bytes
60
- def self._bytes( num )
61
- b = []
62
- while num >= BASE
63
- mod = num % BASE
64
- b << mod
65
- num = (num - mod) / BASE
66
- end
67
- b << num
68
- b = b.reverse
69
- b
70
- end
71
53
 
72
- def self._pack( bytes )
73
- num = 0
74
- bytes.reverse.each_with_index do |byte,index|
75
- num += byte * (BASE**(index))
76
- end
77
- num
78
- end
79
54
 
80
55
  ## encoding alphabet - letter-to-number by index / array
81
56
  def self.alphabet( klass: configuration.format ) klass.alphabet; end
@@ -0,0 +1,85 @@
1
+
2
+
3
+ module Base58
4
+ class Flickr < Base
5
+
6
+ ALPHABET = %w[ 1 2 3 4 5 6 7 8 9
7
+ a b c d e f g h i j k m n o p q r s t u v w x y z
8
+ A B C D E F G H J K L M N P Q R S T U V W X Y Z ]
9
+
10
+
11
+ def self.alphabet() ALPHABET; end ## add alpha / char aliases - why? why not?
12
+
13
+ ## Note:
14
+ ## for decoding allow (misspelled) l/I for 1 - why? why not?
15
+ ## and (misspelled) 0/O for o - why? why not?
16
+
17
+
18
+ NUMBER = {
19
+ '1' => 0, ## 'l' => 0, 'I' => 0,
20
+ '2' => 1,
21
+ '3' => 2,
22
+ '4' => 3,
23
+ '5' => 4,
24
+ '6' => 5,
25
+ '7' => 6,
26
+ '8' => 7,
27
+ '9' => 8,
28
+ 'a' => 9,
29
+ 'b' => 10,
30
+ 'c' => 11,
31
+ 'd' => 12,
32
+ 'e' => 13,
33
+ 'f' => 14,
34
+ 'g' => 15,
35
+ 'h' => 16,
36
+ 'i' => 17,
37
+ 'j' => 18,
38
+ 'k' => 19,
39
+ 'm' => 20,
40
+ 'n' => 21,
41
+ 'o' => 22, ## '0' => 22, 'O' => 22,
42
+ 'p' => 23,
43
+ 'q' => 24,
44
+ 'r' => 25,
45
+ 's' => 26,
46
+ 't' => 27,
47
+ 'u' => 28,
48
+ 'v' => 29,
49
+ 'w' => 30,
50
+ 'x' => 31,
51
+ 'y' => 32,
52
+ 'z' => 33,
53
+ 'A' => 34,
54
+ 'B' => 35,
55
+ 'C' => 36,
56
+ 'D' => 37,
57
+ 'E' => 38,
58
+ 'F' => 39,
59
+ 'G' => 40,
60
+ 'H' => 41,
61
+ 'J' => 42,
62
+ 'K' => 43,
63
+ 'L' => 44,
64
+ 'M' => 45,
65
+ 'N' => 46,
66
+ 'P' => 47,
67
+ 'Q' => 48,
68
+ 'R' => 49,
69
+ 'S' => 50,
70
+ 'T' => 51,
71
+ 'U' => 52,
72
+ 'V' => 53,
73
+ 'W' => 54,
74
+ 'X' => 55,
75
+ 'Y' => 56,
76
+ 'Z' => 57,
77
+ }
78
+
79
+ def self.number() NUMBER; end ## add num alias - why? why not?
80
+
81
+ ## add shortcuts (convenience) aliases
82
+ NUM = NUMBER
83
+
84
+ end # class Flickr
85
+ end # module Base58
@@ -1,8 +1,8 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Base58
4
- MAJOR = 0
5
- MINOR = 1
4
+ MAJOR = 1
5
+ MINOR = 0
6
6
  PATCH = 0
7
7
  VERSION = [MAJOR,MINOR,PATCH].join('.')
8
8
 
@@ -8,25 +8,99 @@ require 'helper'
8
8
 
9
9
  class TestBase58Bitcoin < MiniTest::Test
10
10
 
11
+ NUM_TESTS = [
12
+ [100, "2j"],
13
+ [12345, "4fr"],
14
+ [6639914, "b2pH"],
15
+ [123456789, "BukQL"],
11
16
 
12
- def test_bitcoin
13
- assert_equal "2j", Base58::Bitcoin.encode( 100 )
14
- assert_equal "4fr", Base58::Bitcoin.encode( 12345 )
15
- assert_equal "b2pH", Base58::Bitcoin.encode( 6639914 )
17
+ [3471391110, "6Hknds"],
18
+ [3470152229, "6HeSMr"],
19
+ [3470988633, "6Hiizc"],
20
+ [3470993664, "6HikVM"],
21
+ [3471485480, "6HmGgw"],
22
+ [3469844075, "6Hcrkr"],
23
+ ]
16
24
 
17
- assert_equal 100, Base58::Bitcoin.decode( "2j" )
18
- assert_equal 12345, Base58::Bitcoin.decode( "4fr" )
19
- assert_equal 6639914, Base58::Bitcoin.decode( "b2pH" )
25
+ HEX_TESTS = [
26
+ ["", ""],
27
+ ["00","1"],
28
+ ["00000000000000000000", "1111111111"],
29
+ ["00000000000000000000123456789abcdef0", "111111111143c9JGph3DZ"],
30
+ ["13", "L"],
31
+ ["2e", "o"],
32
+ ["61", "2g"],
33
+ ["626262", "a3gV"],
34
+ ["636363", "aPEr"],
35
+ ["73696d706c792061206c6f6e6720737472696e67", "2cFupjhnEsSn59qHXstmK2ffpLv2"],
36
+ ["00eb15231dfceb60925886b67d065299925915aeb172c06647", "1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L"],
37
+ ["0093ce48570b55c42c2af816aeaba06cfee1224faebb6127fe", "1EUXSxuUVy2PC5enGXR1a3yxbEjNWMHuem"],
38
+ ["516b6fcd0f", "ABnLTmg"],
39
+ ["bf4f89001e670274dd", "3SEo3LWLoPntC"],
40
+ ["572e4794", "3EFU7m"],
41
+ ["ecac89cad93923c02321", "EJDM8drfXA6uyA"],
42
+ ["10c8511e", "Rt5zm"],
43
+ ]
20
44
 
21
45
 
22
- Base58.format = :bitcoin
23
- assert_equal "2j", Base58.encode( 100 )
24
- assert_equal "4fr", Base58.encode( 12345 )
25
- assert_equal "b2pH", Base58.encode( 6639914 )
26
46
 
27
- assert_equal 100, Base58.decode( "2j" )
28
- assert_equal 12345, Base58.decode( "4fr" )
29
- assert_equal 6639914, Base58.decode( "b2pH" )
47
+
48
+
49
+ BIN_TESTS = [
50
+ ["\xCE\xE99\x86".b, "6Hknds"],
51
+ ["\xCE\xD6R%".b, "6HeSMr"],
52
+ ["\xCE\xE3\x15Y".b, "6Hiizc"],
53
+ ["\xCE\xE3)\x00".b, "6HikVM"],
54
+ ["\xCE\xEA\xAA(".b, "6HmGgw"],
55
+ ["\xCE\xD1\x9Ek".b, "6Hcrkr"],
56
+ ["\x01\xAD<l'\xAF!\x96N\x93u\x93\xE2\xAF\x92p\x96=\x89n\xD7\x953\x17\x12\x8E\xBD\xA2\x04\x84~Z".b, "7Ycsh3K7oGpLTcQpNx1h7Z19fVbZ4TXqEYePfdFwDZT"],
57
+
58
+ ["".b, ""],
59
+ [" ".b, "Z"],
60
+ ["-".b, "n"],
61
+ ["0".b, "q"],
62
+ ["1".b, "r"],
63
+ ["-1".b, "4SU"],
64
+ ["11".b, "4k8"],
65
+ ["abc".b, "ZiCa"],
66
+ ["1234598760".b, "3mJr7AoUXx2Wqd"],
67
+ ["abcdefghijklmnopqrstuvwxyz".b, "3yxU3u1igY8WkgtjK92fbJQCd4BZiiT1v25f"],
68
+ ["00000000000000000000000000000000000000000000000000000000000000".b, "3sN2THZeE9Eh9eYrwkvZqNstbHGvrxSAM7gXUXvyFQP8XvQLUqNCS27icwUeDT7ckHm4FUHM2mTVh1vbLmk7y"],
69
+ ]
70
+
71
+
72
+
73
+ def test_num
74
+ NUM_TESTS.each do |item|
75
+ assert_equal item[1], Base58::Bitcoin.encode_num( item[0] )
76
+ assert_equal item[0], Base58::Bitcoin.decode_num( item[1] )
77
+
78
+ Base58.format = :bitcoin
79
+ assert_equal item[1], Base58.encode_num( item[0] )
80
+ assert_equal item[0], Base58.decode_num( item[1] )
81
+ end
82
+ end
83
+
84
+ def test_hex
85
+ HEX_TESTS.each do |item|
86
+ assert_equal item[1], Base58::Bitcoin.encode_hex( item[0] )
87
+ assert_equal item[0], Base58::Bitcoin.decode_hex( item[1] )
88
+
89
+ Base58.format = :bitcoin
90
+ assert_equal item[1], Base58.encode_hex( item[0] )
91
+ assert_equal item[0], Base58.decode_hex( item[1] )
92
+ end
93
+ end
94
+
95
+ def test_bin
96
+ BIN_TESTS.each do |item|
97
+ assert_equal item[1], Base58::Bitcoin.encode_bin( item[0] )
98
+ assert_equal item[0], Base58::Bitcoin.decode_bin( item[1] )
99
+
100
+ Base58.format = :bitcoin
101
+ assert_equal item[1], Base58.encode_bin( item[0] )
102
+ assert_equal item[0], Base58.decode_bin( item[1] )
103
+ end
30
104
  end
31
105
 
32
106
 
@@ -0,0 +1,35 @@
1
+ ###
2
+ # to run use
3
+ # ruby -I ./lib -I ./test test/test_base58_flickr.rb
4
+
5
+
6
+ require 'helper'
7
+
8
+
9
+ class TestBase58Flickr < MiniTest::Test
10
+
11
+ NUM_TESTS = [
12
+ [3471391110, "6hKMCS"],
13
+ [3470152229, "6hDrmR"],
14
+ [3470988633, "6hHHZB"],
15
+ [3470993664, "6hHKum"],
16
+ [3471485480, "6hLgFW"],
17
+ [3469844075, "6hBRKR"],
18
+ [3470820062, "6hGRTd"],
19
+ [3469966999, "6hCuie"],
20
+ ]
21
+
22
+
23
+ def test_num
24
+ NUM_TESTS.each do |item|
25
+ assert_equal item[1], Base58::Flickr.encode_num( item[0] )
26
+ assert_equal item[0], Base58::Flickr.decode_num( item[1] )
27
+
28
+ Base58.format = :flickr
29
+ assert_equal item[1], Base58.encode_num( item[0] )
30
+ assert_equal item[0], Base58.decode_num( item[1] )
31
+ end
32
+ end
33
+
34
+
35
+ end # class TestBase58Flickr
@@ -0,0 +1,26 @@
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 test_bytes
15
+ bytes = Base58::Base._bytes( 123456789 )
16
+ ## pp bytes
17
+
18
+ assert_equal [10,52,43,23,19], bytes
19
+
20
+ assert_equal 123456789, Base58::Base._pack( bytes )
21
+
22
+
23
+ assert_equal 123456789, Base58::Base._pack( Base58::Base._bytes( 123456789 ))
24
+ end
25
+
26
+ end # class TestBytes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: base58-alphabets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.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: 2021-01-16 00:00:00.000000000 Z
11
+ date: 2021-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rdoc
@@ -44,8 +44,8 @@ dependencies:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
46
  version: '3.22'
47
- description: base58-alphabets - base58 encoding / decoding with bitcoin or flickr
48
- notation / alphabet
47
+ description: base58-alphabets - base58 encoding / decoding numbers, hex or binary
48
+ strings (incl. leading zeros) with bitcoin or flickr notation / alphabet
49
49
  email: wwwmake@googlegroups.com
50
50
  executables: []
51
51
  extensions: []
@@ -62,9 +62,12 @@ files:
62
62
  - lib/base58-alphabets/base.rb
63
63
  - lib/base58-alphabets/base58.rb
64
64
  - lib/base58-alphabets/bitcoin.rb
65
+ - lib/base58-alphabets/flickr.rb
65
66
  - lib/base58-alphabets/version.rb
66
67
  - test/helper.rb
67
68
  - test/test_base58_bitcoin.rb
69
+ - test/test_base58_flickr.rb
70
+ - test/test_bytes.rb
68
71
  homepage: https://github.com/rubycoco/blockchain
69
72
  licenses:
70
73
  - Public Domain
@@ -89,6 +92,6 @@ requirements: []
89
92
  rubygems_version: 3.1.4
90
93
  signing_key:
91
94
  specification_version: 4
92
- summary: base58-alphabets - base58 encoding / decoding with bitcoin or flickr notation
93
- / alphabet
95
+ summary: base58-alphabets - base58 encoding / decoding numbers, hex or binary strings
96
+ (incl. leading zeros) with bitcoin or flickr notation / alphabet
94
97
  test_files: []