base58_gmp 0.0.6 → 0.0.7
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.
- data/CHANGELOG +4 -0
- data/README.rdoc +24 -12
- data/VERSION +1 -1
- data/base58_gmp.gemspec +4 -4
- data/lib/base58_gmp.rb +38 -23
- data/test/test_alphabets.rb +29 -0
- data/test/test_base58_gmp.rb +2 -2
- metadata +6 -6
- data/test/test_flickr-gmp.rb +0 -18
data/CHANGELOG
CHANGED
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= Base58GMP
|
2
2
|
|
3
|
-
High speed Base58 encoding using GMP with MD5 support and transcoding between
|
3
|
+
High speed Base58 encoding using GMP with MD5 support and transcoding between Flickr, Bitcoin and GMP alphabets.
|
4
4
|
|
5
5
|
== Installation
|
6
6
|
|
@@ -21,40 +21,52 @@ Download and install base58_gmp with the following:
|
|
21
21
|
# Encode GMP::Z Int as Base58
|
22
22
|
Base58GMP.encode(GMP::Z(12345)) # => 4ER
|
23
23
|
|
24
|
-
# Encode as Base58 using
|
24
|
+
# Encode as Base58 using alternate alphabets
|
25
|
+
Base58GMP.encode(12345, 'bitcoin') # => 4fr
|
25
26
|
Base58GMP.encode(12345, 'gmp') # => 3cn
|
26
27
|
|
27
28
|
# Decode Base58 as GMP::Z Integer
|
28
29
|
Base58GMP.decode('4ER') # => 12345
|
29
30
|
|
30
|
-
# Decode Base58 as GMP::Z Integer using
|
31
|
+
# Decode Base58 as GMP::Z Integer using alternate alphabets
|
32
|
+
Base58GMP.decode('4fr', 'bitcoin') # => 12345
|
31
33
|
Base58GMP.decode('3cn', 'gmp') # => 12345
|
32
34
|
|
33
35
|
# MD5 Base58 Digest
|
34
36
|
Base58GMP.md5('foo@bar.com') # => w6fdCRXnUXyz7EtDn5TgN9
|
35
37
|
|
36
|
-
# Convert between
|
37
|
-
Base58GMP.
|
38
|
-
Base58GMP.
|
38
|
+
# Convert between alphabets
|
39
|
+
Base58GMP.from_to('123456789abcdefghijk','flickr','gmp') # => 0123456789ABCDEFGHIJ
|
40
|
+
Base58GMP.from_to('0123456789ABCDEFGHIJ','gmp','flickr') # => 123456789abcdefghijk
|
39
41
|
|
40
42
|
== Notes
|
41
43
|
|
42
|
-
=== Base58 Implementations - Flickr and GMP
|
44
|
+
=== Base58 Implementations - Flickr, Bitcoin and GMP
|
43
45
|
|
44
|
-
This class supports the Base58
|
46
|
+
This class supports the Base58 alphabets used by Flickr, Bitcoin and GMP. The Flickr alphabet is the default and used when no alphabet is provided.
|
45
47
|
|
46
|
-
|
48
|
+
Flickr Alphabet: [0-9a-zA-Z] excluding [0OIl] to improve human readability
|
47
49
|
|
48
|
-
|
50
|
+
Bitcoin Alphabet: [0-9A-Za-z] excluding [0OIl] to improve human readability
|
49
51
|
|
50
|
-
|
52
|
+
GMP Alphabet: [0-9A-Za-v]
|
51
53
|
|
52
|
-
The encode, decode and md5 methods support an alphabet parameter which can be set to 'gmp' to indicate the value to be encoded or decoded.
|
54
|
+
The encode, decode and md5 methods support an alphabet parameter which can be set to the supported alphabets ['bitcoin', 'flickr', 'gmp'] to indicate the value to be encoded or decoded.
|
53
55
|
|
54
56
|
=== GMP
|
55
57
|
|
56
58
|
This class requires GMP 4.2.0 or above. Prior versions are limited to Base36.
|
57
59
|
|
60
|
+
== Links
|
61
|
+
|
62
|
+
Flickr post introducing Base58:
|
63
|
+
|
64
|
+
http://www.flickr.com/groups/api/discuss/72157616713786392/
|
65
|
+
|
66
|
+
Bitcoin wiki Base58 article:
|
67
|
+
|
68
|
+
https://en.bitcoin.it/wiki/Base58Check_encoding
|
69
|
+
|
58
70
|
== Problems, Comments, Suggestions?
|
59
71
|
|
60
72
|
All of the above are most welcome. mailto:johncwang@gmail.com
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.7
|
data/base58_gmp.gemspec
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'base58_gmp'
|
3
|
-
s.version = '0.0.
|
4
|
-
s.date = '2011-11-
|
3
|
+
s.version = '0.0.7'
|
4
|
+
s.date = '2011-11-21'
|
5
5
|
s.summary = 'High speed Base58 encoding using GMP with MD5 support'
|
6
|
-
s.description = 'Base58 encoding using
|
6
|
+
s.description = 'High speed Base58 encoding using GMP with MD5 support and transcoding between Flickr, Bitcoin and GMP alphabets.'
|
7
7
|
s.authors = ['John Wang']
|
8
8
|
s.email = 'johncwang@gmail.com'
|
9
9
|
s.homepage =
|
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
|
|
21
21
|
'lib/base58_gmp.rb',
|
22
22
|
'test/test_base58_gmp.rb',
|
23
23
|
'test/test_encode-decode-multi.rb',
|
24
|
-
'test/
|
24
|
+
'test/test_alphabets.rb'
|
25
25
|
]
|
26
26
|
|
27
27
|
end
|
data/lib/base58_gmp.rb
CHANGED
@@ -9,53 +9,68 @@ require 'gmp'
|
|
9
9
|
|
10
10
|
class Base58GMP
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
ALPHABETS = {
|
13
|
+
'bitcoin' => '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz',
|
14
|
+
'flickr' => '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ',
|
15
|
+
'gmp' => '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv'
|
16
|
+
}
|
17
|
+
|
14
18
|
DEFAULT_ALPHABET = 'flickr'
|
19
|
+
GMP_ALPHABET = 'gmp'
|
15
20
|
|
16
|
-
def self.
|
17
|
-
base58 =
|
18
|
-
|
19
|
-
GMP::Z(
|
21
|
+
def self.integer_to_base58(integer, alphabet=DEFAULT_ALPHABET)
|
22
|
+
base58 = integer.is_a?(GMP::Z) ?
|
23
|
+
integer.to_s(base = 58) :
|
24
|
+
GMP::Z(integer).to_s(base = 58)
|
20
25
|
|
21
|
-
alphabet.is_a?(String) && alphabet.downcase ==
|
26
|
+
alphabet.is_a?(String) && alphabet.downcase == GMP_ALPHABET ?
|
22
27
|
base58 :
|
23
|
-
|
28
|
+
from_to(base58, GMP_ALPHABET, alphabet)
|
24
29
|
end
|
25
30
|
|
26
|
-
def self.
|
27
|
-
|
31
|
+
def self.base58_to_integer(base58, alphabet=DEFAULT_ALPHABET)
|
32
|
+
unless base58.is_a?(String)
|
28
33
|
raise ArgumentError, 'Base58 argument is not a string.'
|
29
34
|
end
|
30
35
|
|
31
|
-
unless alphabet.is_a?(String) && alphabet.downcase ==
|
32
|
-
base58 =
|
36
|
+
unless alphabet.is_a?(String) && alphabet.downcase == GMP_ALPHABET
|
37
|
+
base58 = from_to(base58, alphabet, GMP_ALPHABET)
|
33
38
|
end
|
34
39
|
|
35
40
|
GMP::Z.new(base58, 58)
|
36
41
|
end
|
37
42
|
|
38
|
-
def self.
|
39
|
-
|
43
|
+
def self.from_to(base58, from_alphabet, to_alphabet)
|
44
|
+
unless base58.is_a?(String)
|
40
45
|
raise ArgumentError, 'Base58 argument is not a string.'
|
41
46
|
end
|
42
|
-
base58 = base58.tr(ALPHABET_GMP, ALPHABET_FLICKR)
|
43
|
-
end
|
44
47
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
+
unless (
|
49
|
+
from_alphabet.is_a?(String) &&
|
50
|
+
from_digits = ALPHABETS[from_alphabet.downcase]
|
51
|
+
)
|
52
|
+
raise ArgumentError, 'From encoding is not valid.'
|
48
53
|
end
|
49
|
-
|
54
|
+
|
55
|
+
unless (
|
56
|
+
to_alphabet.is_a?(String) &&
|
57
|
+
to_digits = ALPHABETS[to_alphabet.downcase]
|
58
|
+
)
|
59
|
+
raise ArgumentError, 'To encoding is not valid.'
|
60
|
+
end
|
61
|
+
|
62
|
+
from_digits != to_digits ?
|
63
|
+
base58.tr(from_digits, to_digits) :
|
64
|
+
base58
|
50
65
|
end
|
51
66
|
|
52
67
|
def self.md5_base58(data, alphabet=DEFAULT_ALPHABET)
|
53
|
-
|
68
|
+
integer_to_base58(Digest::MD5.hexdigest(data).hex, alphabet)
|
54
69
|
end
|
55
70
|
|
56
71
|
class << self
|
57
|
-
alias_method :encode, :
|
58
|
-
alias_method :decode, :
|
72
|
+
alias_method :encode, :integer_to_base58
|
73
|
+
alias_method :decode, :base58_to_integer
|
59
74
|
alias_method :md5, :md5_base58
|
60
75
|
end
|
61
76
|
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'base58_gmp'
|
3
|
+
|
4
|
+
class TestBase58GMP < Test::Unit::TestCase
|
5
|
+
ALPHABETS = {
|
6
|
+
'bitcoin' => '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz',
|
7
|
+
'flickr' => '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ',
|
8
|
+
'gmp' => '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv'
|
9
|
+
}
|
10
|
+
|
11
|
+
def test_alphabets_transcoding
|
12
|
+
assert_equal ALPHABETS['bitcoin'], Base58GMP.from_to(ALPHABETS['flickr'], 'flickr', 'bitcoin')
|
13
|
+
assert_equal ALPHABETS['bitcoin'], Base58GMP.from_to(ALPHABETS['gmp'], 'gmp', 'bitcoin')
|
14
|
+
assert_equal ALPHABETS['flickr'], Base58GMP.from_to(ALPHABETS['bitcoin'], 'bitcoin', 'flickr')
|
15
|
+
assert_equal ALPHABETS['flickr'], Base58GMP.from_to(ALPHABETS['gmp'], 'gmp', 'flickr')
|
16
|
+
assert_equal ALPHABETS['gmp'], Base58GMP.from_to(ALPHABETS['bitcoin'], 'bitcoin', 'gmp')
|
17
|
+
assert_equal ALPHABETS['gmp'], Base58GMP.from_to(ALPHABETS['flickr'], 'flickr', 'gmp')
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_alphabets_digits
|
21
|
+
['gmp'].each { |alphabet|
|
22
|
+
digits = ALPHABETS[alphabet].to_s.split('')
|
23
|
+
(0..57).to_a.each { |decimal|
|
24
|
+
assert_equal digits[decimal].to_s, Base58GMP.encode(decimal,alphabet)
|
25
|
+
assert_equal decimal.to_i, Base58GMP.decode(digits[decimal].to_s, alphabet).to_i
|
26
|
+
}
|
27
|
+
}
|
28
|
+
end
|
29
|
+
end
|
data/test/test_base58_gmp.rb
CHANGED
@@ -506,13 +506,13 @@ INTEGER_EXAMPLES = { "6hKMCS" => 3471391110,
|
|
506
506
|
|
507
507
|
def test_integer_to_base58
|
508
508
|
INTEGER_EXAMPLES.each do |expected, integer|
|
509
|
-
assert_equal expected, Base58GMP.
|
509
|
+
assert_equal expected, Base58GMP.integer_to_base58(integer)
|
510
510
|
end
|
511
511
|
end
|
512
512
|
|
513
513
|
def test_base58_to_integer
|
514
514
|
INTEGER_EXAMPLES.each do |base58, expected|
|
515
|
-
assert_equal expected, Base58GMP.
|
515
|
+
assert_equal expected, Base58GMP.base58_to_integer(base58).to_i
|
516
516
|
end
|
517
517
|
end
|
518
518
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: base58_gmp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 7
|
10
|
+
version: 0.0.7
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- John Wang
|
@@ -15,11 +15,11 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-11-
|
18
|
+
date: 2011-11-21 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
22
|
-
description: Base58 encoding using
|
22
|
+
description: High speed Base58 encoding using GMP with MD5 support and transcoding between Flickr, Bitcoin and GMP alphabets.
|
23
23
|
email: johncwang@gmail.com
|
24
24
|
executables: []
|
25
25
|
|
@@ -37,7 +37,7 @@ files:
|
|
37
37
|
- lib/base58_gmp.rb
|
38
38
|
- test/test_base58_gmp.rb
|
39
39
|
- test/test_encode-decode-multi.rb
|
40
|
-
- test/
|
40
|
+
- test/test_alphabets.rb
|
41
41
|
has_rdoc: true
|
42
42
|
homepage: http://rubygems.org/gems/base58_gmp
|
43
43
|
licenses: []
|
data/test/test_flickr-gmp.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
require 'base58_gmp'
|
3
|
-
|
4
|
-
class TestBase58GMP < Test::Unit::TestCase
|
5
|
-
|
6
|
-
ALPHABETS = {
|
7
|
-
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv' =>
|
8
|
-
'123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
|
9
|
-
}
|
10
|
-
|
11
|
-
def test_flickr_gmp_transcoding
|
12
|
-
ALPHABETS.each do |gmp, flickr|
|
13
|
-
assert_equal gmp, Base58GMP.flickr_to_gmp(flickr)
|
14
|
-
assert_equal flickr, Base58GMP.gmp_to_flickr(gmp)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|