base58_gmp 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ = 0.0.7
2
+ - Add Bitcoin suppport
3
+ - Refactor API
4
+
1
5
  = 0.0.6
2
6
  - Update encode API
3
7
 
@@ -1,6 +1,6 @@
1
1
  = Base58GMP
2
2
 
3
- High speed Base58 encoding using GMP with MD5 support and transcoding between GMP and Flickr implementations.
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 GMP alphabet
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 GMP alphabet
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 Flickr and GMP
37
- Base58GMP.flickr_to_gmp('123456789abcdefghijk') # => 0123456789ABCDEFGHIJ
38
- Base58GMP.gmp_to_flickr('0123456789ABCDEFGHIJ') # => 123456789abcdefghijk
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 implementations used by Flickr and GMP.
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
- The Flickr implementation uses the [0-9a-zA-Z] Base62 alphabet excluding [0OIl] to improve human readability and is described here:
48
+ Flickr Alphabet: [0-9a-zA-Z] excluding [0OIl] to improve human readability
47
49
 
48
- http://www.flickr.com/groups/api/discuss/72157616713786392/
50
+ Bitcoin Alphabet: [0-9A-Za-z] excluding [0OIl] to improve human readability
49
51
 
50
- The GMP implementation uses the [0-9A-Za-v] alphabet.
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.6
1
+ 0.0.7
@@ -1,9 +1,9 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'base58_gmp'
3
- s.version = '0.0.6'
4
- s.date = '2011-11-14'
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 the GNU Multiple Precision Arithmetic Library (GMP)'
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/test_flickr-gmp.rb'
24
+ 'test/test_alphabets.rb'
25
25
  ]
26
26
 
27
27
  end
@@ -9,53 +9,68 @@ require 'gmp'
9
9
 
10
10
  class Base58GMP
11
11
 
12
- ALPHABET_GMP = '0-89A-JK-XYZa-fg-kl-v'
13
- ALPHABET_FLICKR = '1-9ab-km-zABC-HJ-NP-Z'
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.number_to_base58(number, alphabet=DEFAULT_ALPHABET)
17
- base58 = number.is_a?(GMP::Z) ?
18
- number.to_s(base = 58) :
19
- GMP::Z(number).to_s(base = 58)
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 == 'gmp' ?
26
+ alphabet.is_a?(String) && alphabet.downcase == GMP_ALPHABET ?
22
27
  base58 :
23
- self.gmp_to_flickr(base58)
28
+ from_to(base58, GMP_ALPHABET, alphabet)
24
29
  end
25
30
 
26
- def self.base58_to_number(base58, alphabet=DEFAULT_ALPHABET)
27
- if !base58.is_a?(String)
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 == 'gmp'
32
- base58 = self.flickr_to_gmp(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.gmp_to_flickr(base58)
39
- if !base58.is_a?(String)
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
- def self.flickr_to_gmp(base58)
46
- if !base58.is_a?(String)
47
- raise ArgumentError, 'Base58 argument is not a string.'
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
- base58 = base58.tr(ALPHABET_FLICKR, ALPHABET_GMP)
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
- self.number_to_base58(Digest::MD5.hexdigest(data).hex, alphabet)
68
+ integer_to_base58(Digest::MD5.hexdigest(data).hex, alphabet)
54
69
  end
55
70
 
56
71
  class << self
57
- alias_method :encode, :number_to_base58
58
- alias_method :decode, :base58_to_number
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
@@ -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.number_to_base58(integer)
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.base58_to_number(base58).to_i
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: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
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-14 00:00:00 -08:00
18
+ date: 2011-11-21 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
22
- description: Base58 encoding using the GNU Multiple Precision Arithmetic Library (GMP)
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/test_flickr-gmp.rb
40
+ - test/test_alphabets.rb
41
41
  has_rdoc: true
42
42
  homepage: http://rubygems.org/gems/base58_gmp
43
43
  licenses: []
@@ -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