Class PgpHelper

java.lang.Object
com.hsbc.pgp.helper.PgpHelper

public class PgpHelper extends Object

This class is used to encrypt and decrypt data using PGP keys. Encrypted data can be used to call the HSBC Treasury APIs. The response from the HSBC Treasury APIs can be decrypted using the same keys. Keys and the headers used to call the HSBC Treasury APIs are provided by HSBC on the Developer Portal for each project.

Created by 44024985 on 04/09/2018. Updated by 45274934 on 28/03/2024.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    decryptStream(InputStream in, OutputStream out, List<org.bouncycastle.openpgp.PGPPrivateKey> keysIn, List<org.bouncycastle.openpgp.PGPPublicKey> publicKeys)
    This method decrypts a stream of data (PGP message) using a list of private keys and a list of public keys.
    void
    encryptAndSign(OutputStream outf, InputStream inputStream, org.bouncycastle.openpgp.PGPPublicKey encKey, org.bouncycastle.openpgp.PGPPrivateKey privateKey)
    This method encrypts and signs a stream of data using a public key and a private key.
    org.bouncycastle.openpgp.PGPPrivateKey
    findSecretKey(org.bouncycastle.openpgp.PGPSecretKey pgpSecKey, char[] pass)
    This method finds a PGP Private key from a PGP secret key ring using a passphrase.
    List<org.bouncycastle.openpgp.PGPPublicKey>
    readPublicKey(InputStream publicKeyInputStream)
    This method reads a public key from an input stream and returns a list of PGP Public Keys.
    List<org.bouncycastle.openpgp.PGPSecretKey>
    This method reads a secret key from an input stream and returns a list of PGP Secret Keys.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PgpHelper

      public PgpHelper()
  • Method Details

    • readPublicKey

      public List<org.bouncycastle.openpgp.PGPPublicKey> readPublicKey(InputStream publicKeyInputStream) throws IOException, org.bouncycastle.openpgp.PGPException

      This method reads a public key from an input stream and returns a list of PGP Public Keys. The list of PGP Public Key objects are used as an input for encryptAndSign(OutputStream, InputStream, PGPPublicKey, PGPPrivateKey) and decryptStream(InputStream, OutputStream, List, List).

      Parameters:
      publicKeyInputStream - bank/public key input stream.
      Returns:
      List of PGP Public Keys
      Throws:
      IOException
      org.bouncycastle.openpgp.PGPException
    • findSecretKey

      public org.bouncycastle.openpgp.PGPPrivateKey findSecretKey(org.bouncycastle.openpgp.PGPSecretKey pgpSecKey, char[] pass) throws org.bouncycastle.openpgp.PGPException

      This method finds a PGP Private key from a PGP secret key ring using a passphrase.

      Parameters:
      pgpSecKey - Secret Key.
      pass - passphrase to decrypt secret key with.
      Returns:
      PGPPrivate key.
      Throws:
      org.bouncycastle.openpgp.PGPException
    • readSecretKey

      public List<org.bouncycastle.openpgp.PGPSecretKey> readSecretKey(InputStream input) throws IOException, org.bouncycastle.openpgp.PGPException

      This method reads a secret key from an input stream and returns a list of PGP Secret Keys. These then need to be extracted to get the private key using the passphrase. This step is carried out by findSecretKey.

      Parameters:
      input - private key input stream.
      Returns:
      List of PGP Secret Keys
      Throws:
      IOException
      org.bouncycastle.openpgp.PGPException
    • encryptAndSign

      public void encryptAndSign(OutputStream outf, InputStream inputStream, org.bouncycastle.openpgp.PGPPublicKey encKey, org.bouncycastle.openpgp.PGPPrivateKey privateKey) throws IOException

      This method encrypts and signs a stream of data using a public key and a private key. The output is a PGP message which as been base64-encoded and contains the signed, encrypted data.

      Parameters:
      outf - the output stream
      inputStream - the input stream
      encKey - the PGP Public key
      privateKey - the private key
      Throws:
      IOException
    • decryptStream

      public void decryptStream(InputStream in, OutputStream out, List<org.bouncycastle.openpgp.PGPPrivateKey> keysIn, List<org.bouncycastle.openpgp.PGPPublicKey> publicKeys) throws Exception

      This method decrypts a stream of data (PGP message) using a list of private keys and a list of public keys. The signature is verified using the public keys.

      Parameters:
      in - the input stream
      out - the output stream
      keysIn - the list of private keys
      publicKeys - the list of public keys
      Throws:
      Exception