added message digest module
All checks were successful
Build, test and publish the Quarkus libraries / build (push) Successful in 10m26s
All checks were successful
Build, test and publish the Quarkus libraries / build (push) Successful in 10m26s
This commit is contained in:
parent
d6f92b22f9
commit
fd6e7790a6
6 changed files with 231 additions and 0 deletions
|
@ -0,0 +1,12 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.digest;
|
||||
|
||||
import io.smallrye.config.ConfigMapping;
|
||||
import io.smallrye.config.WithDefault;
|
||||
|
||||
@ConfigMapping(prefix = "phoenix.message-digest")
|
||||
public interface MessageDigestConfiguration {
|
||||
|
||||
@WithDefault("SHA-256")
|
||||
String defaultAlgorithm();
|
||||
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.digest;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
/**
|
||||
* Service for creating and using MessageDigest instances.
|
||||
* <p>
|
||||
* To configure the default algorithm, see {@link MessageDigestConfiguration}.
|
||||
*/
|
||||
public interface MessageDigestService {
|
||||
|
||||
/**
|
||||
* Get a new MessageDigest instance using the configured algorithm.
|
||||
*
|
||||
* @return The MessageDigest instance
|
||||
*/
|
||||
MessageDigest getInstance();
|
||||
|
||||
/**
|
||||
* Get a new MessageDigest instance using the specified algorithm.
|
||||
*
|
||||
* @param algorithm the algorithm to use
|
||||
* @return The MessageDigest instance
|
||||
*/
|
||||
MessageDigest getInstance(String algorithm);
|
||||
|
||||
/**
|
||||
* Digests the input using the default charset (UTF-8) and
|
||||
* the configured algorithm.
|
||||
*
|
||||
* @param input the input to digest
|
||||
* @return the digest
|
||||
*/
|
||||
byte[] digest(String input);
|
||||
|
||||
/**
|
||||
* Digests the input using the specified charset and
|
||||
* the configured algorithm.
|
||||
*
|
||||
* @param input the input to digest
|
||||
* @param charset the charset to use
|
||||
* @return the digest
|
||||
*/
|
||||
byte[] digest(String input, Charset charset);
|
||||
|
||||
/**
|
||||
* Digests the input using the specified charset and algorithm.
|
||||
*
|
||||
* @param input the input to digest
|
||||
* @param charset the charset to use
|
||||
* @param algorithm the algorithm to use
|
||||
* @return the digest
|
||||
*/
|
||||
byte[] digest(String input, Charset charset, String algorithm);
|
||||
|
||||
/**
|
||||
* Digests the input using the specified charset and message digest.
|
||||
*
|
||||
* @param input the input to digest
|
||||
* @param charset the charset to use
|
||||
* @param messageDigest the message digest to use
|
||||
* @return the digest
|
||||
*/
|
||||
byte[] digest(String input, Charset charset, MessageDigest messageDigest);
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.digest;
|
||||
|
||||
import io.quarkus.arc.DefaultBean;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
@DefaultBean
|
||||
@ApplicationScoped
|
||||
class MessageDigestServiceImpl implements MessageDigestService {
|
||||
|
||||
private final MessageDigestConfiguration configuration;
|
||||
|
||||
MessageDigestServiceImpl(MessageDigestConfiguration configuration) {
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageDigest getInstance() {
|
||||
return getInstance(configuration.defaultAlgorithm());
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageDigest getInstance(String algorithm) {
|
||||
try {
|
||||
return MessageDigest.getInstance(algorithm);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalArgumentException("Unknown algorithm: " + algorithm, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] digest(String input) {
|
||||
return digest(input, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] digest(String input, Charset charset) {
|
||||
return digest(input, charset, configuration.defaultAlgorithm());
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] digest(String input, Charset charset, String algorithm) {
|
||||
return digest(input, charset, getInstance(algorithm));
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] digest(String input, Charset charset, MessageDigest messageDigest) {
|
||||
var bytes = input.getBytes(StandardCharsets.UTF_8);
|
||||
return messageDigest.digest(bytes);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue