Class ChaCha20Poly1305Encryption
java.lang.Object
io.fluxzero.common.encryption.ChaCha20Poly1305Encryption
- All Implemented Interfaces:
Encryption
Implementation of
Encryption using the ChaCha20-Poly1305 authenticated encryption algorithm.
This class provides secure, fast, and cross-platform compatible encryption and decryption for String
values using the ChaCha20-Poly1305 AEAD cipher. The ChaCha20 stream cipher paired with the Poly1305 MAC
ensures both confidentiality and integrity.
Key Characteristics:
- Uses a 256-bit symmetric key.
- Each encryption operation uses a unique 96-bit (12-byte) random nonce.
- The output is Base64-encoded and formatted as:
nonce:ciphertext:tag - Compatible with other ChaCha20-Poly1305 implementations across platforms.
- Tag length is fixed at 16 bytes.
Usage:
Instances can be constructed directly or viaDefaultEncryption, which wraps this implementation
and adds encryption metadata (e.g., algorithm prefix).
Notes:
- This implementation is stateless; thread-safe for concurrent use.
isEncrypted(String)is unsupported and always throwsUnsupportedOperationException. UseDefaultEncryptioninstead for format-aware encryption detection.
This implementation is adapted from: https://github.com/java-crypto/cross_platform_crypto.
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionDecrypts the given encrypted value.Encrypts the given plain-text value using the configured encryption algorithm and key.Returns the name or identifier of the encryption algorithm used.Returns the encryption key used by this implementation.booleanisEncrypted(String value) Returnstrueif the given value is considered encrypted by this implementation.
-
Field Details
-
ALGORITHM
- See Also:
-
-
Constructor Details
-
ChaCha20Poly1305Encryption
public ChaCha20Poly1305Encryption() -
ChaCha20Poly1305Encryption
-
-
Method Details
-
encrypt
Description copied from interface:EncryptionEncrypts the given plain-text value using the configured encryption algorithm and key.- Specified by:
encryptin interfaceEncryption- Parameters:
value- the plain-text value to encrypt- Returns:
- the encrypted form of the input value
-
decrypt
Description copied from interface:EncryptionDecrypts the given encrypted value.This method assumes that the input is a properly formatted encrypted value produced by the corresponding
Encryption.encrypt(String)method.- Specified by:
decryptin interfaceEncryption- Parameters:
value- the encrypted string to decrypt- Returns:
- the decrypted plain-text value
-
getAlgorithm
Description copied from interface:EncryptionReturns the name or identifier of the encryption algorithm used.- Specified by:
getAlgorithmin interfaceEncryption- Returns:
- the name of the algorithm (e.g., "AES", "RSA", "noop")
-
getEncryptionKey
Description copied from interface:EncryptionReturns the encryption key used by this implementation.- Specified by:
getEncryptionKeyin interfaceEncryption- Returns:
- the configured key for encryption/decryption, usually a secret or secret reference
-
isEncrypted
Description copied from interface:EncryptionReturnstrueif the given value is considered encrypted by this implementation.Useful for avoiding double-encryption or determining if decryption is required.
- Specified by:
isEncryptedin interfaceEncryption- Parameters:
value- the value to check- Returns:
trueif the value appears to be encrypted,falseotherwise
-