Class: Fintecture::Utils::Crypto

Inherits:
Object
  • Object
show all
Defined in:
lib/fintecture/utils/crypto.rb

Class Method Summary collapse

Class Method Details

.create_signature_header(headers, client) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fintecture/utils/crypto.rb', line 55

def create_signature_header(headers, client)
  @client = client
  signing = []
  header = []

  Fintecture::Utils::Constants::SIGNEDHEADERPARAMETERLIST.each do |param|
    next unless headers[param]

    param_low = param.downcase
    signing << "#{param_low}: #{headers[param]}"
    header << param_low
  end

  # Double quote in join needed. If not we will get two slashes \\n
  signature = sign_payload signing.join("\n")
  "keyId=\"#{@client.app_id}\",algorithm=\"rsa-sha256\",headers=\"#{header.join(' ')}\",signature=\"#{signature}\""
end

.decrypt_private(digest) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fintecture/utils/crypto.rb', line 36

def decrypt_private(digest)
  digest = URI.unescape digest
  encrypted_string = Base64.decode64(digest)
  private_key = OpenSSL::PKey::RSA.new(@client.private_key)

  begin
    private_key.private_decrypt(encrypted_string, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
  rescue OpenSSL::PKey::RSAError => e
    raise Fintecture::CryptoException, "error while decrypt, #{e.message}"
  rescue StandardError
    raise Fintecture::CryptoException, 'error during decryption'
  end
end

.generate_uuidObject



15
16
17
# File 'lib/fintecture/utils/crypto.rb', line 15

def generate_uuid
  SecureRandom.uuid
end

.generate_uuid_only_charsObject



19
20
21
# File 'lib/fintecture/utils/crypto.rb', line 19

def generate_uuid_only_chars
  generate_uuid.gsub!('-', '')
end

.hash_base64(plain_text) ⇒ Object



50
51
52
53
# File 'lib/fintecture/utils/crypto.rb', line 50

def hash_base64(plain_text)
  digest = Digest::SHA256.digest plain_text
  Base64.strict_encode64(digest)
end

.sign_payload(payload) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fintecture/utils/crypto.rb', line 23

def sign_payload(payload)
  payload = payload.to_json.to_s if payload.is_a? Hash
  digest = OpenSSL::Digest.new('SHA256')
  private_key = OpenSSL::PKey::RSA.new(@client.private_key)

  begin
    signature = private_key.sign(digest, payload)
    Base64.strict_encode64(signature)
  rescue StandardError
    raise Fintecture::CryptoException, 'error during signature'
  end
end