Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SHA3-512 #1594

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import gov.hhs.cdc.trustedintermediary.domainconnector.DomainConnectorConstructionException;
import gov.hhs.cdc.trustedintermediary.domainconnector.DomainResponseHelper;
import gov.hhs.cdc.trustedintermediary.domainconnector.UnableToReadOpenApiSpecificationException;
import gov.hhs.cdc.trustedintermediary.etor.utils.security.HashHelper;
import gov.hhs.cdc.trustedintermediary.external.apache.ApacheClient;
import gov.hhs.cdc.trustedintermediary.external.azure.AzureDatabaseCredentialsProvider;
import gov.hhs.cdc.trustedintermediary.external.azure.AzureSecrets;
Expand Down Expand Up @@ -85,6 +86,7 @@ private static void registerClasses() {
ApplicationContext.register(YamlCombiner.class, Jackson.getInstance());
ApplicationContext.register(OpenApi.class, OpenApi.getInstance());
ApplicationContext.register(HttpClient.class, ApacheClient.getInstance());
ApplicationContext.register(HashHelper.class, HashHelper.getInstance());
ApplicationContext.register(AuthEngine.class, JjwtEngine.getInstance());
ApplicationContext.register(Cache.class, KeyCache.getInstance());
ApplicationContext.register(DomainResponseHelper.class, DomainResponseHelper.getInstance());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadata;
import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadataMessageType;
import gov.hhs.cdc.trustedintermediary.etor.ruleengine.transformation.TransformationRuleEngine;
import gov.hhs.cdc.trustedintermediary.etor.utils.security.HashHelper;
import gov.hhs.cdc.trustedintermediary.wrappers.Logger;
import gov.hhs.cdc.trustedintermediary.wrappers.MetricMetadata;
import javax.inject.Inject;
Expand All @@ -18,6 +19,7 @@ public class SendOrderUseCase implements SendMessageUseCase<Order<?>> {
@Inject MetricMetadata metadata;
@Inject SendMessageHelper sendMessageHelper;
@Inject Logger logger;
@Inject HashHelper hashHelper;

private SendOrderUseCase() {}

Expand All @@ -29,10 +31,12 @@ public static SendOrderUseCase getInstance() {
public void convertAndSend(final Order<?> order, String receivedSubmissionId)
throws UnableToSendMessageException {

String hashedOrder = hashHelper.generateHash(order);

PartnerMetadata partnerMetadata =
new PartnerMetadata(
receivedSubmissionId,
String.valueOf(order.hashCode()),
hashedOrder,
PartnerMetadataMessageType.ORDER,
order.getSendingApplicationDetails(),
order.getSendingFacilityDetails(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadata;
import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadataMessageType;
import gov.hhs.cdc.trustedintermediary.etor.ruleengine.transformation.TransformationRuleEngine;
import gov.hhs.cdc.trustedintermediary.etor.utils.security.HashHelper;
import gov.hhs.cdc.trustedintermediary.wrappers.Logger;
import javax.inject.Inject;

Expand All @@ -20,6 +21,8 @@ public class SendResultUseCase implements SendMessageUseCase<Result<?>> {

@Inject Logger logger;

@Inject HashHelper hashHelper;

private SendResultUseCase() {}

public static SendResultUseCase getInstance() {
Expand All @@ -30,10 +33,12 @@ public static SendResultUseCase getInstance() {
public void convertAndSend(Result<?> result, String receivedSubmissionId)
throws UnableToSendMessageException {

String hashedResult = hashHelper.generateHash(result);

PartnerMetadata partnerMetadata =
new PartnerMetadata(
receivedSubmissionId,
String.valueOf(result.hashCode()),
hashedResult,
PartnerMetadataMessageType.RESULT,
result.getSendingApplicationDetails(),
result.getSendingFacilityDetails(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gov.hhs.cdc.trustedintermediary.etor.utils.security;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;

public class HashHelper implements SecureHash {
pluckyswan marked this conversation as resolved.
Show resolved Hide resolved

private static final HashHelper INSTANCE = new HashHelper();

public static HashHelper getInstance() {
return INSTANCE;
}

@Override
public String generateHash(Object input) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA3-512");
Dismissed Show dismissed Hide dismissed
byte[] objBytes = input.toString().getBytes(StandardCharsets.UTF_8);
byte[] hashBytes = digest.digest(objBytes);
return HexFormat.of().formatHex(hashBytes);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Algorithm does not exist!", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package gov.hhs.cdc.trustedintermediary.etor.utils.security;

import java.security.NoSuchAlgorithmException;

public interface SecureHash {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts about removing this interface? I feel that it could be removed since we aren't using any third-party libraries in the HashHelper implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel too strongly in either case. I think it could potentially be helpful if we wanted to use a third-party library in the future or expand on it.

String generateHash(Object hash) throws NoSuchAlgorithmException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadataExce
import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadataOrchestrator

import gov.hhs.cdc.trustedintermediary.etor.ruleengine.transformation.TransformationRuleEngine
import gov.hhs.cdc.trustedintermediary.etor.utils.security.HashHelper
import gov.hhs.cdc.trustedintermediary.wrappers.Logger
import gov.hhs.cdc.trustedintermediary.wrappers.MetricMetadata
import spock.lang.Specification
Expand All @@ -29,6 +30,7 @@ class SendOrderUseCaseTest extends Specification {
TestApplicationContext.register(SendMessageHelper, SendMessageHelper.getInstance())
TestApplicationContext.register(TransformationRuleEngine, mockEngine)
TestApplicationContext.register(OrderSender, mockSender)
TestApplicationContext.register(HashHelper, HashHelper.getInstance())
TestApplicationContext.register(Logger, mockLogger)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadata
import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadataException
import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadataOrchestrator
import gov.hhs.cdc.trustedintermediary.etor.ruleengine.transformation.TransformationRuleEngine
import gov.hhs.cdc.trustedintermediary.etor.utils.security.HashHelper
import gov.hhs.cdc.trustedintermediary.wrappers.Logger
import gov.hhs.cdc.trustedintermediary.wrappers.MetricMetadata
import spock.lang.Specification
Expand All @@ -30,6 +31,7 @@ class SendResultUseCaseTest extends Specification {
TestApplicationContext.register(TransformationRuleEngine, mockEngine)
TestApplicationContext.register(ResultSender, mockSender)
TestApplicationContext.register(Logger, mockLogger)
TestApplicationContext.register(HashHelper, HashHelper.getInstance())
TestApplicationContext.injectRegisteredImplementations()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package gov.hhs.cdc.trustedintermediary.etor.utils.security
import gov.hhs.cdc.trustedintermediary.context.TestApplicationContext
import gov.hhs.cdc.trustedintermediary.etor.orders.Order
import gov.hhs.cdc.trustedintermediary.etor.results.Result
import gov.hhs.cdc.trustedintermediary.wrappers.Logger
import spock.lang.Specification

class HashHelperTest extends Specification {
def mockLogger = Mock(Logger)
def hashHelper = new HashHelper()

def setup() {
TestApplicationContext.reset()
TestApplicationContext.init()
TestApplicationContext.register(Logger, mockLogger)
TestApplicationContext.injectRegisteredImplementations()
}

def "generateHash generates hash for an order"() {
given:
def mockOrder = Mock(Order)

when:
String mockHash = hashHelper.generateHash(mockOrder)

then:
mockHash !== ""
0 * mockLogger.logError(_, _)
}

def "generateHash generates the same hash for the same object"() {
given:
def mockResult = Mock(Result)
def mockResult2 = mockResult

when:
String mockHash = hashHelper.generateHash(mockResult)
String mockHash2 = hashHelper.generateHash(mockResult2)

then:
mockHash !== ""
mockHash == mockHash2
0 * mockLogger.logError(_, _)
}
}