rfc-base32 0.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.
Files changed (2) hide show
  1. data/lib/rfc.rb +63 -0
  2. metadata +65 -0
@@ -0,0 +1,63 @@
1
+ # coding: binary
2
+
3
+ # A popular base32 algorithm by RFC4648. (not the Crockford one)
4
+ # Ruby 1.9.x Only.
5
+ # Reference - http://tools.ietf.org/html/rfc4648#section-6
6
+ module RFC
7
+ module Base32
8
+
9
+ ENCODE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
10
+ DECODE = Hash[ENCODE.chars.each_with_index.to_a]
11
+ # remain => pad
12
+ PAD = ['', '======', '====', '===', '=']
13
+
14
+ def encode32 str
15
+ remain = str.bytesize % 5
16
+ str += "\0" * (5 - remain)
17
+ str.gsub!(/.{5}/mn) do |s|
18
+ # split 40bits into [unsigned big endian int] + [unsigned byte]
19
+ d, r = s.unpack 'NC'
20
+ d = (d << 8) | r
21
+ 8.times.inject '' do |acc|
22
+ d, r = d.divmod 32
23
+ "#{ENCODE[r]}#{acc}"
24
+ end
25
+ end
26
+ str << PAD[remain]
27
+ end
28
+
29
+ def decode32 str
30
+ pad = str[/=*$/]
31
+ str = str[0..(-1-pad.bytesize)]
32
+ str.gsub!(/.{8}/mn) do |s|
33
+ d = s.chars.inject(0) {|s, c| (s << 5) | DECODE[c]}
34
+ r = d & 255
35
+ d >>= 8
36
+ [d, r].pack 'NC'
37
+ end
38
+ str[0..(PAD.index(pad) - 6)]
39
+ end
40
+ end
41
+
42
+ extend Base32
43
+ end
44
+
45
+ # test
46
+ if $0 == __FILE__
47
+ s = 100.times.map{rand 256}.pack('C*') + "hello_w\r\norld"
48
+
49
+ 0.upto 10 do |i|
50
+ a = s[0..i]
51
+ b = RFC.decode32 RFC.encode32 a
52
+ raise a if a != b
53
+ end
54
+
55
+ require "benchmark"
56
+ puts Benchmark.measure {
57
+ 100.times{ RFC.encode32 s }
58
+ }
59
+
60
+ puts RFC.encode32 'hello world'
61
+
62
+ end
63
+
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rfc-base32
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - luikore
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-10-28 00:00:00 +08:00
17
+ default_executable:
18
+ dependencies: []
19
+
20
+ description: RFC4648 Base32 Encode / Decode
21
+ email: OVZXK4TGMZ4EAZ3NMFUWYLTDN5WQAAAA====
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files: []
27
+
28
+ files:
29
+ - lib/rfc.rb
30
+ has_rdoc: true
31
+ homepage: http://github.com/luikore/rfc-base32
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ segments:
45
+ - 1
46
+ - 9
47
+ - 0
48
+ version: 1.9.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.7
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: RFC4648 Base32 Encode / Decode
64
+ test_files: []
65
+