You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2019/12/16 08:38:52 UTC

[incubator-tuweni] branch master updated (3c23981 -> 528fd0b)

This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git.


    from 3c23981  Allow CN as option when generating the self-signed certs
     new 8430f8e  Correct import
     new 528fd0b  Fix TUWENI-30: expose key for GenericHash

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../apache/tuweni/crypto/sodium/GenericHash.java   | 120 +++++++++++++++++++++
 .../tuweni/crypto/sodium/GenericHashTest.java      |   7 ++
 .../main/java/org/apache/tuweni/net/tls/TLS.java   |  11 +-
 3 files changed, 134 insertions(+), 4 deletions(-)


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org


[incubator-tuweni] 02/02: Fix TUWENI-30: expose key for GenericHash

Posted by to...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git

commit 528fd0bae7cce6bbd3be42f3cc3f7e0540f61d32
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Mon Dec 16 00:38:41 2019 -0800

    Fix TUWENI-30: expose key for GenericHash
---
 .../apache/tuweni/crypto/sodium/GenericHash.java   | 120 +++++++++++++++++++++
 .../tuweni/crypto/sodium/GenericHashTest.java      |   7 ++
 2 files changed, 127 insertions(+)

diff --git a/crypto/src/main/java/org/apache/tuweni/crypto/sodium/GenericHash.java b/crypto/src/main/java/org/apache/tuweni/crypto/sodium/GenericHash.java
index b37003f..84a32cd 100644
--- a/crypto/src/main/java/org/apache/tuweni/crypto/sodium/GenericHash.java
+++ b/crypto/src/main/java/org/apache/tuweni/crypto/sodium/GenericHash.java
@@ -128,6 +128,106 @@ public final class GenericHash {
   }
 
   /**
+   * Key of generic hash function.
+   */
+  public static final class Key implements Destroyable {
+    private final Allocated value;
+
+    private Key(Pointer ptr, int length) {
+      this.value = new Allocated(ptr, length);
+    }
+
+    @Override
+    public void destroy() {
+      value.destroy();
+    }
+
+    @Override
+    public boolean isDestroyed() {
+      return value.isDestroyed();
+    }
+
+    /**
+     *
+     * @return the length of the key
+     */
+    public int length() {
+      return value.length();
+    }
+
+    /**
+     * Create a {@link GenericHash.Key} from a pointer.
+     *
+     * @param allocated the allocated pointer
+     * @return A key.
+     */
+    public static Key fromPointer(Allocated allocated) {
+      return new Key(Sodium.dup(allocated.pointer(), allocated.length()), allocated.length());
+    }
+
+    /**
+     * Create a {@link GenericHash.Key} from a hash.
+     *
+     * @param hash the hash
+     * @return A key.
+     */
+    public static Key fromHash(Hash hash) {
+      return new Key(Sodium.dup(hash.value.pointer(), hash.value.length()), hash.value.length());
+    }
+
+    /**
+     * Create a {@link GenericHash.Key} from an array of bytes.
+     *
+     * @param bytes The bytes for the key.
+     * @return A key.
+     */
+    public static Key fromBytes(Bytes bytes) {
+      return fromBytes(bytes.toArrayUnsafe());
+    }
+
+    /**
+     * Create a {@link GenericHash.Key} from an array of bytes.
+     *
+     * @param bytes The bytes for the key.
+     * @return A key.
+     */
+    public static Key fromBytes(byte[] bytes) {
+      return Sodium.dup(bytes, GenericHash.Key::new);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+      if (obj == this) {
+        return true;
+      }
+      if (!(obj instanceof GenericHash.Key)) {
+        return false;
+      }
+      Key other = (Key) obj;
+      return other.value.equals(value);
+    }
+
+    @Override
+    public int hashCode() {
+      return Objects.hashCode(value);
+    }
+
+    /**
+     * @return The bytes of this key.
+     */
+    public Bytes bytes() {
+      return value.bytes();
+    }
+
+    /**
+     * @return The bytes of this key.
+     */
+    public byte[] bytesArray() {
+      return value.bytesArray();
+    }
+  }
+
+  /**
    * Generic hash function output.
    */
   public static final class Hash implements Destroyable {
@@ -210,4 +310,24 @@ public final class GenericHash {
     Sodium.crypto_generichash(output, hashLength, input.value.pointer(), input.length(), null, 0);
     return new Hash(output, hashLength);
   }
+
+  /**
+   * Creates a generic hash of specified length of the input
+   *
+   * @param hashLength the length of the hash
+   * @param input the input of the hash function
+   * @param key the key of the hash function
+   * @return the hash of the input
+   */
+  public static Hash hash(int hashLength, Input input, Key key) {
+    Pointer output = Sodium.malloc(hashLength);
+    Sodium.crypto_generichash(
+        output,
+        hashLength,
+        input.value.pointer(),
+        input.length(),
+        key.value.pointer(),
+        key.length());
+    return new Hash(output, hashLength);
+  }
 }
diff --git a/crypto/src/test/java/org/apache/tuweni/crypto/sodium/GenericHashTest.java b/crypto/src/test/java/org/apache/tuweni/crypto/sodium/GenericHashTest.java
index 9e5193e..489ce69 100644
--- a/crypto/src/test/java/org/apache/tuweni/crypto/sodium/GenericHashTest.java
+++ b/crypto/src/test/java/org/apache/tuweni/crypto/sodium/GenericHashTest.java
@@ -34,4 +34,11 @@ class GenericHashTest {
     assertNotNull(output);
     assertEquals(64, output.bytes().size());
   }
+
+  @Test
+  void hashWithKeyValue() {
+    GenericHash.Hash output = GenericHash.hash(64, GenericHash.Input.fromBytes(Bytes.random(384)), GenericHash.Key.fromBytes(Bytes.random(32)));
+    assertNotNull(output);
+    assertEquals(64, output.bytes().size());
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org


[incubator-tuweni] 01/02: Correct import

Posted by to...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git

commit 8430f8e22b86236e54a8ca0a8469c21020682fc9
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Mon Dec 16 00:38:19 2019 -0800

    Correct import
---
 net/src/main/java/org/apache/tuweni/net/tls/TLS.java | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/net/src/main/java/org/apache/tuweni/net/tls/TLS.java b/net/src/main/java/org/apache/tuweni/net/tls/TLS.java
index 5ecc20f..b30d9e5 100644
--- a/net/src/main/java/org/apache/tuweni/net/tls/TLS.java
+++ b/net/src/main/java/org/apache/tuweni/net/tls/TLS.java
@@ -16,7 +16,6 @@ import static java.nio.charset.StandardCharsets.UTF_8;
 import static java.nio.file.Files.createDirectories;
 import static org.apache.tuweni.crypto.Hash.sha2_256;
 
-import jdk.internal.joptsimple.internal.Strings;
 import org.apache.tuweni.bytes.Bytes;
 
 import java.io.BufferedReader;
@@ -38,6 +37,7 @@ import java.util.Calendar;
 import java.util.Date;
 import java.util.UUID;
 
+import com.google.common.base.Strings;
 import org.bouncycastle.asn1.x500.X500Name;
 import org.bouncycastle.cert.X509v3CertificateBuilder;
 import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
@@ -86,11 +86,13 @@ public final class TLS {
    *
    * @param key The key path.
    * @param certificate The certificate path.
-   * @param commonName the name to use for the CN attribute of the certificate. If null or empty, a random value is used.
+   * @param commonName the name to use for the CN attribute of the certificate. If null or empty, a random value is
+   *        used.
    * @return {@code true} if a self-signed certificate was created.
    * @throws IOException If an IO error occurs creating the certificate.
    */
-  public static boolean createSelfSignedCertificateIfMissing(Path key, Path certificate, String commonName) throws IOException {
+  public static boolean createSelfSignedCertificateIfMissing(Path key, Path certificate, String commonName)
+      throws IOException {
     if (Files.exists(certificate) || Files.exists(key)) {
       return false;
     }
@@ -112,7 +114,8 @@ public final class TLS {
     return true;
   }
 
-  private static void createSelfSignedCertificate(Date now, Path key, Path certificate, String commonName) throws NoSuchAlgorithmException,
+  private static void createSelfSignedCertificate(Date now, Path key, Path certificate, String commonName)
+      throws NoSuchAlgorithmException,
       IOException,
       OperatorCreationException,
       CertificateException {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org