base32-0.3.2/0000755000175600017570000000000012723526214011640 5ustar pravipravibase32-0.3.2/LICENSE0000644000175600017570000000204512723526214012646 0ustar pravipraviCopyright (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.2/.travis.yml0000644000175600017570000000013012723526214013743 0ustar pravipravilanguage: ruby rvm: - 1.8.7 - 1.9.2 - 1.9.3 - 2.0.0 - ruby-head - rbx-19modebase32-0.3.2/config/0000755000175600017570000000000012723526214013105 5ustar pravipravibase32-0.3.2/config/environment.rb0000644000175600017570000000245312723526214016002 0ustar pravipravi# 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.2/README0000644000175600017570000000216112723526214012520 0ustar pravipravi= base32 {}[https://travis-ci.org/stesla/base32] {}[https://codeclimate.com/github/stesla/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 require "base32" encoded = Base32.encode("chunky bacon!") #==> "MNUHK3TLPEQGEYLDN5XCC===" decoded = Base32.decode(encoded) #==> "chunky bacon!" puts %Q{"#{decoded}" is "#{encoded}" in base32} base32-0.3.2/test/0000755000175600017570000000000012723526214012617 5ustar pravipravibase32-0.3.2/test/base32_test.rb0000644000175600017570000001121012723526214015255 0ustar pravipravigem '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.2/CHANGELOG.md0000644000175600017570000000056612723526214013460 0ustar pravipravi# CHANGELOG ## 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.2/Gemfile0000644000175600017570000000004612723526214013133 0ustar pravipravisource 'http://rubygems.org' gemspec base32-0.3.2/lib/0000755000175600017570000000000012723526214012406 5ustar pravipravibase32-0.3.2/lib/base32.rb0000644000175600017570000000325412723526214014016 0ustar pravipravirequire 'openssl' # Module for encoding and decoding in Base32 per RFC 3548 module Base32 TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.freeze class Chunk def initialize(bytes) @bytes = bytes end def decode bytes = @bytes.take_while {|c| c != 61} # strip padding n = (bytes.length * 5.0 / 8.0).floor p = bytes.length < 8 ? 5 - (n * 8) % 5 : 0 c = bytes.inject(0) {|m,o| (m << 5) + Base32.table.index(o.chr)} >> 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 @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.2/lib/version.rb0000644000175600017570000000004612723526214014420 0ustar pravipravimodule Base32 VERSION = '0.3.2' end base32-0.3.2/base32.gemspec0000644000175600017570000000135612723526214014271 0ustar pravipravi$:.push File.expand_path('../lib', __FILE__) require '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.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.has_rdoc = true s.extra_rdoc_files = ['README'] s.post_install_message = File.read('UPGRADING') if File.exists?('UPGRADING') s.add_development_dependency 'rake' s.add_development_dependency 'minitest' s.add_development_dependency 'gem-release' end base32-0.3.2/Rakefile0000644000175600017570000000062512723526214013310 0ustar pravipravibegin 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.2/.gitignore0000644000175600017570000000007012723526214013625 0ustar pravipraviext/Makefile ext/*.o ext/*.so ext/*.bundle Gemfile.lock