lib/eet/utils.rb in eet-0.1.1 vs lib/eet/utils.rb in eet-0.2.0
- old
+ new
@@ -1,10 +1,12 @@
+require 'signer'
+
module Eet
module Utils
- def self.create_pkp(data, certificate)
+ def self.create_pkp(message, certificate)
digest = OpenSSL::Digest::SHA256.new
- signature = certificate.key.sign(digest, serialize_pkp_data(data))
+ signature = certificate.key.sign(digest, serialize_pkp_data(message))
Base64.encode64(signature).delete("\n")
end
def self.create_bkp(pkp_value)
decoded = Base64.decode64(pkp_value)
@@ -16,17 +18,34 @@
ch = c.ord.to_s(16)
ch = '0' + ch if ch.size == 1
ret += ch
end
- # '9356D566-A3E48838-FB403790-D201244E-95DCBD92'
ret.upcase.chars.each_slice(8).map(&:join).join('-')
end
- private
+ def self.sign(xml, certificate)
+ signer = Signer.new(xml)
+ signer.cert = OpenSSL::X509::Certificate.new(certificate.certificate)
+ signer.private_key = OpenSSL::PKey::RSA.new(certificate.key, 'eet')
- def self.serialize_pkp_data(data)
- # "CZ72080043|181|00/2535/CN58|0/2482/IE25|2016-12-07T22:01:00+01:00|87988.00"
- [data[:dic_popl], data[:id_provoz], data[:id_pokl], data[:porad_cis], data[:dat_trzby], data[:celk_trzba]].join('|')
+ signer.security_node = signer.document.children.first.children.first.children.first
+ signer.digest_algorithm = :sha256
+ signer.signature_digest_algorithm = :sha256
+ signer.ds_namespace_prefix = 'ds'
+ signer.security_token_id = 'A79845F15C5549CA0514761283545705'
+ signer.digest!(signer.document.at_xpath('//soap:Body'), inclusive_namespaces: [''])
+ signer.sign!(security_token: true, inclusive_namespaces: ['soap'])
+
+ signer.to_xml
+ end
+
+ def self.serialize_pkp_data(message)
+ [message.dic_popl,
+ message.id_provoz,
+ message.id_pokl,
+ message.porad_cis,
+ message.dat_trzby,
+ message.celk_trzba].join('|')
end
end
end