diff --git a/bindings/js/README.md b/bindings/js/README.md index 775dfece..2d23ac49 100644 --- a/bindings/js/README.md +++ b/bindings/js/README.md @@ -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 ``` diff --git a/bindings/jvm/src/main/java/org/ssohub/crypto/ecc/Hash.java b/bindings/jvm/src/main/java/org/ssohub/crypto/ecc/Hash.java new file mode 100644 index 00000000..4c7b2f38 --- /dev/null +++ b/bindings/jvm/src/main/java/org/ssohub/crypto/ecc/Hash.java @@ -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. + *

+ * See SHA-2 + * + * @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. + *

+ * See SHA-2 + * + * @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); + } +} diff --git a/bindings/jvm/src/main/java/overview.html b/bindings/jvm/src/main/java/overview.html index eb1bae47..7d4151a0 100644 --- a/bindings/jvm/src/main/java/overview.html +++ b/bindings/jvm/src/main/java/overview.html @@ -264,6 +264,9 @@

Proxy Re-Encryption (PRE)

Cryptographic primitives and utilities

+ecc_hash_sha256
+ecc_hash_sha512
+
 ecc_kdf_argon2id
 
diff --git a/bindings/jvm/src/test/java/org/ssohub/crypto/ecc/HashTest.java b/bindings/jvm/src/test/java/org/ssohub/crypto/ecc/HashTest.java new file mode 100644 index 00000000..287efb35 --- /dev/null +++ b/bindings/jvm/src/test/java/org/ssohub/crypto/ecc/HashTest.java @@ -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()); + } +} diff --git a/bindings/python/README.md b/bindings/python/README.md index 0adc3921..77797646 100644 --- a/bindings/python/README.md +++ b/bindings/python/README.md @@ -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 ```