Class: PostFinanceCheckout::EncryptionUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/postfinancecheckout-ruby-sdk/encryption_util.rb

Class Method Summary collapse

Class Method Details

.get_algorithm_class(algorithm) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/postfinancecheckout-ruby-sdk/encryption_util.rb', line 31

def self.get_algorithm_class(algorithm)
  case algorithm
  when 'SHA256withECDSA'
    OpenSSL::PKey::EC
  else
    nil
  end
end

.is_content_valid(content, signature, public_key, encryption_algorithm) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/postfinancecheckout-ruby-sdk/encryption_util.rb', line 6

def self.is_content_valid(content, signature, public_key, encryption_algorithm)
  algorithm_class = get_algorithm_class(encryption_algorithm)
  raise "Unsupported algorithm: #{encryption_algorithm}" if algorithm_class.nil?

  begin
    signature = Base64.decode64(signature)
  rescue ArgumentError
    raise 'Invalid signature value format'
  end

  begin
    public_key_bytes = Base64.decode64(public_key)
  rescue ArgumentError
    raise 'Invalid public key value format'
  end

  public_key = OpenSSL::PKey.read(public_key_bytes)

  begin
    return public_key.verify(OpenSSL::Digest::SHA256.new, signature, content)
  rescue OpenSSL::PKey::PKeyError, OpenSSL::PKey::ECError, OpenSSL::PKey::EC::Point::Error
    return false
  end
end