Module: Ccrypto::Ruby::DataConversion

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Add the methods to class level



58
59
60
61
62
# File 'lib/ccrypto/ruby/data_conversion.rb', line 58

def self.included(klass)
  klass.class_eval <<-END
  extend Ccrypto::Ruby::DataConversion
  END
end

Instance Method Details

#from_b64(str, opts = { }) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ccrypto/ruby/data_conversion.rb', line 35

def from_b64(str, opts = { })
  if not str.nil?
    if not (opts[:strict].nil? and opts[:strict] == true)
      Base64.decode64(str)
    else
      Base64.strict_decode64(str)
    end
  else
    str
  end
end

#from_hex(str, opts = { }) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/ccrypto/ruby/data_conversion.rb', line 15

def from_hex(str, opts = { })
  if not str.nil?
    str.scan(/../).map { |x| x.hex.chr }.join
  else
    str
  end
end

#to_b64(bin, opts = { }) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ccrypto/ruby/data_conversion.rb', line 23

def to_b64(bin, opts = { })
  if not bin.nil?
    if not (opts[:strict].nil? and opts[:strict] == true)
      Base64.encode64(bin)
    else
      Base64.strict_encode64(bin)
    end
  else
    bin
  end
end

#to_hex(bin, opts = { }) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/ccrypto/ruby/data_conversion.rb', line 7

def to_hex(bin, opts = { })
  if not bin.nil?  
    bin.each_byte.map { |b| b.to_s(16).rjust(2,'0') }.join
  else
    bin
  end
end

#to_int_array(str, opts = { }) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/ccrypto/ruby/data_conversion.rb', line 47

def to_int_array(str, opts = { })
  if not str.nil?
    str.each_char.map { |c| c.ord }
  else
    str
  end
end