Module: Banklink::Common

Extended by:
ActiveSupport::Concern
Included in:
Seb::Helper, Seb::Notification, Swedbank::Helper, Swedbank::Notification
Defined in:
lib/banklink/banklink.rb

Overview

Calculation using method VK_VERSION=008: VK_MAC is RSA signature of the request fields coded into BASE64. VK_MAC will be calculated using secret key of the sender using RSA. Signature will be calculated for string that consists of all field lengths and contents in the query. Also empty fields are used in calculation – lenght “000”. Unnumbered (optional) fields are not used in calculation. MAC(x1,x2,…,xn) := RSA( SHA-1(p(x1 )|| x1|| p(x2 )|| x2 || … ||p( xn )||xn),d,n) where: || is string concatenation mark x1, x2, …, xn are parameters of the query p(x) is length of the field x represented by three digits d is RSA secret exponent n is RSA modulus

Instance Method Summary collapse

Instance Method Details

#encode_to_utf8(string) ⇒ Object



49
50
51
# File 'lib/banklink/banklink.rb', line 49

def encode_to_utf8 string
  string.encode('UTF-8', :invalid => :replace, :replace => '').encode('UTF-8')
end

#func_p(val) ⇒ Object

p(x) is length of the field x represented by three digits



20
21
22
# File 'lib/banklink/banklink.rb', line 20

def func_p(val)
  sprintf("%03i", val.length)
end

#generate_data_string(service_msg_number, sigparams, required_service_params) ⇒ Object

Generate a string to be signed out of service message parameters. p(x1 )|| x1|| p(x2 )|| x2 || … ||p( xn )||xn || is string concatenation mark p(x) is length of the field x represented by three digits Parameters val1, val2, value3 would be turned into: ‘003val1003val2006value3’



30
31
32
33
34
35
36
37
38
# File 'lib/banklink/banklink.rb', line 30

def generate_data_string(service_msg_number, sigparams, required_service_params)
  str = ''
  required_params = required_service_params[Integer(service_msg_number)] || required_service_params[service_msg_number]
  required_params.each do |param|
    val = sigparams[param].to_s # nil goes to ''
    str << func_p(val) << val
  end
  str
end

#generate_mac(service_msg_number, sigparams, required_service_params) ⇒ Object



45
46
47
# File 'lib/banklink/banklink.rb', line 45

def generate_mac(service_msg_number, sigparams, required_service_params)
  Base64.encode64(generate_signature(service_msg_number, sigparams, required_service_params)).gsub(/\n/,'')
end

#generate_signature(service_msg_number, sigparams, required_service_params) ⇒ Object



40
41
42
43
# File 'lib/banklink/banklink.rb', line 40

def generate_signature(service_msg_number, sigparams, required_service_params)
  privkey = self.class.parent.get_private_key
  privkey.sign(OpenSSL::Digest::SHA1.new, generate_data_string(service_msg_number, sigparams, required_service_params))
end

#parse(post) ⇒ Object

Take the posted data and move the relevant data into a hash



54
55
56
57
58
59
60
# File 'lib/banklink/banklink.rb', line 54

def parse(post)
  @raw = post.to_s
  for line in @raw.split('&')
    key, value = *line.scan( %r{^([A-Za-z0-9_.]+)\=(.*)$} ).flatten
    params[key] = CGI.unescape(value)
  end
end