Class Crypto


  • public class Crypto
    extends Object
    Util class that provides cryptography functions for drasyl.
    • Method Detail

      • generateKeys

        public static KeyPair generateKeys()
        Generates an asymmetric curve key pair for signing.
        Returns:
        asymmetric key pair
      • getPublicKeyFromBytes

        public static PublicKey getPublicKeyFromBytes​(byte[] pubKey)
                                               throws CryptoException
        Generates an asymmetric curve public key from the given bytes.
        Parameters:
        pubKey - public key as byte array
        Returns:
        asymmetric curve public key
        Throws:
        CryptoException - if public key could not be generated
      • getPrivateKeyFromBytes

        public static PrivateKey getPrivateKeyFromBytes​(byte[] privKey)
                                                 throws CryptoException
        Generates an asymmetric curve private key from the given bytes.
        Parameters:
        privKey - private key as byte array
        Returns:
        asymmetric curve private key
        Throws:
        CryptoException - if private key could not be generated
      • parseCompressedPublicKey

        public static org.bouncycastle.jce.interfaces.ECPublicKey parseCompressedPublicKey​(byte[] compressedPubKey)
                                                                                    throws CryptoException
        Generates an asymmetric curve public key from the given compressed public key.
        Parameters:
        compressedPubKey - compressed public key
        Returns:
        asymmetric curve public key
        Throws:
        CryptoException - if public key could not be generated
      • parseCompressedPrivateKey

        public static org.bouncycastle.jce.interfaces.ECPrivateKey parseCompressedPrivateKey​(byte[] compressedPrivateKey)
                                                                                      throws CryptoException
        Generates an asymmetric curve private key from the given compressed private key.
        Parameters:
        compressedPrivateKey - compressed private key
        Returns:
        asymmetric curve private key
        Throws:
        CryptoException - if private key could not be generated
      • compressedKey

        public static byte[] compressedKey​(PublicKey key)
                                    throws CryptoException
        Generates an asymmetric, compressed curve public key from the given public key.
        Parameters:
        key - the public key
        Returns:
        compressed public key
        Throws:
        CryptoException - if the public key was not in ECPublicKey format
      • compressedKey

        public static byte[] compressedKey​(PrivateKey privkey)
                                    throws CryptoException
        Generates an asymmetric, compressed curve private key from the given private key.
        Parameters:
        privkey - the private key
        Returns:
        compressed private key
        Throws:
        CryptoException - if the public key was not in ECPrivateKey format
      • signMessage

        public static byte[] signMessage​(PrivateKey key,
                                         byte[] message)
                                  throws CryptoException
        Creates signature from the given message with the PrivateKey.
        Parameters:
        key - Key to use
        message - message to sign
        Throws:
        CryptoException - on failure
      • verifySignature

        public static boolean verifySignature​(PublicKey pubkey,
                                              byte[] message,
                                              byte[] signature)
        Verify the signature of the given message with the signature and public key.
        Parameters:
        pubkey - the public key
        message - the message to verify
        signature - the signature of the message
        Returns:
        if the message is valid or not
      • randomString

        public static String randomString​(int entropy)
        Generates a secure random HEX String with the given entropy of bytes.

        Recommendation:

        • 4 byte for small sets
        • 8 bytes for unique internal strings, e.g. hash tables
        • 16 bytes for global uniqueness, e.g. auth token

        You can also use the following probability table for the "Birthday problem", as a starting point for a suitable entropy size: Birthday problem probability table

        Parameters:
        entropy - entropy in bytes
        Returns:
        a secure random HEX String
      • randomBytes

        public static byte[] randomBytes​(int entropy)
        Generates a secure random bytes with the given entropy.

        Recommendation:

        • 4 byte for small sets
        • 8 bytes for unique internal strings, e.g. hash tables
        • 16 bytes for global uniqueness, e.g. auth token

        You can also use the following probability table for the "Birthday problem", as a starting point for a suitable entropy size: Birthday problem probability table

        Parameters:
        entropy - entropy in bytes
        Returns:
        a secure random bytes
      • randomNumber

        public static int randomNumber​(int bound)
        Generates a random number with the static SecureRandom of this class. Avoids overhead of generating a new instance of SecureRandom.
        Parameters:
        bound - the upper bound (exclusive). Must be positive.
        Returns:
        the next pseudorandom, uniformly distributed int value between zero (inclusive) and bound (exclusive) from this random number generator's sequence