Class CryptoUtils

java.lang.Object
com.blockchyp.client.crypto.CryptoUtils

public final class CryptoUtils extends Object
This is utility class providing cryptography support for the BlockChyp client. It handles the authorization headers required for direct gateway requests along with AES encryption for the offline route cache.
  • Field Details

  • Method Details

    • getInstance

      public static CryptoUtils getInstance()
      Returns the CryptoUtils singleton.
      Returns:
      an instance of CryptoUtils
    • computeSHA256

      public String computeSHA256(String hex)
      Computes a SHA 256 hash of the given hex.
      Parameters:
      hex - data to be hashed in hex format.
      Returns:
      hex encoded hash of the input data
    • generateApiHeaders

      public Map<String,String> generateApiHeaders(String apiKey, String bearerToken, String signingKey)
      Generates the big three API request headers based on the input credentials.
      Parameters:
      apiKey - BlockChyp API Key (root or transient)
      bearerToken - BlockChyp Bearer Token (root or transient)
      signingKey - BlockChyp Signing Key (root or transient)
      Returns:
      a Map containing the API request headers.
    • generateTimestamp

      public String generateTimestamp()
      Generates a timestamp encoded for use in Gateway API request headers.
      Returns:
      ISO encoded UTC timestamp string.
    • decrypt

      public String decrypt(String cipherText, byte[] key) throws Exception
      Decrypts ciphertext previously encoded with encrypt(java.lang.String,byte[]).
      Parameters:
      cipherText - the initialization vector and ciphertext separated by a pipe character.
      key - the AES encryption to use.
      Returns:
      the plain text
      Throws:
      Exception - if the decryption or associated encoding operations fail.
    • encrypt

      public String encrypt(String plainText, byte[] key) throws Exception
      Encrypts the given plain text using the given key using AES with PKCS#5 padding. Generates and returns a CBC initialization vector prepended to the main ciphertext.
      Parameters:
      plainText - the message to be encrypted
      key - AES encryption key
      Returns:
      return hex encoded initialization vector and ciphertext, pipe delimited.
      Throws:
      Exception - if an error occurs during encryption or encoding.
    • randomBytes

      public byte[] randomBytes(int len)
      Returns an array of random bytes of the given length.
      Parameters:
      len - Number of random bytes to return.
      Returns:
      byte[]