base32_pure 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +43 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/base32/crockford.rb +88 -0
- data/lib/base32_pure.rb +2 -0
- data/spec/base32_pure_spec.rb +61 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +94 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Rune Myrland
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
= base32_pure
|
2
|
+
|
3
|
+
Encodes and decodes binary tokens to and from base32.
|
4
|
+
|
5
|
+
== Description
|
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.
|
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
|
16
|
+
|
17
|
+
== Usage
|
18
|
+
|
19
|
+
require 'base32_pure'
|
20
|
+
|
21
|
+
encoded = Base32::Crockford.encode("Some token")
|
22
|
+
Base32::Crockford.decode(encoded)
|
23
|
+
|
24
|
+
hypenated = Base32::Crockford.hypenate(encoded)
|
25
|
+
Base32::Crockford.decode(hypenated)
|
26
|
+
|
27
|
+
== Features/problems
|
28
|
+
|
29
|
+
The optional check value is not implemented.
|
30
|
+
|
31
|
+
== Note on Patches/Pull Requests
|
32
|
+
|
33
|
+
* Fork the project.
|
34
|
+
* Make your feature addition or bug fix.
|
35
|
+
* Add tests for it. This is important so I don't break it in a
|
36
|
+
future version unintentionally.
|
37
|
+
* Commit, do not mess with rakefile, version, or history.
|
38
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
39
|
+
* Send me a pull request. Bonus points for topic branches.
|
40
|
+
|
41
|
+
== Copyright
|
42
|
+
|
43
|
+
Copyright (c) 2010 Rune Myrland. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "base32_pure"
|
8
|
+
gem.summary = %Q{Encode and decode tokens to and from base32.}
|
9
|
+
gem.description = %Q{Pure ruby libary. Using the crockford alphabeth}
|
10
|
+
gem.email = "rune@epubify.com"
|
11
|
+
gem.homepage = "http://github.com/wrimle/base32_pure"
|
12
|
+
gem.authors = ["Rune Myrland"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "base32_pure #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
@@ -0,0 +1,88 @@
|
|
1
|
+
|
2
|
+
module Base32
|
3
|
+
|
4
|
+
class Crockford
|
5
|
+
ENCODE = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
|
6
|
+
DECODE = {
|
7
|
+
'0' => 0,
|
8
|
+
'1' => 1,
|
9
|
+
'2' => 2,
|
10
|
+
'3' => 3,
|
11
|
+
'4' => 4,
|
12
|
+
'5' => 5,
|
13
|
+
'6' => 6,
|
14
|
+
'7' => 7,
|
15
|
+
'8' => 8,
|
16
|
+
'9' => 9,
|
17
|
+
'A' => 10,
|
18
|
+
'B' => 11,
|
19
|
+
'C' => 12,
|
20
|
+
'D' => 13,
|
21
|
+
'E' => 14,
|
22
|
+
'F' => 15,
|
23
|
+
'G' => 16,
|
24
|
+
'H' => 17,
|
25
|
+
'J' => 18,
|
26
|
+
'K' => 19,
|
27
|
+
'M' => 20,
|
28
|
+
'N' => 21,
|
29
|
+
'P' => 22,
|
30
|
+
'Q' => 23,
|
31
|
+
'R' => 24,
|
32
|
+
'S' => 25,
|
33
|
+
'T' => 26,
|
34
|
+
'V' => 27,
|
35
|
+
'W' => 28,
|
36
|
+
'X' => 29,
|
37
|
+
'Y' => 30,
|
38
|
+
'Z' => 31,
|
39
|
+
}
|
40
|
+
|
41
|
+
def self.encode v
|
42
|
+
normalized = v.reverse
|
43
|
+
n = 0
|
44
|
+
bits = 0
|
45
|
+
res = ""
|
46
|
+
normalized.each_byte do |b|
|
47
|
+
n += b << bits
|
48
|
+
bits += 8
|
49
|
+
|
50
|
+
while bits >= 5 do
|
51
|
+
res << ENCODE[ n & 31 ]
|
52
|
+
n >>= 5
|
53
|
+
bits -= 5
|
54
|
+
end
|
55
|
+
end
|
56
|
+
res << ENCODE[ n & 31 ] if n != 0
|
57
|
+
res << 0 if res.empty?
|
58
|
+
|
59
|
+
res.reverse
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.hypenate v
|
63
|
+
v.reverse.gsub(/(\w\w\w\w\w)/, "\\1-").gsub(/-$/, "").downcase.reverse
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.decode v
|
67
|
+
normalized = v.upcase.tr("OILU", "011").tr("-", "").reverse
|
68
|
+
|
69
|
+
n = 0
|
70
|
+
bits = 0
|
71
|
+
res = ""
|
72
|
+
normalized.each_byte do |b|
|
73
|
+
n += (DECODE[ b.chr ] << bits)
|
74
|
+
bits += 5
|
75
|
+
|
76
|
+
while bits >= 8 do
|
77
|
+
res << (n & 255).chr
|
78
|
+
n >>= 8
|
79
|
+
bits -= 8
|
80
|
+
end
|
81
|
+
end
|
82
|
+
res << n if n != 0
|
83
|
+
res << 0 if res.empty?
|
84
|
+
|
85
|
+
res.reverse
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/lib/base32_pure.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
|
+
|
4
|
+
|
5
|
+
describe "Base32" do
|
6
|
+
include Base32
|
7
|
+
|
8
|
+
context "Crockford" do
|
9
|
+
it "encodes a string to base 32" do
|
10
|
+
encoded = Base32::Crockford.encode("A")
|
11
|
+
encoded.should == "21"
|
12
|
+
|
13
|
+
puts Base32::Crockford.hypenate(Base32::Crockford.encode("a test"))
|
14
|
+
end
|
15
|
+
|
16
|
+
it "decodes a string from base 32" do
|
17
|
+
string = "21"
|
18
|
+
decoded = Base32::Crockford.decode(string)
|
19
|
+
decoded.should == "A"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "is not case sensitive" do
|
23
|
+
string = "21abdevp58"
|
24
|
+
decoded_downcase = Base32::Crockford.decode(string)
|
25
|
+
decoded_upcase = Base32::Crockford.decode(string.upcase)
|
26
|
+
decoded_downcase.should == decoded_upcase
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
it "treats 1 and l as the same value" do
|
31
|
+
string = "21abdevp58"
|
32
|
+
decoded_reference = Base32::Crockford.decode(string)
|
33
|
+
decoded_modified = Base32::Crockford.decode(string.tr("1", "l"))
|
34
|
+
decoded_reference.should == decoded_modified
|
35
|
+
end
|
36
|
+
|
37
|
+
it "treats 1 and I as the same value" do
|
38
|
+
string = "21abdevp58"
|
39
|
+
decoded_reference = Base32::Crockford.decode(string)
|
40
|
+
decoded_modified = Base32::Crockford.decode(string.tr("1", "I"))
|
41
|
+
decoded_reference.should == decoded_modified
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
it "treats 0 and O as the same value" do
|
46
|
+
string = "21ab0devp58"
|
47
|
+
decoded_reference = Base32::Crockford.decode(string)
|
48
|
+
decoded_modified = Base32::Crockford.decode(string.tr("0", "O"))
|
49
|
+
decoded_reference.should == decoded_modified
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
it "allows hypenation" do
|
54
|
+
string = "21ab0devsdagjølp58"
|
55
|
+
encoded = Base32::Crockford.hypenate(Base32::Crockford.encode(string))
|
56
|
+
decoded = Base32::Crockford.decode(encoded)
|
57
|
+
|
58
|
+
decoded.should == string
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: base32_pure
|
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
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Rune Myrland
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-08-18 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 13
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 2
|
33
|
+
- 9
|
34
|
+
version: 1.2.9
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
description: Pure ruby libary. Using the crockford alphabeth
|
38
|
+
email: rune@epubify.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- LICENSE
|
45
|
+
- README.rdoc
|
46
|
+
files:
|
47
|
+
- .document
|
48
|
+
- .gitignore
|
49
|
+
- LICENSE
|
50
|
+
- README.rdoc
|
51
|
+
- Rakefile
|
52
|
+
- VERSION
|
53
|
+
- lib/base32/crockford.rb
|
54
|
+
- lib/base32_pure.rb
|
55
|
+
- spec/base32_pure_spec.rb
|
56
|
+
- spec/spec.opts
|
57
|
+
- spec/spec_helper.rb
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: http://github.com/wrimle/base32_pure
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options:
|
64
|
+
- --charset=UTF-8
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
hash: 3
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: "0"
|
85
|
+
requirements: []
|
86
|
+
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.3.7
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: Encode and decode tokens to and from base32.
|
92
|
+
test_files:
|
93
|
+
- spec/base32_pure_spec.rb
|
94
|
+
- spec/spec_helper.rb
|