base32-multi 0.1.1

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.
@@ -0,0 +1,23 @@
1
+ require File.expand_path("../lib/base32/version", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'base32-multi'
5
+ s.rubyforge_project = 'base32-multi'
6
+
7
+ s.platform = Gem::Platform::RUBY
8
+ s.version = Base32::VERSION.to_s
9
+
10
+ s.summary = "Multi-variant Base32 encoder and decoder"
11
+ s.description = "pure-ruby encoding and decoding of Base32 with support for zbase32 and Crockford's base32"
12
+ s.license = 'MIT'
13
+
14
+ s.authors = ["Sean Keith McAuley"]
15
+ s.email = 'tsu@peripia.com'
16
+ s.homepage = 'https://github.com/tsutsu/base32-multi'
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ end
23
+
@@ -0,0 +1,26 @@
1
+ module Base32
2
+ # native-ruby encoding/decoding implementation
3
+ # probably could be a lot faster!
4
+
5
+ module_function
6
+
7
+ def b2a(binstr)
8
+ n_pentets = ((binstr.length * 8) + 4) / 5
9
+ bigint = binstr.bytes.inject(0){ |i,octet| (i << 8) | octet }
10
+ bigint.to_s(32).rjust(n_pentets, '0')
11
+ end
12
+
13
+ def a2b(ascstr)
14
+ n_octets = (ascstr.length * 5) / 8
15
+ bigint = ascstr.to_i(32)
16
+ octet_arr = Array.new(n_octets)
17
+
18
+ while n_octets > 0
19
+ octet_arr[n_octets - 1] = bigint & 0b11111111
20
+ bigint >>= 8
21
+ n_octets -= 1
22
+ end
23
+
24
+ octet_arr.pack('C*')
25
+ end
26
+ end
@@ -0,0 +1,11 @@
1
+ class Base32::Variant
2
+ class Crockford < Base32::Variant
3
+ def alphabet
4
+ '0123456789ABCDEFGHJKMNPQRSTVWXYZ'
5
+ end
6
+
7
+ def clean(ascstr)
8
+ super(ascstr.tr('OIL', '011'))
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ class Base32::Variant
2
+ class Plain < Base32::Variant
3
+ end
4
+ end
@@ -0,0 +1,11 @@
1
+ class Base32::Variant
2
+ class ZBase32 < Base32::Variant
3
+ def alphabet
4
+ 'ybndrfg8ejkmcpqxot1uwisza345h769'
5
+ end
6
+
7
+ def clean(asc_src)
8
+ asc_src.downcase.tr("^#{self.alphabet}", '')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,33 @@
1
+ module Base32
2
+ class Variant
3
+ def alphabet
4
+ Base32::ALPHABET
5
+ end
6
+
7
+ def wrap(asc_src)
8
+ asc_src.tr(Base32::ALPHABET, self.alphabet)
9
+ end
10
+
11
+ def unwrap(asc_src)
12
+ asc_src.tr(self.alphabet, Base32::ALPHABET)
13
+ end
14
+
15
+ def clean(asc_src)
16
+ asc_src.upcase.tr("^#{self.alphabet}", '')
17
+ end
18
+
19
+
20
+ def self.shortname
21
+ self.name.split('::').last.downcase.intern
22
+ end
23
+
24
+ def self.[](variant_name)
25
+ @variants ||= {}
26
+
27
+ @variants[variant_name] ||= (
28
+ require "base32/variant/#{variant_name}"
29
+ self.constants.map{ |k| self.const_get(k) }.find{ |k| k.shortname == variant_name }.new
30
+ )
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ module Base32
2
+ module VERSION
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 1
6
+
7
+ def self.to_s
8
+ "#{MAJOR}.#{MINOR}.#{TINY}"
9
+ end
10
+ end
11
+ end
data/lib/base32.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'base32/base32'
2
+ require 'base32/variant'
3
+
4
+ module Base32
5
+ ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUV'
6
+
7
+ module_function
8
+
9
+ def encode32(binstr, variant_name = :plain)
10
+ Variant[variant_name].wrap(self.b2a(binstr))
11
+ end
12
+
13
+ def decode32(ascstr, variant_name = :plain)
14
+ variant = Variant[variant_name]
15
+ cleanascstr = variant.clean(ascstr)
16
+
17
+ if cleanascstr.length == 0 && ascstr.length != 0
18
+ raise ArgumentError, "invalid Base32 sequence"
19
+ end
20
+
21
+ self.a2b(variant.unwrap(cleanascstr))
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ require 'digest'
2
+ require 'base32'
3
+
4
+ module Digest
5
+ module Instance
6
+ def base32digest(variant_name = nil)
7
+ Base32.encode32(self.digest, variant_name)
8
+ end
9
+
10
+ def base32digest!(variant_name = nil)
11
+ Base32.encode32(self.digest!, variant_name)
12
+ end
13
+ end
14
+
15
+ class Base
16
+ def self.base32digest(str, variant_name = nil)
17
+ Base32.encode32(self.digest(str), variant_name)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ require 'base32'
2
+ require 'securerandom'
3
+
4
+ module SecureRandom
5
+ module_function
6
+
7
+ def base32(num_bytes = nil, variant_name = nil)
8
+ Base32.encode32(self.random_bytes(num_bytes), variant_name)
9
+ end
10
+
11
+ # 160 bits rather than 128, to ensure a constant number of pentets (32)
12
+ def base32_uuid(variant_name = nil)
13
+ Base32.encode32(self.random_bytes(20), variant_name)
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: base32-multi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sean Keith McAuley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: pure-ruby encoding and decoding of Base32 with support for zbase32 and
15
+ Crockford's base32
16
+ email: tsu@peripia.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - base32-multi.gemspec
22
+ - lib/base32.rb
23
+ - lib/base32/base32.rb
24
+ - lib/base32/variant.rb
25
+ - lib/base32/variant/crockford.rb
26
+ - lib/base32/variant/plain.rb
27
+ - lib/base32/variant/zbase32.rb
28
+ - lib/base32/version.rb
29
+ - lib/digest/base32.rb
30
+ - lib/securerandom/base32.rb
31
+ homepage: https://github.com/tsutsu/base32-multi
32
+ licenses:
33
+ - MIT
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project: base32-multi
52
+ rubygems_version: 1.8.23
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Multi-variant Base32 encoder and decoder
56
+ test_files: []