Class CryptoAsymmetricService
java.lang.Object
com.eurodyn.qlack.fuse.crypto.service.CryptoAsymmetricService
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected String
convertKeyToPEM
(KeyPair keyPair, String keyType) createKeyPair
(CreateKeyPairDTO createKeyPairRequest) Generates a new keypair consisting of a public key and a private key.byte[]
Decrypts a payload encrypted with a public key using the private key.byte[]
Encrypts a payload.getSecureRandomAlgorithm
(String secureRandomAlgorithm) Finds the requested secure random algorithm or returns the default one.byte[]
keyToByteArray
(@NotNull Key key) pemToPrivateKey
(String privateKey, String algorithm) Converts a text-based private key (in PEM format) toPrivateKey
.pemToPublicKey
(String publicKey, String algorithm) Converts a text-based public key (in PEM format) toPublicKey
.privateKeyFromByteArray
(@jakarta.validation.constraints.NotNull byte[] key, @NotNull String keyAlgorithm, String keyProvider) Converts a byte array holding a private key in DER format to a private key.privateKeyToPEM
(KeyPair keyPair) Converts a private key to string in PEM format.publicKeyFromByteArray
(@jakarta.validation.constraints.NotNull byte[] key, @NotNull String keyAlgorithm, String keyProvider) publicKeyToPEM
(KeyPair keyPair) Converts a public key to PEM format.byte[]
Signs a message with a private key.byte[]
sign
(String privateKeyPEM, InputStream payload, String signatureAlgorithm, String keyAlgorithm) Signs a message using anInputStream
.boolean
verifySignature
(String publicKeyPEM, byte[] payload, String signature, String signatureAlgorithm, String keyAlgorithm) Verifies a signature.boolean
verifySignature
(String publicKeyPEM, InputStream payload, String signature, String signatureAlgorithm, String keyAlgorithm) Verifies a signature using anInputStream
.
-
Constructor Details
-
CryptoAsymmetricService
public CryptoAsymmetricService()
-
-
Method Details
-
convertKeyToPEM
- Throws:
IOException
-
createKeyPair
public KeyPair createKeyPair(CreateKeyPairDTO createKeyPairRequest) throws NoSuchAlgorithmException, NoSuchProviderException Generates a new keypair consisting of a public key and a private key.- Parameters:
createKeyPairRequest
- The details of the keypair to create- Returns:
- the generated keypair
- Throws:
NoSuchAlgorithmException
- thrown when no algorithm is found for encryptionNoSuchProviderException
-
keyToByteArray
-
privateKeyFromByteArray
public PrivateKey privateKeyFromByteArray(@NotNull @jakarta.validation.constraints.NotNull byte[] key, @NotNull @NotNull String keyAlgorithm, String keyProvider) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException Converts a byte array holding a private key in DER format to a private key. -
publicKeyFromByteArray
public PublicKey publicKeyFromByteArray(@NotNull @jakarta.validation.constraints.NotNull byte[] key, @NotNull @NotNull String keyAlgorithm, String keyProvider) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException -
publicKeyToPEM
Converts a public key to PEM format.- Parameters:
keyPair
- The keypair containing the public key- Returns:
- the generated PEM format
- Throws:
IOException
- thrown when something unexpected happens
-
privateKeyToPEM
Converts a private key to string in PEM format.- Parameters:
keyPair
- the keypair containing the private key to convert- Returns:
- the generated PEM format
- Throws:
IOException
- thrown when generating PEM
-
pemToPublicKey
public PublicKey pemToPublicKey(String publicKey, String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException Converts a text-based public key (in PEM format) toPublicKey
.- Parameters:
publicKey
- the public key in PEM format to convertalgorithm
- the security algorithm with which this key was generated- Returns:
- the generated PEM format
- Throws:
NoSuchAlgorithmException
- thrown when no algorithm is found for encryptionInvalidKeySpecException
- thrown when the provided key is invalid
-
pemToPrivateKey
public PrivateKey pemToPrivateKey(String privateKey, String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException Converts a text-based private key (in PEM format) toPrivateKey
.- Parameters:
privateKey
- the private key in PEM format to convertalgorithm
- the security algorithm with which this key was generated- Returns:
- the generated PEM format
- Throws:
NoSuchAlgorithmException
- thrown when no algorithm is found for encryptionInvalidKeySpecException
- thrown when the provided key is invalid
-
sign
public byte[] sign(String privateKeyPEM, byte[] payload, String signatureAlgorithm, String keyAlgorithm) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException Signs a message with a private key.- Parameters:
privateKeyPEM
- the private key to sign with in PEM formatpayload
- the payload to signsignatureAlgorithm
- the signature algorithm to use, e.g. SHA256withRSAkeyAlgorithm
- the algorithm with which the private key was generated, e.g. RSA- Returns:
- the signature in bytes
- Throws:
NoSuchAlgorithmException
- thrown when no algorithm is found for encryptionInvalidKeySpecException
- thrown when the provided key is invalidInvalidKeyException
- thrown when the provided key is invalidSignatureException
- thrown when something unexpected occurs during signing
-
sign
public byte[] sign(String privateKeyPEM, InputStream payload, String signatureAlgorithm, String keyAlgorithm) throws IOException, NoSuchAlgorithmException, SignatureException, InvalidKeySpecException, InvalidKeyException Signs a message using anInputStream
.- Parameters:
privateKeyPEM
- the private key to sign with in PEM formatpayload
- the data to signsignatureAlgorithm
- the signature algorithm to use, e.g. SHA256withRSAkeyAlgorithm
- the algorithm with which the private key was generated, e.g. RSA- Returns:
- the signature in bytes
- Throws:
IOException
- thrown when something unexpected happensNoSuchAlgorithmException
- thrown when no algorithm is found for encryptionInvalidKeySpecException
- thrown when the provided key is invalidInvalidKeyException
- thrown when the provided key is invalidSignatureException
- thrown when something unexpected occurs during signing
-
verifySignature
public boolean verifySignature(String publicKeyPEM, byte[] payload, String signature, String signatureAlgorithm, String keyAlgorithm) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException Verifies a signature.- Parameters:
publicKeyPEM
- the public key to verify the signature withpayload
- the signed contentsignature
- the signature to verify in Base64 formatsignatureAlgorithm
- the algorithm with which the signature was created, e.g. SHA256withRSAkeyAlgorithm
- the algorithm with which the key was generated, e.g. RSA- Returns:
- true if the signature is verified, false if it is not
- Throws:
NoSuchAlgorithmException
- thrown when no algorithm is found for encryptionInvalidKeySpecException
- thrown when the provided key is invalidInvalidKeyException
- thrown when the provided key is invalidSignatureException
- thrown when something unexpected occurs during signing
-
verifySignature
public boolean verifySignature(String publicKeyPEM, InputStream payload, String signature, String signatureAlgorithm, String keyAlgorithm) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException, IOException Verifies a signature using anInputStream
.- Parameters:
publicKeyPEM
- the public key to verify the signature withpayload
- the signed contentsignature
- the signature to verify in Base64 formatsignatureAlgorithm
- the algorithm with which the signature was created, e.g. SHA256withRSAkeyAlgorithm
- the algorithm with which the key was generated, e.g. RSA- Returns:
- true if the signature is verified, false if it is not
- Throws:
IOException
- thrown when something unexpected happensNoSuchAlgorithmException
- thrown when no algorithm is found for encryptionInvalidKeySpecException
- thrown when the provided key is invalidInvalidKeyException
- thrown when the provided key is invalidSignatureException
- thrown when something unexpected occurs during signing
-
encrypt
public byte[] encrypt(String publicKeyPEM, byte[] payload, String cipherFactory, String keyAlgorithm) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException Encrypts a payload.- Parameters:
publicKeyPEM
- the public key to encrypt withpayload
- the payload to encryptcipherFactory
- the factory for the encryption cipher to use, e.g. RSA/ECB/PKCS1PaddingkeyAlgorithm
- the algorithm with which the public key was created, e.g. RSA- Returns:
- the encrypted key in bytes
- Throws:
NoSuchPaddingException
- thrown when the provided cipherFactory is not validNoSuchAlgorithmException
- thrown when no algorithm is found for encryptionInvalidKeySpecException
- thrown when the provided key is invalidInvalidKeyException
- thrown when the provided key is invalidBadPaddingException
- thrown when the provided cipherFactory is not validIllegalBlockSizeException
- thrown when the provided cipherFactory is not valid
-
decrypt
public byte[] decrypt(String privateKeyPEM, byte[] payload, String cipherFactory, String keyAlgorithm) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException Decrypts a payload encrypted with a public key using the private key.- Parameters:
privateKeyPEM
- the private key to decrypt withpayload
- the payload to decryptcipherFactory
- the factory for the decryption cipher to use, e.g. RSA/ECB/PKCS1PaddingkeyAlgorithm
- the algorithm with which the private key was created, e.g. RSA- Returns:
- the decrypted key in bytes
- Throws:
NoSuchPaddingException
- thrown when the provided cipherFactory is not validNoSuchAlgorithmException
- thrown when no algorithm is found for encryptionInvalidKeySpecException
- thrown when the provided key is invalidInvalidKeyException
- thrown when the provided key is invalidBadPaddingException
- thrown when the provided cipherFactory is not validIllegalBlockSizeException
- thrown when the provided cipherFactory is not valid
-
getSecureRandomAlgorithm
Finds the requested secure random algorithm or returns the default one.- Parameters:
secureRandomAlgorithm
- the secure random algorithm to find.
-