generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* use sha-256 and add unit test --------- Co-authored-by: jcrichlake <[email protected]>
- Loading branch information
1 parent
90bcf6b
commit 3e73ebc
Showing
7 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
etor/src/main/java/gov/hhs/cdc/trustedintermediary/etor/utils/security/HashHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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 { | ||
|
||
private static final HashHelper INSTANCE = new HashHelper(); | ||
|
||
public static HashHelper getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
public String generateHash(Object input) { | ||
try { | ||
MessageDigest digest = MessageDigest.getInstance("SHA3-512"); | ||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...src/test/groovy/gov/hhs/cdc/trustedintermediary/etor/utils/security/HashHelperTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(_, _) | ||
} | ||
} |