-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
110 additions
and
3 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
63 changes: 63 additions & 0 deletions
63
bindings/jvm/src/main/java/org/ssohub/crypto/ecc/Hash.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,63 @@ | ||
/* | ||
* Copyright (c) 2023, Alden Torres | ||
* | ||
* Licensed under the terms of the MIT license. | ||
* Copy of the license at https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
package org.ssohub.crypto.ecc; | ||
|
||
import static org.ssohub.crypto.ecc.libecc.*; | ||
|
||
/** | ||
* Hash SHA-2 functions. | ||
* | ||
* @author aldenml | ||
*/ | ||
public final class Hash { | ||
|
||
private Hash() { | ||
} | ||
|
||
/** | ||
* Computes the SHA-256 of a given input. | ||
* <p> | ||
* See <a href="https://en.wikipedia.org/wiki/SHA-2">SHA-2</a> | ||
* | ||
* @param input the input message | ||
* @return the SHA-256 of the input | ||
*/ | ||
public static Data sha256(Data input) { | ||
byte[] inputBytes = input.toBytes(); | ||
|
||
byte[] digest = new byte[ecc_hash_sha256_HASHSIZE]; | ||
|
||
ecc_hash_sha256( | ||
digest, | ||
inputBytes, inputBytes.length | ||
); | ||
|
||
return new Data(digest); | ||
} | ||
|
||
/** | ||
* Computes the SHA-512 of a given input. | ||
* <p> | ||
* See <a href="https://en.wikipedia.org/wiki/SHA-2">SHA-2</a> | ||
* | ||
* @param input the input message | ||
* @return the SHA-512 of the input | ||
*/ | ||
public static Data sha512(Data input) { | ||
byte[] inputBytes = input.toBytes(); | ||
|
||
byte[] digest = new byte[ecc_hash_sha512_HASHSIZE]; | ||
|
||
ecc_hash_sha512( | ||
digest, | ||
inputBytes, inputBytes.length | ||
); | ||
|
||
return new Data(digest); | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
bindings/jvm/src/test/java/org/ssohub/crypto/ecc/HashTest.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,38 @@ | ||
/* | ||
* Copyright (c) 2023, Alden Torres | ||
* | ||
* Licensed under the terms of the MIT license. | ||
* Copy of the license at https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
package org.ssohub.crypto.ecc; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.ssohub.crypto.ecc.Hash.sha256; | ||
import static org.ssohub.crypto.ecc.Hash.sha512; | ||
|
||
/** | ||
* @author aldenml | ||
*/ | ||
public class HashTest { | ||
|
||
@Test | ||
void test_sha256() { | ||
Data input = new Data(Util.str2bin("abc")); | ||
|
||
Data digest = sha256(input); | ||
|
||
assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", digest.toHex()); | ||
} | ||
|
||
@Test | ||
void test_sha512() { | ||
Data input = new Data(Util.str2bin("abc")); | ||
|
||
Data digest = sha512(input); | ||
|
||
assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", digest.toHex()); | ||
} | ||
} |
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