base32-0.3.4/0000755000004100000410000000000013765057065012643 5ustar www-datawww-database32-0.3.4/.travis.yml0000644000004100000410000000013013765057065014746 0ustar www-datawww-datalanguage: ruby rvm: - 1.8.7 - 1.9.2 - 1.9.3 - 2.0.0 - ruby-head - rbx-19modebase32-0.3.4/test/0000755000004100000410000000000013765057065013622 5ustar www-datawww-database32-0.3.4/test/base32_test.rb0000644000004100000410000001121013765057065016260 0ustar www-datawww-datagem 'minitest' require 'minitest/autorun' require File.dirname(__FILE__) + '/../lib/base32.rb' class TestBase32 < Minitest::Test def assert_decoding(encoded, plain) decoded = Base32.decode(encoded) assert_equal(plain, decoded) end def assert_encoding(encoded, plain) actual = Base32.encode(plain) assert_equal(encoded, actual) end def assert_encode_and_decode(encoded, plain) assert_encoding(encoded, plain) assert_decoding(encoded, plain) end def assert_hex_encode_and_decode(encoded, hex) plain = [hex].pack('H*') assert_encode_and_decode(encoded, plain) end def test_empty_string assert_encode_and_decode('', '') end def test_a assert_encode_and_decode('ME======', 'a') end def test_12345 assert_encode_and_decode('GEZDGNBV', '12345') end def test_abcde assert_encode_and_decode('MFRGGZDF', 'abcde') end def test_constitution_preamble plaintext =<<-EOT We the people of the United States, in order to form a more perfect union, establish justice, insure domestic tranquility, provide for the common defense, promote the general welfare, and secure the blessings of liberty to ourselves and our posterity, do ordain and establish this Constitution for the United States of America. EOT encoded = %W( EAQCAIBAEBLWKIDUNBSSA4DFN5YGYZJAN5TCA5DIMUQFK3TJORSWIICTORQXIZLTFQQGS3RA N5ZGIZLSEB2G6IDGN5ZG2IDBEBWW64TFEBYGK4TGMVRXIIDVNZUW63RMBIQCAIBAEAQGK43U MFRGY2LTNAQGU5LTORUWGZJMEBUW443VOJSSAZDPNVSXG5DJMMQHI4TBNZYXK2LMNF2HSLBA OBZG65TJMRSSAZTPOIQHI2DFEBRW63LNN5XAUIBAEAQCAIDEMVTGK3TTMUWCA4DSN5WW65DF EB2GQZJAM5SW4ZLSMFWCA53FNRTGC4TFFQQGC3TEEBZWKY3VOJSSA5DIMUQGE3DFONZWS3TH OMQG6ZRANRUWEZLSOR4QUIBAEAQCAIDUN4QG65LSONSWY5TFOMQGC3TEEBXXK4RAOBXXG5DF OJUXI6JMEBSG6IDPOJSGC2LOEBQW4ZBAMVZXIYLCNRUXG2BAORUGS4ZAINXW443UNF2HK5DJ N5XAUIBAEAQCAIDGN5ZCA5DIMUQFK3TJORSWIICTORQXIZLTEBXWMICBNVSXE2LDMEXAU===).join assert_encode_and_decode(encoded, plaintext) end def test_hex_byte_encoding assert_hex_encode_and_decode('FA======', '28') assert_hex_encode_and_decode('2Y======', 'd6') assert_hex_encode_and_decode('234A====', 'd6f8') assert_hex_encode_and_decode('234AA===', 'd6f800') assert_hex_encode_and_decode('234BA===', 'd6f810') assert_hex_encode_and_decode('234BCDA=', 'd6f8110c') assert_hex_encode_and_decode('234BCDEA', 'd6f8110c80') assert_hex_encode_and_decode('234BCDEFGA======', 'd6f8110c8530') assert_hex_encode_and_decode('234BCDEFG234BCDEFE======', 'd6f8110c8536b7c0886429') end def test_random_base32 assert_equal(16, Base32.random_base32.length) assert_match(/^[A-Z2-7]+$/, Base32.random_base32) end def test_random_base32_length assert_equal(32, Base32.random_base32(32).length) assert_equal(40, Base32.random_base32(40).length) assert_equal(32, Base32.random_base32(29).length) assert_match(/^[A-Z2-7]{1}={7}$/, Base32.random_base32(1)) assert_match(/^[A-Z2-7]{29}={3}$/, Base32.random_base32(29)) end def test_random_base32_padding assert_equal(32, Base32.random_base32(32, false).length) assert_equal(40, Base32.random_base32(40, false).length) assert_equal(29, Base32.random_base32(29, false).length) assert_match(/^[A-Z2-7]{1}$/, Base32.random_base32(1, false)) assert_match(/^[A-Z2-7]{29}$/, Base32.random_base32(29, false)) end def test_assign_new_table new_table = 'abcdefghijklmnopqrstuvwxyz234567' Base32.table = new_table assert_equal(new_table, Base32.table) Base32.table = Base32::TABLE # so as not to ruin other tests end def test_check_table_length assert_raises(ArgumentError) { Base32.table = ('a' * 31) } assert_raises(ArgumentError) { Base32.table = ('a' * 32) } assert_raises(ArgumentError) { Base32.table = ('a' * 33) } assert_raises(ArgumentError) { Base32.table = ('abcdefghijklmnopqrstuvwxyz234567' * 2) } Base32.table = Base32::TABLE # so as not to ruin other tests end def test_encode_decode_with_alternate_table Base32.table = 'abcdefghijklmnopqrstuvwxyz234567' assert_hex_encode_and_decode('fa======', '28') assert_hex_encode_and_decode('2y======', 'd6') assert_hex_encode_and_decode('234a====', 'd6f8') assert_hex_encode_and_decode('234aa===', 'd6f800') assert_hex_encode_and_decode('234ba===', 'd6f810') assert_hex_encode_and_decode('234bcda=', 'd6f8110c') assert_hex_encode_and_decode('234bcdea', 'd6f8110c80') assert_hex_encode_and_decode('234bcdefga======', 'd6f8110c8530') assert_hex_encode_and_decode('234bcdefg234bcdefe======', 'd6f8110c8536b7c0886429') Base32.table = Base32::TABLE # so as not to ruin other tests end end base32-0.3.4/README.md0000644000004100000410000000166113765057065014126 0ustar www-datawww-data# base32 For Version: 0.1.3 This package contains base32, a Ruby extension for encoding and decoding in base32 per RFC 3548. ## Download The latest version of base32 can be found at http://rubyforge.org/frs/?group_id=3938 ## Installation ### Normal Installation You can install base32 with the following command from the distribution directory. ` rake install` ### Gem Installation Download and install base32 with the following command. ` gem install --remote base32` ### Running the Test Suite If you want to run the automated tests for base32, issue this command from the distribution directory. ` rake test:all` ## References * RFC 3548: http://www.faqs.org/rfcs/rfc3548.html ## Simple Example ```rb require "base32" encoded = Base32.encode("chunky bacon!") #==> "MNUHK3TLPEQGEYLDN5XCC===" decoded = Base32.decode(encoded) #==> "chunky bacon!" puts %Q{"#{decoded}" is "#{encoded}" in base32} ``` base32-0.3.4/CHANGELOG.md0000644000004100000410000000172613765057065014462 0ustar www-datawww-data# CHANGELOG ## 0.3.4 - PR #7: Updated README formatting (contributer: codehs) - PR #8: Include license in gemspec (contributer: flavio) - PR #9: Detect invalid characters when decoding (contributer: voondo) - PR #10: Properly initialize character table (contributer: pwnall) - PR #11: Move version.rb into lib/base32 (contributer: pravi) - PR #12: Fix deprecation warnings (contributer: kenchan) - PR #13: Fix build errors (contributor: kenchan) ## 0.3.2 - PR #5: Make padding optional for `random_base32` (contributer: lukesteensen) ## 0.3.1 - PR #4: Fix compatibility with older JRubies (contributor: localshred) ## 0.3.0 - Add `Base32::random_base32` function - Add test cases for hexadecimal conversion - Upgrade tests to use Minitest (successor to Test::Unit) - Add Travis and CodeClimate badges to README - Add Gemfile and gemspec files, using `gem-release` gem to release - Track version as a Ruby constant ## 0.2.0 and prior - Support Base32::encode/decode methods base32-0.3.4/.gitignore0000644000004100000410000000007013765057065014630 0ustar www-datawww-dataext/Makefile ext/*.o ext/*.so ext/*.bundle Gemfile.lock base32-0.3.4/LICENSE0000644000004100000410000000204513765057065013651 0ustar www-datawww-dataCopyright (c) 2007-2011 Samuel Tesla Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. base32-0.3.4/Rakefile0000644000004100000410000000062513765057065014313 0ustar www-datawww-databegin require 'bundler' Bundler.setup(:default, :development) rescue Bundler::BundlerError => e $stderr.puts e.message $stderr.puts 'Run `bundle install` to install missing gems' exit e.status_code end require 'bundler/gem_tasks' require 'rubygems' require 'rake/testtask' task :default => :test Rake::TestTask.new do |t| t.libs << 'test' t.test_files = FileList['test/**/*_test.rb'] end base32-0.3.4/lib/0000755000004100000410000000000013765057065013411 5ustar www-datawww-database32-0.3.4/lib/base32.rb0000644000004100000410000000344513765057065015023 0ustar www-datawww-datarequire 'openssl' # Module for encoding and decoding in Base32 per RFC 3548 module Base32 TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.freeze @table = TABLE class <> p (0..n-1).to_a.reverse.collect {|i| ((c >> i * 8) & 0xff).chr} end def encode n = (@bytes.length * 8.0 / 5.0).ceil p = n < 8 ? 5 - (@bytes.length * 8) % 5 : 0 c = @bytes.inject(0) {|m,o| (m << 8) + o} << p [(0..n-1).to_a.reverse.collect {|i| Base32.table[(c >> i * 5) & 0x1f].chr}, ("=" * (8-n))] end end def self.chunks(str, size) result = [] bytes = str.bytes while bytes.any? do result << Chunk.new(bytes.take(size)) bytes = bytes.drop(size) end result end def self.encode(str) chunks(str, 5).collect(&:encode).flatten.join end def self.decode(str) chunks(str, 8).collect(&:decode).flatten.join end def self.random_base32(length=16, padding=true) random = '' OpenSSL::Random.random_bytes(length).each_byte do |b| random << self.table[b % 32] end padding ? random.ljust((length / 8.0).ceil * 8, '=') : random end def self.table=(table) raise ArgumentError, "Table must have 32 unique characters" unless self.table_valid?(table) @table = table end def self.table_valid?(table) table.bytes.to_a.size == 32 && table.bytes.to_a.uniq.size == 32 end end base32-0.3.4/lib/base32/0000755000004100000410000000000013765057065014470 5ustar www-datawww-database32-0.3.4/lib/base32/version.rb0000644000004100000410000000004613765057065016502 0ustar www-datawww-datamodule Base32 VERSION = '0.3.4' end base32-0.3.4/config/0000755000004100000410000000000013765057065014110 5ustar www-datawww-database32-0.3.4/config/environment.rb0000644000004100000410000000245313765057065017005 0ustar www-datawww-data# Copyright (c) 2007-2011 Samuel Tesla # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. unless defined? BASE32_ROOT root_path = File.join(File.dirname(__FILE__), '..') BASE32_ROOT = File.expand_path(root_path) end $LOAD_PATH.unshift File.join(BASE32_ROOT, 'lib') $LOAD_PATH.unshift File.join(BASE32_ROOT, 'ext') base32-0.3.4/Gemfile0000644000004100000410000000004613765057065014136 0ustar www-datawww-datasource 'http://rubygems.org' gemspec base32-0.3.4/base32.gemspec0000644000004100000410000000133213765057065015266 0ustar www-datawww-data$:.unshift File.expand_path('../lib', __FILE__) require 'base32/version' Gem::Specification.new do |s| s.name = 'base32' s.version = Base32::VERSION s.authors = ['Samuel Tesla'] s.email = 'samuel.tesla@gmail.com' s.summary = 'Ruby extension for base32 encoding and decoding' s.licenses = ['MIT'] s.files = `git ls-files`.split($/) s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) } s.test_files = s.files.grep(%r{^(test|spec|features)/}) s.require_paths = ['lib'] s.post_install_message = File.read('UPGRADING') if File.exist?('UPGRADING') s.add_development_dependency 'rake' s.add_development_dependency 'minitest' s.add_development_dependency 'gem-release' end