base32_pure 0.0.0 → 0.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.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+
3
+ group :development do
4
+ gem 'jeweler'
5
+ gem 'rspec', '<2.0'
6
+ end
data/README.rdoc CHANGED
@@ -1,18 +1,16 @@
1
1
  = base32_pure
2
2
 
3
- Encodes and decodes binary tokens to and from base32.
3
+ Encodes and decodes binary string or integer tokens to and from base32.
4
4
 
5
5
  == Description
6
6
 
7
- It uses the Crockford alphabet defined defined at http://www.crockford.com/wrmg/base32.html
8
- - which is probably not the most efficient.
7
+ It uses the Crockford alphabet defined at http://www.crockford.com/wrmg/base32.html -
8
+ which is probably not the most efficient, but it is tolerant to user mistakes.
9
9
 
10
- It is quite tolerant to user mistakes.
11
-
12
- * It's case insensitive
13
- * I,l and 1 maps to the same value
14
- * 0 and O maps to the same value
15
- * U is excluded to reduce the likelyhood of accidental obscenity
10
+ * It's case insensitive
11
+ * I, l and 1 maps to the same value
12
+ * 0 and O maps to the same value
13
+ * U is excluded to reduce the likelyhood of accidental obscenity
16
14
 
17
15
  == Usage
18
16
 
@@ -24,12 +22,24 @@ It is quite tolerant to user mistakes.
24
22
  hypenated = Base32::Crockford.hypenate(encoded)
25
23
  Base32::Crockford.decode(hypenated)
26
24
 
25
+ # Fixnum and Bignum should work as well
26
+ encoded = Base32::Crockford.encode(12345)
27
+ Base32::Crockford.decode(encoded, :integer)
28
+
29
+
30
+ # Typical use case
31
+ require 'uuidtools'
32
+
33
+ digest = UUIDTools::UUID.random_create.raw
34
+ base32_token = Base32::Crockford.encode(digest)
35
+
36
+
27
37
  == Features/problems
28
38
 
29
39
  The optional check value is not implemented.
30
40
 
31
41
  == Note on Patches/Pull Requests
32
-
42
+
33
43
  * Fork the project.
34
44
  * Make your feature addition or bug fix.
35
45
  * Add tests for it. This is important so I don't break it in a
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ begin
6
6
  Jeweler::Tasks.new do |gem|
7
7
  gem.name = "base32_pure"
8
8
  gem.summary = %Q{Encode and decode tokens to and from base32.}
9
- gem.description = %Q{Pure ruby libary. Using the crockford alphabeth}
9
+ gem.description = %Q{Pure ruby libary. Using the crockford alphabet.}
10
10
  gem.email = "rune@epubify.com"
11
11
  gem.homepage = "http://github.com/wrimle/base32_pure"
12
12
  gem.authors = ["Rune Myrland"]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.1.0
@@ -1,6 +1,23 @@
1
+ # Extend fixnum with an each_byte method
1
2
 
2
3
  module Base32
3
4
 
5
+ class NormalizedInteger
6
+ def initialize v
7
+ @value = v
8
+ end
9
+
10
+
11
+ def each_byte &blk
12
+ v = @value
13
+ while v > 0
14
+ yield (v & 255)
15
+ v >>= 8
16
+ end
17
+ end
18
+ end
19
+
20
+
4
21
  class Crockford
5
22
  ENCODE = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
6
23
  DECODE = {
@@ -38,8 +55,14 @@ module Base32
38
55
  'Z' => 31,
39
56
  }
40
57
 
58
+
41
59
  def self.encode v
42
- normalized = v.reverse
60
+ if v.respond_to?(:integer?) && v.integer?
61
+ normalized = NormalizedInteger.new(v)
62
+ else
63
+ normalized = v.reverse
64
+ end
65
+
43
66
  n = 0
44
67
  bits = 0
45
68
  res = ""
@@ -63,12 +86,13 @@ module Base32
63
86
  v.reverse.gsub(/(\w\w\w\w\w)/, "\\1-").gsub(/-$/, "").downcase.reverse
64
87
  end
65
88
 
66
- def self.decode v
89
+ def self.decode v, to = :binary_string
67
90
  normalized = v.upcase.tr("OILU", "011").tr("-", "").reverse
68
91
 
69
92
  n = 0
70
93
  bits = 0
71
94
  res = ""
95
+ res.force_encoding("binary") if res.respond_to?(:force_encoding)
72
96
  normalized.each_byte do |b|
73
97
  n += (DECODE[ b.chr ] << bits)
74
98
  bits += 5
@@ -81,8 +105,19 @@ module Base32
81
105
  end
82
106
  res << n if n != 0
83
107
  res << 0 if res.empty?
108
+ res = res.reverse
84
109
 
85
- res.reverse
110
+ case to
111
+ when :integer
112
+ v = 0
113
+ res.each_byte do |byte|
114
+ v <<= 8
115
+ v |= byte
116
+ end
117
+ v
118
+ else
119
+ res
120
+ end
86
121
  end
87
122
  end
88
123
  end
@@ -6,11 +6,19 @@ describe "Base32" do
6
6
  include Base32
7
7
 
8
8
  context "Crockford" do
9
+
9
10
  it "encodes a string to base 32" do
10
11
  encoded = Base32::Crockford.encode("A")
11
12
  encoded.should == "21"
13
+ end
14
+
15
+ it "encodes a fixnum to base 32" do
16
+ encoded = Base32::Crockford.encode(12)
17
+ encoded.should == "C9J"
18
+ end
12
19
 
13
- puts Base32::Crockford.hypenate(Base32::Crockford.encode("a test"))
20
+ it "encodes a fixnum the same as the equivalent string" do
21
+ Base32::Crockford.encode(12).should == Base32::Crockford.encode("12")
14
22
  end
15
23
 
16
24
  it "decodes a string from base 32" do
@@ -52,6 +60,7 @@ describe "Base32" do
52
60
 
53
61
  it "allows hypenation" do
54
62
  string = "21ab0devsdagjølp58"
63
+ string.force_encoding("binary") if string.respond_to?(:force_encoding)
55
64
  encoded = Base32::Crockford.hypenate(Base32::Crockford.encode(string))
56
65
  decoded = Base32::Crockford.decode(encoded)
57
66
 
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: base32_pure
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 0
9
- - 0
10
- version: 0.0.0
4
+ prerelease:
5
+ version: 0.1.0
11
6
  platform: ruby
12
7
  authors:
13
8
  - Rune Myrland
@@ -15,26 +10,43 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2010-08-18 00:00:00 +02:00
13
+ date: 2011-05-10 00:00:00 +02:00
19
14
  default_executable:
20
15
  dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: jeweler
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: *id001
21
27
  - !ruby/object:Gem::Dependency
22
28
  name: rspec
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - <
33
+ - !ruby/object:Gem::Version
34
+ version: "2.0"
35
+ type: :development
23
36
  prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec
40
+ requirement: &id003 !ruby/object:Gem::Requirement
25
41
  none: false
26
42
  requirements:
27
43
  - - ">="
28
44
  - !ruby/object:Gem::Version
29
- hash: 13
30
- segments:
31
- - 1
32
- - 2
33
- - 9
34
45
  version: 1.2.9
35
46
  type: :development
36
- version_requirements: *id001
37
- description: Pure ruby libary. Using the crockford alphabeth
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ description: Pure ruby libary. Using the crockford alphabet.
38
50
  email: rune@epubify.com
39
51
  executables: []
40
52
 
@@ -45,7 +57,7 @@ extra_rdoc_files:
45
57
  - README.rdoc
46
58
  files:
47
59
  - .document
48
- - .gitignore
60
+ - Gemfile
49
61
  - LICENSE
50
62
  - README.rdoc
51
63
  - Rakefile
@@ -60,8 +72,8 @@ homepage: http://github.com/wrimle/base32_pure
60
72
  licenses: []
61
73
 
62
74
  post_install_message:
63
- rdoc_options:
64
- - --charset=UTF-8
75
+ rdoc_options: []
76
+
65
77
  require_paths:
66
78
  - lib
67
79
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -69,26 +81,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
69
81
  requirements:
70
82
  - - ">="
71
83
  - !ruby/object:Gem::Version
72
- hash: 3
73
- segments:
74
- - 0
75
84
  version: "0"
76
85
  required_rubygems_version: !ruby/object:Gem::Requirement
77
86
  none: false
78
87
  requirements:
79
88
  - - ">="
80
89
  - !ruby/object:Gem::Version
81
- hash: 3
82
- segments:
83
- - 0
84
90
  version: "0"
85
91
  requirements: []
86
92
 
87
93
  rubyforge_project:
88
- rubygems_version: 1.3.7
94
+ rubygems_version: 1.6.2
89
95
  signing_key:
90
96
  specification_version: 3
91
97
  summary: Encode and decode tokens to and from base32.
92
- test_files:
93
- - spec/base32_pure_spec.rb
94
- - spec/spec_helper.rb
98
+ test_files: []
99
+
data/.gitignore DELETED
@@ -1,21 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
-
21
- ## PROJECT::SPECIFIC