Class Crypto

java.lang.Object
org.drasyl.crypto.Crypto

public class Crypto extends Object
Util class that provides cryptography functions for drasyl.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final SecureRandom
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    Generates an asymmetric, compressed curve private key from the given private key.
    static byte[]
    Generates an asymmetric, compressed curve public key from the given public key.
    static KeyPair
    Generates an asymmetric curve key pair for signing.
    static PrivateKey
    getPrivateKeyFromBytes(byte[] privKey)
    Generates an asymmetric curve private key from the given bytes.
    static PublicKey
    getPublicKeyFromBytes(byte[] pubKey)
    Generates an asymmetric curve public key from the given bytes.
    static KeyPair
    makeKeyPair(byte[] compressedPrivate, byte[] compressedPublic)
    Generates an asymmetric curve key pair from the given compressed private and public key.
    static org.bouncycastle.jce.interfaces.ECPrivateKey
    parseCompressedPrivateKey(byte[] compressedPrivateKey)
    Generates an asymmetric curve private key from the given compressed private key.
    static org.bouncycastle.jce.interfaces.ECPublicKey
    parseCompressedPublicKey(byte[] compressedPubKey)
    Generates an asymmetric curve public key from the given compressed public key.
    static int
    randomNumber(int bound)
    Generates a random number with the static SecureRandom of this class.
    static String
    randomString(int entropy)
    Generates a secure random HEX String with the given entropy of bytes.
    static void
    sign(PrivateKey key, Signable signable)
    Signs the given signable with the PrivateKey.
    static byte[]
    signMessage(PrivateKey key, byte[] message)
    Creates signature from the given message with the PrivateKey.
    static boolean
    verifySignature(byte[] compressedPublicKey, byte[] message, byte[] signature)
    Verifies the signature of the given message with the signature and compressed public key.
    static boolean
    verifySignature(PublicKey pubkey, byte[] message, byte[] signature)
    Verify the signature of the given message with the signature and public key.
    static boolean
    verifySignature(PublicKey publicKey, Signable content)
    Verify the signature of the given Signable object with the public key.

    Methods inherited from class java.lang.Object

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

  • Method Details

    • generateKeys

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

      public static KeyPair makeKeyPair(byte[] compressedPrivate, byte[] compressedPublic) throws CryptoException
      Generates an asymmetric curve key pair from the given compressed private and public key.
      Parameters:
      compressedPrivate - compressed private key
      compressedPublic - compressed public key
      Returns:
      asymmetric curve key pair
      Throws:
      CryptoException - if key pair could not be generated
    • 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
    • sign

      public static void sign(PrivateKey key, Signable signable) throws CryptoException
      Signs the given signable with the PrivateKey. This will also put the resulting signature into the Signable object
      Parameters:
      key - Key to use
      signable - signature to create
      Throws:
      CryptoException - on failure
    • 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(byte[] compressedPublicKey, byte[] message, byte[] signature)
      Verifies the signature of the given message with the signature and compressed public key.
      Parameters:
      compressedPublicKey - the compressed public key
      message - the message to verify
      signature - the signature of the message
      Returns:
      if the message is valid or not
    • 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
    • verifySignature

      public static boolean verifySignature(PublicKey publicKey, Signable content)
      Verify the signature of the given Signable object with the public key.
      Parameters:
      publicKey - the public key
      content - the Signable object
      Returns:
      if the content 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
    • 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