base32 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ ext/Makefile
2
+ ext/*.o
3
+ ext/*.so
4
+ ext/*.bundle
5
+ Gemfile.lock
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - ruby-head
8
+ - rbx-19mode
@@ -0,0 +1,14 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.3.0
4
+
5
+ - Add `Base32::random_base32` function
6
+ - Add test cases for hexadecimal conversion
7
+ - Upgrade tests to use Minitest (successor to Test::Unit)
8
+ - Add Travis and CodeClimate badges to README
9
+ - Add Gemfile and gemspec files, using `gem-release` gem to release
10
+ - Track version as a Ruby constant
11
+
12
+ ## 0.2.0 and prior
13
+
14
+ - Support Base32::encode/decode methods
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2007-2011 Samuel Tesla
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README CHANGED
@@ -1,8 +1,11 @@
1
1
  = base32
2
2
 
3
+ {<img src="https://travis-ci.org/stesla/base32.png" />}[https://travis-ci.org/stesla/base32]
4
+ {<img src="https://codeclimate.com/github/stesla/base32.png" />}[https://codeclimate.com/github/stesla/base32]
5
+
3
6
  For Version: 0.1.3
4
7
 
5
- This package contains base32, a ruby extension for encoding and decoding
8
+ This package contains base32, a Ruby extension for encoding and decoding
6
9
  in base32 per RFC 3548.
7
10
 
8
11
  == Download
data/Rakefile CHANGED
@@ -1,53 +1,19 @@
1
- # Copyright (c) 2007-2011 Samuel Tesla
2
-
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
-
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
-
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
1
+ begin
2
+ require 'bundler'
3
+ Bundler.setup(:default, :development)
4
+ rescue Bundler::BundlerError => e
5
+ $stderr.puts e.message
6
+ $stderr.puts 'Run `bundle install` to install missing gems'
7
+ exit e.status_code
8
+ end
20
9
 
21
- require File.join(File.dirname(__FILE__), 'config', 'environment.rb')
22
- require 'rake/clean'
23
- require 'rake/gempackagetask'
24
- require 'rake/testtask'
10
+ require 'bundler/gem_tasks'
25
11
  require 'rubygems'
12
+ require 'rake/testtask'
26
13
 
27
- task :default => ['test:all']
28
-
29
- gemspec = Gem::Specification.new do |s|
30
- s.author = "Samuel Tesla"
31
- s.email = "samuel.tesla@gmail.com"
32
- s.extra_rdoc_files = ["README"]
33
- s.files = FileList["Rakefile", "{config,lib,test}/**/*"]
34
- s.has_rdoc = true
35
- s.name = 'base32'
36
- s.require_paths << 'lib'
37
- s.requirements << 'none'
38
- s.summary = "Ruby extension for base32 encoding and decoding"
39
- s.version = "0.2.0"
40
- end
41
-
42
- Rake::GemPackageTask.new(gemspec) do |pkg|
43
- pkg.need_tar = true
44
- end
14
+ task :default => :test
45
15
 
46
- namespace :test do
47
- Rake::TestTask.new :all do |t|
48
- t.libs << 'test'
49
- t.pattern = 'test/**/*_test.rb'
50
- t.verbose = true
51
- end
52
- Rake::Task['test:all'].comment = 'Run all of the tests'
16
+ Rake::TestTask.new do |t|
17
+ t.libs << 'test'
18
+ t.test_files = FileList['test/**/*_test.rb']
53
19
  end
@@ -0,0 +1,24 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+ require 'version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'base32'
6
+ s.version = Base32::VERSION
7
+ s.authors = ['Samuel Tesla']
8
+ s.email = 'samuel.tesla@gmail.com'
9
+ s.summary = 'Ruby extension for base32 encoding and decoding'
10
+
11
+ s.files = `git ls-files`.split($/)
12
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
13
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
14
+ s.require_paths = ['lib']
15
+
16
+ s.has_rdoc = true
17
+ s.extra_rdoc_files = ['README']
18
+
19
+ s.post_install_message = File.read('UPGRADING') if File.exists?('UPGRADING')
20
+
21
+ s.add_development_dependency 'rake'
22
+ s.add_development_dependency 'minitest'
23
+ s.add_development_dependency 'gem-release'
24
+ end
@@ -1,5 +1,8 @@
1
+ require 'openssl'
2
+
3
+ # Module for encoding and decoding in Base32 per RFC 3548
1
4
  module Base32
2
- TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
5
+ TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.freeze
3
6
 
4
7
  class Chunk
5
8
  def initialize(bytes)
@@ -10,15 +13,15 @@ module Base32
10
13
  bytes = @bytes.take_while {|c| c != 61} # strip padding
11
14
  n = (bytes.length * 5.0 / 8.0).floor
12
15
  p = bytes.length < 8 ? 5 - (n * 8) % 5 : 0
13
- c = bytes.inject(0) {|m,o| (m << 5) + TABLE.index(o.chr)} >> p
16
+ c = bytes.inject(0) {|m,o| (m << 5) + Base32.table.index(o.chr)} >> p
14
17
  (0..n-1).to_a.reverse.collect {|i| ((c >> i * 8) & 0xff).chr}
15
18
  end
16
-
19
+
17
20
  def encode
18
21
  n = (@bytes.length * 8.0 / 5.0).ceil
19
22
  p = n < 8 ? 5 - (@bytes.length * 8) % 5 : 0
20
23
  c = @bytes.inject(0) {|m,o| (m << 8) + o} << p
21
- [(0..n-1).to_a.reverse.collect {|i| TABLE[(c >> i * 5) & 0x1f].chr},
24
+ [(0..n-1).to_a.reverse.collect {|i| Base32.table[(c >> i * 5) & 0x1f].chr},
22
25
  ("=" * (8-n))]
23
26
  end
24
27
  end
@@ -32,7 +35,7 @@ module Base32
32
35
  end
33
36
  result
34
37
  end
35
-
38
+
36
39
  def self.encode(str)
37
40
  chunks(str, 5).collect(&:encode).flatten.join
38
41
  end
@@ -40,4 +43,25 @@ module Base32
40
43
  def self.decode(str)
41
44
  chunks(str, 8).collect(&:decode).flatten.join
42
45
  end
46
+
47
+ def self.random_base32(length=16)
48
+ random = ''
49
+ OpenSSL::Random.random_bytes(length).each_byte do |b|
50
+ random << self.table[b % 32]
51
+ end
52
+ random.ljust((length / 8.0).ceil * 8, '=') # add padding
53
+ end
54
+
55
+ def self.table=(table)
56
+ raise ArgumentError, "Table must have 32 unique characters" unless self.table_valid?(table)
57
+ @table = table
58
+ end
59
+
60
+ def self.table
61
+ @table || TABLE
62
+ end
63
+
64
+ def self.table_valid?(table)
65
+ table.bytes.size == 32 && table.bytes.uniq.size == 32
66
+ end
43
67
  end
@@ -0,0 +1,3 @@
1
+ module Base32
2
+ VERSION = '0.3.0'
3
+ end
@@ -1,27 +1,8 @@
1
- # Copyright (c) 2007-2011 Samuel Tesla
2
-
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
-
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
-
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
20
-
21
- require 'test/unit'
22
- require 'base32'
23
-
24
- class TestBase32 < Test::Unit::TestCase
1
+ gem 'minitest'
2
+ require 'minitest/autorun'
3
+ require File.dirname(__FILE__) + '/../lib/base32.rb'
4
+
5
+ class TestBase32 < Minitest::Test
25
6
  def assert_decoding(encoded, plain)
26
7
  decoded = Base32.decode(encoded)
27
8
  assert_equal(plain, decoded)
@@ -37,6 +18,11 @@ class TestBase32 < Test::Unit::TestCase
37
18
  assert_decoding(encoded, plain)
38
19
  end
39
20
 
21
+ def assert_hex_encode_and_decode(encoded, hex)
22
+ plain = [hex].pack('H*')
23
+ assert_encode_and_decode(encoded, plain)
24
+ end
25
+
40
26
  def test_empty_string
41
27
  assert_encode_and_decode('', '')
42
28
  end
@@ -72,4 +58,59 @@ class TestBase32 < Test::Unit::TestCase
72
58
  N5XAUIBAEAQCAIDGN5ZCA5DIMUQFK3TJORSWIICTORQXIZLTEBXWMICBNVSXE2LDMEXAU===).join
73
59
  assert_encode_and_decode(encoded, plaintext)
74
60
  end
61
+
62
+ def test_hex_byte_encoding
63
+ assert_hex_encode_and_decode('FA======', '28')
64
+ assert_hex_encode_and_decode('2Y======', 'd6')
65
+ assert_hex_encode_and_decode('234A====', 'd6f8')
66
+ assert_hex_encode_and_decode('234AA===', 'd6f800')
67
+ assert_hex_encode_and_decode('234BA===', 'd6f810')
68
+ assert_hex_encode_and_decode('234BCDA=', 'd6f8110c')
69
+ assert_hex_encode_and_decode('234BCDEA', 'd6f8110c80')
70
+ assert_hex_encode_and_decode('234BCDEFGA======', 'd6f8110c8530')
71
+ assert_hex_encode_and_decode('234BCDEFG234BCDEFE======', 'd6f8110c8536b7c0886429')
72
+ end
73
+
74
+ def test_random_base32
75
+ assert_equal(16, Base32.random_base32.length)
76
+ assert_match(/^[A-Z2-7]+$/, Base32.random_base32)
77
+ end
78
+
79
+ def test_random_base32_length
80
+ assert_equal(32, Base32.random_base32(32).length)
81
+ assert_equal(40, Base32.random_base32(40).length)
82
+ assert_equal(32, Base32.random_base32(29).length)
83
+ assert_match(/^[A-Z2-7]{1}={7}$/, Base32.random_base32(1))
84
+ assert_match(/^[A-Z2-7]{29}={3}$/, Base32.random_base32(29))
85
+ end
86
+
87
+ def test_assign_new_table
88
+ new_table = 'abcdefghijklmnopqrstuvwxyz234567'
89
+ Base32.table = new_table
90
+ assert_equal(new_table, Base32.table)
91
+ Base32.table = Base32::TABLE # so as not to ruin other tests
92
+ end
93
+
94
+ def test_check_table_length
95
+ assert_raises(ArgumentError) { Base32.table = ('a' * 31) }
96
+ assert_raises(ArgumentError) { Base32.table = ('a' * 32) }
97
+ assert_raises(ArgumentError) { Base32.table = ('a' * 33) }
98
+ assert_raises(ArgumentError) { Base32.table = ('abcdefghijklmnopqrstuvwxyz234567' * 2) }
99
+ Base32.table = Base32::TABLE # so as not to ruin other tests
100
+ end
101
+
102
+ def test_encode_decode_with_alternate_table
103
+ Base32.table = 'abcdefghijklmnopqrstuvwxyz234567'
104
+ assert_hex_encode_and_decode('fa======', '28')
105
+ assert_hex_encode_and_decode('2y======', 'd6')
106
+ assert_hex_encode_and_decode('234a====', 'd6f8')
107
+ assert_hex_encode_and_decode('234aa===', 'd6f800')
108
+ assert_hex_encode_and_decode('234ba===', 'd6f810')
109
+ assert_hex_encode_and_decode('234bcda=', 'd6f8110c')
110
+ assert_hex_encode_and_decode('234bcdea', 'd6f8110c80')
111
+ assert_hex_encode_and_decode('234bcdefga======', 'd6f8110c8530')
112
+ assert_hex_encode_and_decode('234bcdefg234bcdefe======', 'd6f8110c8536b7c0886429')
113
+ Base32.table = Base32::TABLE # so as not to ruin other tests
114
+ end
115
+
75
116
  end
metadata CHANGED
@@ -1,60 +1,106 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: base32
3
- version: !ruby/object:Gem::Version
4
- version: 0.2.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ prerelease:
5
6
  platform: ruby
6
- authors:
7
+ authors:
7
8
  - Samuel Tesla
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
-
12
- date: 2011-08-12 00:00:00 -05:00
13
- default_executable:
14
- dependencies: []
15
-
12
+ date: 2014-01-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: minitest
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: gem-release
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
16
62
  description:
17
63
  email: samuel.tesla@gmail.com
18
64
  executables: []
19
-
20
65
  extensions: []
21
-
22
- extra_rdoc_files:
66
+ extra_rdoc_files:
67
+ - README
68
+ files:
69
+ - .gitignore
70
+ - .travis.yml
71
+ - CHANGELOG.md
72
+ - Gemfile
73
+ - LICENSE
23
74
  - README
24
- files:
25
75
  - Rakefile
76
+ - base32.gemspec
26
77
  - config/environment.rb
27
78
  - lib/base32.rb
79
+ - lib/version.rb
28
80
  - test/base32_test.rb
29
- - README
30
- has_rdoc: true
31
81
  homepage:
32
82
  licenses: []
33
-
34
83
  post_install_message:
35
84
  rdoc_options: []
36
-
37
- require_paths:
85
+ require_paths:
38
86
  - lib
39
- - lib
40
- required_ruby_version: !ruby/object:Gem::Requirement
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- version: "0"
45
- version:
46
- required_rubygems_version: !ruby/object:Gem::Requirement
47
- requirements:
48
- - - ">="
49
- - !ruby/object:Gem::Version
50
- version: "0"
51
- version:
52
- requirements:
53
- - none
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
54
100
  rubyforge_project:
55
- rubygems_version: 1.3.5
101
+ rubygems_version: 1.8.23
56
102
  signing_key:
57
103
  specification_version: 3
58
104
  summary: Ruby extension for base32 encoding and decoding
59
- test_files: []
60
-
105
+ test_files:
106
+ - test/base32_test.rb