Module: Base32

Defined in:
lib/Dnsruby/resource/NSEC3.rb

Overview

– Copyright 2007 Nominet UK

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ++

Constant Summary collapse

HEX =
'[0-9a-v]'

Class Method Summary collapse

Class Method Details

.decode32hex(str) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/Dnsruby/resource/NSEC3.rb', line 32

def decode32hex(str)
  str.gsub(/\G\s*(#{HEX}{8}|#{HEX}{7}=|#{HEX}{5}={3}|#{HEX}{4}={4}|#{HEX}{2}={6}|(\S))/imno) do
    raise "invalid base32" if $2
    s = $1
    s.tr("=", "0").to_i(32).divmod(256).pack("NC")[0,
      (s.count("^=")*5).div(8)]
  end
end

.encode32hex(str) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/Dnsruby/resource/NSEC3.rb', line 18

def encode32hex(str)
  str.gsub(/\G(.{5})|(.{1,4}\z)/mn) do
    full = $1; frag = $2
    n, c = (full || frag.ljust(5, "\0")).unpack("NC")
    full = ((n << 8) | c).to_s(32).rjust(8, "0")
    if frag
      full[0, (frag.length*8+4).div(5)].ljust(8, "=").upcase
    else
      full.upcase
    end
  end
end