Skip to content

Commit

Permalink
add create and destroy hasher interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sken committed Mar 24, 2020
1 parent 6f5b804 commit 0b649dd
Showing 4 changed files with 66 additions and 1 deletion.
1 change: 0 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@ crossPaths := false // drop off Scala suffix from artifact names.
organization := "org.scash"

scmInfo := Some(ScmInfo(url("https://github.com/sken77/BLAKE3jni"), "git@github.com:sken77/BLAKE3jni.git"))
licenses += ("MIT", url("http://opensource.org/licenses/MIT"))

bintrayPackageLabels := Seq("cryptography", "BLAKE3", "p2p", "blockchain")
bintrayOrganization := Some("scala-cash")
42 changes: 42 additions & 0 deletions src/main/java/org/scash/NativeBLAKE3.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package org.scash;

import org.scijava.nativelib.NativeLoader;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import static org.scash.NativeBLAKE3Util.checkState;

public class NativeBLAKE3 {
private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
private final Lock r = rwl.readLock();
private final Lock w = rwl.writeLock();

private static final boolean enabled;

private long hasher = -1;

static {
boolean isEnabled = false;
try {
@@ -21,6 +31,38 @@ public static boolean isEnabled() {
return enabled;
}

public NativeBLAKE3() throws IllegalStateException {
checkState(enabled);
long initHasher = createHasher();
checkState(initHasher != 0);
hasher = initHasher;
}

private long getHasher() throws IllegalStateException {
checkState(isValid());
return hasher;
}

public boolean isValid() {
return hasher != -1;
}

public void close() {
if(isValid()) {
cleanUp();
}
}

private void cleanUp() {
w.lock();
try {
destroyHasher(getHasher());
} finally {
hasher = -1;
w.unlock();
}
}

private static native long createHasher();

private static native void destroyHasher(long hasher);
6 changes: 6 additions & 0 deletions src/main/java/org/scash/NativeBLAKE3Util.java
Original file line number Diff line number Diff line change
@@ -9,6 +9,12 @@ public static void assertEquals(boolean val, boolean val2, String message) throw
System.out.println("PASS: " + message);
}

public static void checkState(boolean expression) {
if (!expression) {
throw new IllegalStateException();
}
}

public static class AssertFailException extends Exception {
public AssertFailException(String message) {
super(message);
18 changes: 18 additions & 0 deletions src/test/java/org/scash/NativeBLAKE3Test.java
Original file line number Diff line number Diff line change
@@ -10,4 +10,22 @@ public class NativeBLAKE3Test {
public void testLibrary() throws AssertFailException {
assertEquals(NativeBLAKE3.isEnabled(), true, "isEnabled");
}

@Test
public void testHasher() throws AssertFailException {
NativeBLAKE3 h = new NativeBLAKE3();
assertEquals(h.isValid(), true, "isValidHasher");
h.close();
assertEquals(h.isValid(), false, "hasherDestroyed");
}

@Test
public void multipleHashers() throws AssertFailException {
NativeBLAKE3 h1 = new NativeBLAKE3();
NativeBLAKE3 h2 = new NativeBLAKE3();
h1.close();
assertEquals(!h1.isValid() && h2.isValid(), true, "differentHashers");
h2.close();
assertEquals(!h1.isValid() && !h2.isValid(), true, "all deleted");
}
}

0 comments on commit 0b649dd

Please sign in to comment.