Class DrasylSodiumWrapper

java.lang.Object
org.drasyl.crypto.sodium.DrasylSodiumWrapper

@UnstableApi public class DrasylSodiumWrapper extends Object
Simple wrapper class that make native class easier.
  • Field Details

  • Constructor Details

    • DrasylSodiumWrapper

      public DrasylSodiumWrapper(Sodium sodium)
  • Method Details

    • sha256

      public byte[] sha256(byte[] in) throws CryptoException
      Generates a SHA-256 hash of the given input.
      Parameters:
      in - the input to hash
      Returns:
      SHA-256 hash of the input
      Throws:
      CryptoException
    • cryptoSignKeypair

      public boolean cryptoSignKeypair(byte[] publicKey, byte[] secretKey)
      Generate a signing keypair (ed25519).
      Parameters:
      publicKey - Public key.
      secretKey - Secret key.
      Returns:
      True if successful.
    • convertPublicKeyEd25519ToCurve25519

      public boolean convertPublicKeyEd25519ToCurve25519(byte[] curve, byte[] ed)
      Converts a public ed25519 key to a public curve25519 key.
      Parameters:
      curve - The array in which the generated key will be placed.
      ed - The public key in ed25519.
      Returns:
      Return true if the conversion was successful.
    • convertSecretKeyEd25519ToCurve25519

      public boolean convertSecretKeyEd25519ToCurve25519(byte[] curve, byte[] ed)
      Converts a secret ed25519 key to a secret curve25519 key.
      Parameters:
      curve - The array in which the generated key will be placed.
      ed - The secret key in ed25519.
      Returns:
      Return true if the conversion was successful.
    • cryptoKxServerSessionKeys

      public SessionPair cryptoKxServerSessionKeys(byte[] serverPk, byte[] serverSk, byte[] clientPk) throws CryptoException
      This function computes a pair of shared keys (rx and tx) using the client's public key clientPk, the server's secret key serverSk and the server's public key serverPk.
      Parameters:
      serverPk - Server public key of size CURVE25519_PUBLICKEYBYTES.
      serverSk - Server secret key of size CURVE25519_SECRETKEYBYTES.
      clientPk - Client public key of size CURVE25519_PUBLICKEYBYTES.
      Returns:
      True if successful or false if the client public key is wrong.
      Throws:
      CryptoException
    • cryptoKxClientSessionKeys

      public SessionPair cryptoKxClientSessionKeys(byte[] clientPk, byte[] clientSk, byte[] serverPk) throws CryptoException
      This function computes a pair of shared keys (rx and tx) using the client's public key clientPk, the client's secret key clientSk and the server's public key serverPk.
      Parameters:
      clientPk - Client public key of size CURVE25519_PUBLICKEYBYTES.
      clientSk - Client secret key of size CURVE25519_SECRETKEYBYTES.
      serverPk - Server public key of size CURVE25519_PUBLICKEYBYTES.
      Returns:
      True if successful or false if the server public key is wrong.
      Throws:
      CryptoException
    • cryptoAeadXChaCha20Poly1305IetfEncrypt

      public byte[] cryptoAeadXChaCha20Poly1305IetfEncrypt(byte[] m, byte[] ad, byte[] nPub, byte[] k)
      This function encrypts the given message m.
      Parameters:
      m - the message as byte array
      ad - the authentication tag
      nPub - the public nonce
      k - the key for encryption
      Returns:
      the encrypted message or null on failure
    • cryptoAeadXChaCha20Poly1305IetfDecrypt

      public byte[] cryptoAeadXChaCha20Poly1305IetfDecrypt(byte[] c, byte[] ad, byte[] nPub, byte[] k)
      This function decrypts the given ciphertext c.
      Parameters:
      c - the cipher text
      ad - the authentication tag
      nPub - the public nonce
      k - the key for encryption
      Returns:
      the decrypted message or null on failure
    • cryptoSignDetached

      public byte[] cryptoSignDetached(byte[] message, byte[] secretKey)
      Returns a signature for a message. This does not prepend the signature to the message.
      Parameters:
      message - The message to sign.
      secretKey - The secret key.
      Returns:
      the signature or null on failure
    • cryptoSignVerifyDetached

      public boolean cryptoSignVerifyDetached(byte[] signature, byte[] message, byte[] publicKey)
      Verifies that signature is valid for the message.
      Parameters:
      signature - The signature.
      message - The message.
      publicKey - The public key that signed the message.
      Returns:
      Returns true if the signature is valid for the message.
    • successful

      public boolean successful(int res)
      Evaluates the return value of a native sodium function call.
      Parameters:
      res - the result of the function call
      Returns:
      true if call was successful, otherwise false
    • getSodium

      public Sodium getSodium()