Skip to content

Commit

Permalink
add hash sha2 java api layer (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml authored Mar 19, 2023
1 parent 6ac557b commit e3b9de8
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 3 deletions.
6 changes: 3 additions & 3 deletions bindings/js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ https://en.wikipedia.org/wiki/Proxy_re-encryption
### Cryptographic primitives and utilities

```
hash_sha256
hash_sha512
ecc_hash_sha256
ecc_hash_sha512
kdf_argon2id
ecc_kdf_argon2id
```
63 changes: 63 additions & 0 deletions bindings/jvm/src/main/java/org/ssohub/crypto/ecc/Hash.java
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);
}
}
3 changes: 3 additions & 0 deletions bindings/jvm/src/main/java/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ <h3 id="proxy-re-encryption-pre">Proxy Re-Encryption (PRE)</h3>
<h3 id="cryptographic-primitives-and-utilities">Cryptographic primitives and utilities</h3>

<pre>
ecc_hash_sha256
ecc_hash_sha512

ecc_kdf_argon2id
</pre>

Expand Down
38 changes: 38 additions & 0 deletions bindings/jvm/src/test/java/org/ssohub/crypto/ecc/HashTest.java
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());
}
}
3 changes: 3 additions & 0 deletions bindings/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,5 +260,8 @@ https://en.wikipedia.org/wiki/Proxy_re-encryption
### Cryptographic primitives and utilities

```
ecc_hash_sha256
ecc_hash_sha512
ecc_kdf_argon2id
```

0 comments on commit e3b9de8

Please sign in to comment.