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 3 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 @@ -8,7 +8,11 @@
import gov.hhs.cdc.trustedintermediary.etor.ruleengine.transformation.TransformationRuleEngine;
import gov.hhs.cdc.trustedintermediary.wrappers.Logger;
import gov.hhs.cdc.trustedintermediary.wrappers.MetricMetadata;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.inject.Inject;
import org.apache.commons.codec.binary.Hex;

/** The overall logic to receive, convert to OML, and subsequently send a lab order. */
public class SendOrderUseCase implements SendMessageUseCase<Order<?>> {
Expand All @@ -29,10 +33,12 @@
public void convertAndSend(final Order<?> order, String receivedSubmissionId)
throws UnableToSendMessageException {

String hash = generateHash(order);

PartnerMetadata partnerMetadata =
new PartnerMetadata(
receivedSubmissionId,
String.valueOf(order.hashCode()),
hash,
PartnerMetadataMessageType.ORDER,
order.getSendingApplicationDetails(),
order.getSendingFacilityDetails(),
Expand All @@ -51,4 +57,16 @@

sendMessageHelper.saveSentMessageSubmissionId(receivedSubmissionId, outboundReportId);
}

public String generateHash(Object obj) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA3-512");
Fixed Show fixed Hide fixed
byte[] objBytes = obj.toString().getBytes(StandardCharsets.UTF_8);
byte[] hashBytes = digest.digest(objBytes);
return Hex.encodeHexString(hashBytes);
} catch (NoSuchAlgorithmException e) {
logger.logError("Algorithm does not exist!", e);
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,41 @@ class SendOrderUseCaseTest extends Specification {
1 * mockOrchestrator.findMessagesIdsToLink(_ as String) >> Set.of()
0 * mockOrchestrator.updateMetadataForSentMessage(_ as String, _ as String)
}

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

when:
String mockHash = SendOrderUseCase.getInstance().generateHash(mockOrder)

then:
mockHash !== ""
}

def "generateHash generates unique hash for the same object"() {
given:
def mockOrder = Mock(Order)
def mockOrder2 = Mock(Order)

when:
String mockHash = SendOrderUseCase.getInstance().generateHash(mockOrder)
String mockHash2 = SendOrderUseCase.getInstance().generateHash(mockOrder2)

then:
mockHash !== ""
mockHash !== mockHash2
}

// Need to check failure edge case
// def "generateHash logs error when object cannot be hashed"() {
// given:
// def mockOrder = Mock(Order)
//
// when:
// String mockHash = SendOrderUseCase.getInstance().generateHash(mockOrder)
//
// then:
// mockHash == ""
// }
}