Class: Ipizza::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/ipizza/util.rb

Class Method Summary collapse

Class Method Details

.mac_data_string(params, sign_param_order) ⇒ Object

Produces string to be signed out of service message parameters.

p(x1)||x1||p(x2)||x2||...||p(xn)||xn

Where || is string concatenation, p(x) is length of the (stripped) field x represented by three digits.

Parameters val1, val2, value3 would be turned into “003val1003val2006value3”.



55
56
57
58
59
60
61
# File 'lib/ipizza/util.rb', line 55

def mac_data_string(params, sign_param_order)
  (sign_param_order || []).inject('') do |memo, param|
    val = params[param].to_s.strip
    memo << func_p(val) << val
    memo
  end
end

.sign(privkey_path, privkey_secret, data) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/ipizza/util.rb', line 18

def sign(privkey_path, privkey_secret, data)
  privkey = File.open(privkey_path, 'r') { |f| f.read }
  privkey = OpenSSL::PKey::RSA.new(privkey.gsub(/  /, ''), privkey_secret)

  signature = privkey.sign(OpenSSL::Digest::SHA1.new, data)
  signature = Base64.encode64(signature).gsub(/\n/, '')
end

.sign_731(ref_num) ⇒ Object

Calculates and adds control number using 7-3-1 algoritm for Estonian banking account and reference numbers.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ipizza/util.rb', line 27

def sign_731(ref_num)
  arr = ref_num.to_s.reverse.split('')
  m = 0
  r = 0

  arr.each do |e|
    m = case m
    when 7
      3
    when 3
      1
    else
      7
    end
    r = r + (e.to_i * m)
  end
  c = ((r + 9) / 10).to_f.truncate * 10 - r
  arr.reverse! << c
  arr.join
end

.time_to_iso8601(time) ⇒ Object



63
64
65
# File 'lib/ipizza/util.rb', line 63

def time_to_iso8601(time)
  time.strftime('%Y-%m-%dT%H:%M:%S%z')
end

.verify_signature(certificate_path, signature, data) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/ipizza/util.rb', line 9

def verify_signature(certificate_path, signature, data)
  if !certificate_path.to_s.empty? && !signature.to_s.empty? && File.file?(certificate_path)
    certificate = OpenSSL::X509::Certificate.new(File.read(certificate_path).gsub(/  /, '')).public_key
    certificate.verify(OpenSSL::Digest::SHA1.new, Base64.decode64(signature), data)
  else
    false
  end
end