Merge pull request 'added message digest module' (#6) from dev into main
All checks were successful
Build, test and publish the Quarkus libraries / build (push) Successful in 10m39s
All checks were successful
Build, test and publish the Quarkus libraries / build (push) Successful in 10m39s
Reviewed-on: phoenix/quarkus-commons#6
This commit is contained in:
commit
7d01a51fc8
6 changed files with 231 additions and 0 deletions
1
pom.xml
1
pom.xml
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>quarkus-clock-service</module>
|
<module>quarkus-clock-service</module>
|
||||||
|
<module>quarkus-message-digest-service</module>
|
||||||
<module>quarkus-random-number-generator</module>
|
<module>quarkus-random-number-generator</module>
|
||||||
<module>quarkus-uuid-generator</module>
|
<module>quarkus-uuid-generator</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
16
quarkus-message-digest-service/pom.xml
Normal file
16
quarkus-message-digest-service/pom.xml
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>ch.phoenixtechnologies.quarkus</groupId>
|
||||||
|
<artifactId>quarkus-commons</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>quarkus-message-digest-service</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
</project>
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,81 @@
|
||||||
|
package ch.phoenixtechnologies.quarkus.commons.digest;
|
||||||
|
|
||||||
|
import io.quarkus.test.junit.QuarkusTest;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
|
||||||
|
|
||||||
|
@QuarkusTest
|
||||||
|
class MessageDigestServiceImplTest {
|
||||||
|
|
||||||
|
public static final String INPUT = "test";
|
||||||
|
public static final byte[] EXPECTED_SHA256_DIGEST = {-97, -122, -48, -127, -120, 76, 125, 101, -102, 47, -22, -96, -59, 90, -48, 21, -93, -65, 79, 27, 43, 11, -126, 44, -47, 93, 108, 21, -80, -16, 10, 8};
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
MessageDigestServiceImpl service;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getInstance() {
|
||||||
|
assertThat(service.getInstance())
|
||||||
|
.isInstanceOf(MessageDigest.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {"SHA-256", "SHA-512", "MD5"})
|
||||||
|
void getInstanceWithAlgorithm(String algorithm) {
|
||||||
|
assertThat(service.getInstance(algorithm))
|
||||||
|
.isInstanceOf(MessageDigest.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {"ADBC", "acvb", "null"})
|
||||||
|
void getInstanceWhenUnknownAlgorithm(String algorithm) {
|
||||||
|
assertThatThrownBy(() -> service.getInstance(algorithm))
|
||||||
|
.isInstanceOf(IllegalArgumentException.class)
|
||||||
|
.hasMessageContaining("Unknown algorithm: " + algorithm);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getInstanceWhenAlgorithmIsNull() {
|
||||||
|
assertThatThrownBy(() -> service.getInstance(null))
|
||||||
|
.isInstanceOf(NullPointerException.class)
|
||||||
|
.hasMessageContaining("null algorithm name");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void digest() {
|
||||||
|
assertThat(service.digest(INPUT))
|
||||||
|
.as("Digest should match expected value")
|
||||||
|
.isEqualTo(EXPECTED_SHA256_DIGEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void digestWithCharset() {
|
||||||
|
assertThat(service.digest(INPUT, StandardCharsets.UTF_8))
|
||||||
|
.as("Digest should match expected value")
|
||||||
|
.isEqualTo(EXPECTED_SHA256_DIGEST);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void digestWithCharsetAndAlgorithm() {
|
||||||
|
assertThat(service.digest(INPUT, StandardCharsets.UTF_8, "SHA-256"))
|
||||||
|
.as("Digest should match expected value")
|
||||||
|
.isEqualTo(EXPECTED_SHA256_DIGEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void digestWithCharsetAndMessageDigestInstance() {
|
||||||
|
var messageDigest = service.getInstance();
|
||||||
|
assertThat(service.digest(INPUT, StandardCharsets.UTF_8, messageDigest))
|
||||||
|
.as("Digest should match expected value")
|
||||||
|
.isEqualTo(EXPECTED_SHA256_DIGEST);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue