EncryptionService
Name | EncryptionService |
Class Path |
|
Versions | 9 10 11 |
Overview
The EncryptionService
class offers cryptographic operations for encrypting, decrypting, hashing, signing, verifying, and securely handling passwords using various algorithms.
Available Methods
Encryption & Decryption
String encrypt(String cipher, String data, String encodingFormat)
Encrypts the given data using the specified cipher.
Parameters:
cipher
(String
): Encryption algorithm (e.g.,"AES"
,"RSA"
).data
(String
): The plaintext data to be encrypted.encodingFormat
(String
): Output encoding format ("base64"
,"hex"
,"y64"
).
Returns:
String
: The encrypted data in the specified encoding format.
String decrypt(String cipher, String encrypted, String decodingFormat)
Decrypts the given encrypted data.
Parameters:
cipher
(String
): Decryption algorithm.encrypted
(String
): Encrypted data.decodingFormat
(String
): Format of the encrypted data.
Returns:
String
: The decrypted plaintext.
Hashing
byte[] hash(String hashAlg, byte[] message)
Computes a cryptographic hash for the given byte array.
Parameters:
hashAlg
(String
): Hashing algorithm (e.g.,"SHA-256"
).message
(byte[]
): Data to hash.
Returns:
byte[]
: The computed hash.
String hash(String hashAlg, String message, String encodingFormat)
Computes a hash for a given string and encodes it.
Parameters:
hashAlg
(String
): Hashing algorithm.message
(String
): Data to hash.encodingFormat
(String
): Encoding format ("base64"
,"hex"
,"y64"
).
Returns:
String
: Encoded hash.
HMAC (Hashed Message Authentication Code)
String generateHMac(String cipher, String data, String decodingFormat, String encodingFormat)
Generates an HMAC using the specified algorithm.
Parameters:
cipher
(String
): HMAC algorithm ("HmacSHA256"
,"HmacMD5"
, etc.).data
(String
): Data to be signed.decodingFormat
(String
): Encoding format of input.encodingFormat
(String
): Encoding format of output.
Returns:
String
: HMAC value.
Salted Hashing
String saltedHash(String algorithm, String password, String salt)
Computes a salted hash using the specified algorithm.
Parameters:
algorithm
(String
):"SHA"
or"SHA256"
.password
(String
): User password.salt
(String
): Salt value.
Returns:
String
: Salted hash in base64.
boolean verifySaltedPassword(String password, String salt, String digest)
Verifies if a password matches the salted hash.
Parameters:
password
(String
): Plaintext password.salt
(String
): Salt value.digest
(String
): Hashed password.
Returns:
boolean
:true
if password is valid, otherwisefalse
.
String retrieveSalt(String digest)
Extracts the salt from a salted hash.
Parameters:
digest
(String
): Salted hash.
Returns:
String
: Extracted salt.
String generateSalt(int len)
Generates a cryptographic salt of a given length.
Parameters:
len
(int
): Desired salt length.
Returns:
String
: Base64-encoded salt.
Asymmetric Cryptography (RSA)
void setPKCS8EncodedKeySpec(String key)
Loads a private key in PKCS8 format.
Parameters:
key
(String
): Base64-encoded private key in PKCS8 format.
void setRsaPublicKey(String key)
Loads a public key in X.509 format.
Parameters:
key
(String
): Base64-encoded public key in X.509 format.
void setRsaPkcs8PublicKey(String key)
Loads a public key in PKCS8 format.
Parameters:
key
(String
): Base64-encoded public key in PKCS8 format.
Digital Signatures
String sign(String algorithm, String data, String decodingFormat, String encodingFormat)
Signs a message using a private key.
Parameters:
algorithm
(String
): Signature algorithm (e.g.,"SHA256withRSA"
).data
(String
): Message to sign.decodingFormat
(String
): Encoding format of the input.encodingFormat
(String
): Encoding format of the signature.
Returns:
String
: Digital signature.
boolean verify(String algorithm, String data, String signature, String decodingFormat)
Verifies a digital signature.
Parameters:
algorithm
(String
): Signature algorithm.data
(String
): Original message.signature
(String
): Digital signature.decodingFormat
(String
): Encoding format of the signature.
Returns:
boolean
:true
if valid,false
otherwise.
boolean verifyWithPublicKey(String algorithm, String publicKey, String data, String signature, String decodingFormat)
Verifies a signature using a public key.
Parameters:
algorithm
(String
): Signature algorithm.publicKey
(String
): Base64-encoded public key.data
(String
): Original message.signature
(String
): Digital signature.decodingFormat
(String
): Encoding format of the signature.
Returns:
boolean
:true
if valid,false
otherwise.
boolean verifyWithCertificate(String algorithm, String certificate, String data, String signature, String decodingFormat)
Verifies a signature using an X.509 certificate.
Parameters:
algorithm
(String
): Signature algorithm.certificate
(String
): X.509 certificate in Base64.data
(String
): Original message.signature
(String
): Digital signature.decodingFormat
(String
): Encoding format of the signature.
Returns:
boolean
:true
if valid,false
otherwise.
Password Hashing
String bcryptHash(String password, String salt, int rounds, boolean php)
Hashes a password using bcrypt.
Parameters:
password
(String
): Plaintext password.salt
(String
): Salt for hashing.rounds
(int
): Cost factor.php
(boolean
): Iftrue
, outputs PHP-compatible bcrypt hash.
Returns:
String
: Bcrypt hash.
Argon2 Hashing
String argon2Hash(String password, int saltLength, int parallelism, int resultLength, int memorySizeInKb, int iterations)
Hashes a password using Argon2id.
boolean argon2Verify(String password, String encodedHash)
Verifies an Argon2 hash.
PBKDF2 Hashing
String pbkdf2Hash(...)
Hashes a password using PBKDF2.
boolean pbkdf2Verify(...)
Verifies a PBKDF2 hash.
This service provides a robust suite of cryptographic functions for secure data processing.