Class: Ipizza::Util

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

Constant Summary collapse

DEFAULT_HASH_ALGORITHM =
'sha1'

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”.



64
65
66
67
68
69
70
# File 'lib/ipizza/util.rb', line 64

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, hash_algorithm = DEFAULT_HASH_ALGORITHM) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/ipizza/util.rb', line 24

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

  signature = privkey.sign(
    digest_class(hash_algorithm).new,
    data
  )
  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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ipizza/util.rb', line 36

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



72
73
74
# File 'lib/ipizza/util.rb', line 72

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

.verify_signature(certificate_path, signature, data, hash_algorithm = DEFAULT_HASH_ALGORITHM) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ipizza/util.rb', line 11

def verify_signature(certificate_path, signature, data, hash_algorithm = DEFAULT_HASH_ALGORITHM)
  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(
      digest_class(hash_algorithm).new,
      Base64.decode64(signature),
      data
    )
  else
    false
  end
end