Class CryptoHelper


  • public abstract class CryptoHelper
    extends Object
    Helper abstract class with crypto utility methods
    • Field Detail

      • DIGITS

        private static final char[] DIGITS
        The Constant DIGITS.
      • MASK_256

        private static final BigInteger MASK_256
    • Constructor Detail

      • CryptoHelper

        public CryptoHelper()
    • Method Detail

      • encrypt

        public static byte[] encrypt​(byte[] publicKey,
                                     byte[] inputData)
                              throws CryptoException
        Given a public key and input data encrypts the input data using the public key. So only the holder of the private key can decrypt the data
        Parameters:
        publicKey - public key byte[]
        inputData - data to encrypt []
        Returns:
        encrypted bytes
        Throws:
        CryptoException - Encryption error
      • encrypt

        public static byte[] encrypt​(byte[] publicKey,
                                     byte[] inputData,
                                     String algorithm)
                              throws CryptoException
        Given a public key and input data encrypts the input data using the public key. So only the holder of the private key can decrypt the data
        Parameters:
        publicKey - public key byte[]
        inputData - data to encrypt []
        algorithm - algorithm to use
        Returns:
        encrypted bytes
        Throws:
        CryptoException - Encryption error
      • decrypt

        public static byte[] decrypt​(byte[] privateKey,
                                     byte[] inputData)
                              throws CryptoException
        Given a private key and some encrypted data tries to decrypt the data using the private key
        Parameters:
        privateKey - private key byte[]
        inputData - encrypted input data
        Returns:
        decrypted data
        Throws:
        CryptoException - Decryption error
      • decrypt

        public static byte[] decrypt​(byte[] privateKey,
                                     byte[] inputData,
                                     String algorithm)
                              throws CryptoException
        Given a private key and some encrypted data tries to decrypt the data using the private key
        Parameters:
        privateKey - private key byte[]
        inputData - encrypted input data
        algorithm - algorithm to use
        Returns:
        decrypted data
        Throws:
        CryptoException - Decryption error
      • generateKeyPair

        public static KeyPair generateKeyPair​(String algorithm,
                                              int keySize)
                                       throws CryptoException
        Generates a ECSDA KeyPair
        Parameters:
        algorithm - Algorithm to use
        keySize - key size
        Returns:
        KeyPair
        Throws:
        CryptoException - error generating key pair
      • sha3_256

        public static String sha3_256​(String input)
        Given an input string generates the digest SHA3 256 hash
        Parameters:
        input -
        Returns:
      • keccak256

        public static byte[] keccak256​(String input)
        Given an input string generates the equivalent to keccak256(abi.encodePacked())
        Parameters:
        input -
        Returns:
      • sha3256

        public static String sha3256​(String input)
        Given an input string return the result of sha3256
        Parameters:
        input - string
        Returns:
        hashed message
      • soliditySha3

        public static byte[] soliditySha3​(Object... data)
        Given N objects, return the SHA3 of those objects
        Parameters:
        data - objects
        Returns:
        sha3
      • getHex

        public static String getHex​(byte[] data)
        Gets the hex.
        Parameters:
        data - the data
        Returns:
        the hex
      • toBytes

        public static byte[] toBytes​(Object obj)
        Given an object (byte[], BigInteger, Address, Uint or Number) gets the byte[] with the proper length
        Parameters:
        obj - the object
        Returns:
        byte[]