Package org.moera.lib.crypto
Class CryptoUtil
java.lang.Object
org.moera.lib.crypto.CryptoUtil
Utility class that provides cryptographic functions for key generation,
signature creation, fingerprinting, verification, and mnemonic handling.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
digest
(byte[] fingerprint) Computes the cryptographic digest of the provided fingerprint using the standard digest algorithm (Rules.DIGEST_ALGORITHM
).static List<byte[]>
digest
(Collection<byte[]> fingerprint) Hashes each byte array in the provided collection using the standard cryptographic digest algorithm (Rules.DIGEST_ALGORITHM
).static byte[]
fingerprint
(String text) Generates a fingerprint of the given text.static byte[]
fingerprint
(Fingerprint fingerprint, FieldWithSchema[] schema) Computes the byte array representation of a given fingerprint using the provided schema.static KeyPair
Generates an elliptic curve key pair using the standard EC curve used in Moera (Rules.EC_CURVE
) and a secure random generator.static MnemonicKey
Generates a newMnemonicKey
containing a securely generated mnemonic phrase, corresponding secret in base64 format, and a public key derived from the mnemonic.static ECPrivateKey
mnemonicToPrivateKey
(String mnemonic) Converts a mnemonic phrase into an elliptic curve private key.static byte[]
rawPrivateKey
(ECPrivateKey privateKey) Converts the providedECPrivateKey
into its raw byte representation.static byte[]
rawPublicKey
(ECPublicKey publicKey) Converts anECPublicKey
to its raw representation in a byte array format.static ECPrivateKey
rawToPrivateKey
(byte[] rawKey) Converts a raw private key represented as a byte array into anECPrivateKey
instance using the standard Moera elliptic curve (Rules.EC_CURVE
) parameters.static ECPublicKey
rawToPublicKey
(byte[] rawKey) Converts a raw byte array representation of an EC public key into anECPublicKey
object.static RestoredFingerprint
restoreFingerprint
(byte[] bytes, Function<Integer, FieldWithSchema[]> schemaProvider) Restores a fingerprint object from the given byte array using the provided schema.static RestoredFingerprint
restoreFingerprint
(byte[] bytes, FieldWithSchema[] schema) Restores a fingerprint object from the given byte array using the provided schema.static String
secretToMnemonic
(String secret) Converts a Base64-encoded secret into a mnemonic phrase using the BIP39 standard.static byte[]
sign
(byte[] fingerprint, byte[] privateKey) Signs the given fingerprint using the specified private key.static byte[]
sign
(byte[] fingerprint, ECPrivateKey privateKey) Signs the given fingerprint data using the specified private key.static String
token()
Generates a secure random token encoded in Base64 URL format.static boolean
verifySignature
(byte[] fingerprint, byte[] signature, byte[] publicKey) Verifies a digital signature using a provided fingerprint, signature, and public key.static boolean
verifySignature
(byte[] fingerprint, byte[] signature, ECPublicKey publicKey) Verifies a digital signature using a provided fingerprint, signature, and public key.
-
Constructor Details
-
CryptoUtil
public CryptoUtil()
-
-
Method Details
-
generateKey
Generates an elliptic curve key pair using the standard EC curve used in Moera (Rules.EC_CURVE
) and a secure random generator. The generated key pair consists of a private key and a public key.- Returns:
- a
KeyPair
object containing the generated elliptic curve keys - Throws:
CryptoException
- if a cryptographic error occurs during key pair generation
-
generateMnemonicKey
Generates a newMnemonicKey
containing a securely generated mnemonic phrase, corresponding secret in base64 format, and a public key derived from the mnemonic. The mnemonic and public key are generated using the elliptic curve specified byRules.EC_CURVE
.- Returns:
- A new instance of
MnemonicKey
containing the secret, mnemonic phrase, and derived public key. - Throws:
CryptoException
- if any cryptographic operation fails during the key generation process.
-
secretToMnemonic
Converts a Base64-encoded secret into a mnemonic phrase using the BIP39 standard.The method decodes the provided Base64 secret into entropy bytes, then generates a mnemonic phrase using the English word list. The resulting mnemonic is a human-readable sequence of words corresponding to the entropy.
- Parameters:
secret
- the Base64-encoded secret to be transformed into a mnemonic phrase- Returns:
- the mnemonic phrase representing the decoded entropy
-
mnemonicToPrivateKey
Converts a mnemonic phrase into an elliptic curve private key. The mnemonic is processed to generate a seed, which is used to derive the private key.- Parameters:
mnemonic
- A valid mnemonic phrase used to generate the private key.- Returns:
- The derived elliptic curve private key.
- Throws:
CryptoException
- If a cryptographic error occurs during key generation.
-
rawPublicKey
Converts anECPublicKey
to its raw representation in a byte array format. The raw representation includes the X and Y affine coordinates of the key, encoded as unsigned integers.- Parameters:
publicKey
- theECPublicKey
to be converted to its raw byte array format- Returns:
- a byte array containing the raw representation of the public key
-
rawToPublicKey
Converts a raw byte array representation of an EC public key into anECPublicKey
object.- Parameters:
rawKey
- the raw byte array representing the EC public key. It is expected to contain the concatenated X and Y coordinates of the elliptic curve point.- Returns:
- the
ECPublicKey
object created from the provided raw byte array. - Throws:
CryptoException
- if the key cannot be generated due to invalid input, missing algorithm/provider, or an invalid key specification.
-
rawPrivateKey
Converts the providedECPrivateKey
into its raw byte representation.- Parameters:
privateKey
- theECPrivateKey
instance to be converted into a raw byte array- Returns:
- a byte array representing the raw form of the given private key
-
rawToPrivateKey
Converts a raw private key represented as a byte array into anECPrivateKey
instance using the standard Moera elliptic curve (Rules.EC_CURVE
) parameters.- Parameters:
rawKey
- the raw private key as a byte array- Returns:
- an
ECPrivateKey
instance corresponding to the provided raw key - Throws:
CryptoException
- if an error occurs during the conversion process, such as invalid key specification or issues with the cryptographic provider
-
token
Generates a secure random token encoded in Base64 URL format.- Returns:
- Base64 URL-encoded token string.
- Throws:
CryptoException
- If the specified cryptographic algorithm or provider is not available.
-
fingerprint
Computes the byte array representation of a given fingerprint using the provided schema.- Parameters:
fingerprint
- theFingerprint
object to be converted into bytesschema
- the schema that describes the structure of the fingerprint- Returns:
- a byte array representing the serialized fingerprint
-
fingerprint
Generates a fingerprint of the given text.- Parameters:
text
- the input text to be fingerprinted- Returns:
- a byte array representing the fingerprint of the input text
- Throws:
CryptoException
- if an error occurs during fingerprint generation
-
restoreFingerprint
Restores a fingerprint object from the given byte array using the provided schema.- Parameters:
bytes
- the byte array containing the serialized fingerprint dataschema
- the schema of the fingerprint- Returns:
- a
RestoredFingerprint
object containing the reconstructed fingerprint and the number of bytes remaining in the input array
-
restoreFingerprint
public static RestoredFingerprint restoreFingerprint(byte[] bytes, Function<Integer, FieldWithSchema[]> schemaProvider) Restores a fingerprint object from the given byte array using the provided schema.- Parameters:
bytes
- the byte array containing the serialized representation of the fingerprintschemaProvider
- a function that provides the schema for a given fingerprint version- Returns:
- a
RestoredFingerprint
object containing the reconstructed fingerprint and the number of bytes remaining in the input array
-
digest
public static byte[] digest(byte[] fingerprint) Computes the cryptographic digest of the provided fingerprint using the standard digest algorithm (Rules.DIGEST_ALGORITHM
).- Parameters:
fingerprint
- the byte array containing the fingerprint to be processed- Returns:
- a byte array containing the computed digest
-
digest
Hashes each byte array in the provided collection using the standard cryptographic digest algorithm (Rules.DIGEST_ALGORITHM
).- Parameters:
fingerprint
- a collection of byte arrays to be processed; each array will be hashed individually.- Returns:
- a list of hashed byte arrays corresponding to the inputs, or
null
if the input isnull
.
-
sign
public static byte[] sign(byte[] fingerprint, byte[] privateKey) Signs the given fingerprint using the specified private key.- Parameters:
fingerprint
- the data to be signed, represented as a byte arrayprivateKey
- the private key to be used for signing the fingerprint, in the raw format- Returns:
- the resulting signature as a byte array
- Throws:
CryptoException
- if an error occurs during the signing process
-
sign
Signs the given fingerprint data using the specified private key.- Parameters:
fingerprint
- the data to be signed, represented as a byte array.privateKey
- the EC private key to be used for generating the signature.- Returns:
- the resulting signature as a byte array
- Throws:
CryptoException
- if an error occurs during the signing process
-
verifySignature
public static boolean verifySignature(byte[] fingerprint, byte[] signature, byte[] publicKey) Verifies a digital signature using a provided fingerprint, signature, and public key.- Parameters:
fingerprint
- the byte array representation of the data (fingerprint) to be validatedsignature
- the byte array representing the digital signature to verifypublicKey
- the byte array of the raw public key used for verification- Returns:
true
if the signature is valid and matches the fingerprint using the public key,false
otherwise- Throws:
CryptoException
- if any cryptographic error occurs during verification
-
verifySignature
Verifies a digital signature using a provided fingerprint, signature, and public key.- Parameters:
fingerprint
- the byte array representation of the data (fingerprint) to be validatedsignature
- the byte array representing the digital signature to verifypublicKey
- the ECDSA public key used to verify the signature- Returns:
true
if the signature is valid and matches the fingerprint using the public key,false
otherwise- Throws:
CryptoException
- if any cryptographic error occurs during verification
-