Class Utils

java.lang.Object
com.gherynos.libsaltpack.Utils

public class Utils extends Object
Utilities class.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    baseXblockSize(String alphabet, int size)
    Returns the number of required characters to represent in BaseX, for a given alphabet, size characters.
    static byte[]
    baseXdecode(String data, String alphabet)
    Decodes the string from BaseX and the given alphabet.
    static String
    baseXencode(byte[] data, int size, String alphabet)
    Encodes the data in BaseX using the given alphabet.
    static String
    baseXencode(byte[] data, String alphabet)
    Encodes the data in BaseX using the given alphabet.
    static String
    binToHex(byte[] bin)
    Binary to hexadecimal encoding.
    static byte[]
    deriveKeyFromPassword(long keySize, char[] password, byte[] salt, long opsLimit, long memLimit)
    Wrapper for the `crypto_pwhash` function from `libsodium`.
    static byte[]
    derivePublickey(byte[] secretkey)
    Derives the public key from a private key.
    static void
    generateKeypair(byte[] publickey, byte[] secretkey)
    Generates an encryption keypair using `libsodium`.
    static byte[]
    Generates some random bytes using `libsodium`.
    static void
    generateSignKeypair(byte[] publickey, byte[] secretkey)
    Generates a signing keypair using `libsodium`.
    static byte[]
    Hexadecial to binary encoding.

    Methods inherited from class java.lang.Object

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

    • generateKeypair

      public static void generateKeypair(byte[] publickey, byte[] secretkey) throws SaltpackException
      Generates an encryption keypair using `libsodium`.
      Parameters:
      publickey - the array that will be populated with the public key.
      secretkey - the array that will be populated with the private key.
      Throws:
      SaltpackException
    • generateSignKeypair

      public static void generateSignKeypair(byte[] publickey, byte[] secretkey) throws SaltpackException
      Generates a signing keypair using `libsodium`.
      Parameters:
      publickey - the array that will be populated with the public key.
      secretkey - the array that will be populated with the private key.
      Throws:
      SaltpackException
    • derivePublickey

      public static byte[] derivePublickey(byte[] secretkey) throws SaltpackException
      Derives the public key from a private key.
      Parameters:
      secretkey - the private key.
      Returns:
      the public key.
      Throws:
      SaltpackException
    • baseXblockSize

      public static int baseXblockSize(String alphabet, int size) throws SaltpackException
      Returns the number of required characters to represent in BaseX, for a given alphabet, size characters.
      Parameters:
      alphabet - the alphabet for the BaseX encoding.
      size - the size of the data to represent.
      Returns:
      the number of characters required to encode size characters.
      Throws:
      SaltpackException
    • baseXencode

      public static String baseXencode(byte[] data, String alphabet) throws SaltpackException
      Encodes the data in BaseX using the given alphabet.
      Parameters:
      data - the data to encode.
      alphabet - the alphabet for the BaseX encoding.
      Returns:
      the encoded string.
      Throws:
      SaltpackException
    • baseXencode

      public static String baseXencode(byte[] data, int size, String alphabet) throws SaltpackException
      Encodes the data in BaseX using the given alphabet.
      Parameters:
      data - the data to encode.
      size - the number of characters to encode from data.
      alphabet - the alphabet for the BaseX encoding.
      Returns:
      the encoded string.
      Throws:
      SaltpackException
    • baseXdecode

      public static byte[] baseXdecode(String data, String alphabet) throws SaltpackException
      Decodes the string from BaseX and the given alphabet.
      Parameters:
      data - data the string to decode.
      alphabet - the alphabet for the BaseX decoding.
      Returns:
      the decoded data.
      Throws:
      SaltpackException
    • hexToBin

      public static byte[] hexToBin(String hex) throws SaltpackException
      Hexadecial to binary encoding.
      Parameters:
      hex - the hexadecimal string.
      Returns:
      the binary data.
      Throws:
      SaltpackException
    • binToHex

      public static String binToHex(byte[] bin) throws SaltpackException
      Binary to hexadecimal encoding.
      Parameters:
      bin - the binary data.
      Returns:
      the hexadecimal string.
      Throws:
      SaltpackException
    • generateRandomBytes

      public static byte[] generateRandomBytes(long size) throws SaltpackException
      Generates some random bytes using `libsodium`.
      Parameters:
      size - the amount of bytes to generate.
      Returns:
      the random bytes.
      Throws:
      SaltpackException
    • deriveKeyFromPassword

      public static byte[] deriveKeyFromPassword(long keySize, char[] password, byte[] salt, long opsLimit, long memLimit) throws SaltpackException
      Wrapper for the `crypto_pwhash` function from `libsodium`.
      Parameters:
      keySize - the size of the key.
      password - the password used to derive the key.
      salt - the salt used to derive the key.
      opsLimit - the maximum amount of computations to perform.
      memLimit - the maximum amount of RAM that the function will use, in bytes.
      Returns:
      the derived key.
      Throws:
      SaltpackException