diff --git a/core/src/main/java/com/hashengineering/crypto/X11.java b/core/src/main/java/com/hashengineering/crypto/X11.java index 0b3d0fe11..c970b54bc 100644 --- a/core/src/main/java/com/hashengineering/crypto/X11.java +++ b/core/src/main/java/com/hashengineering/crypto/X11.java @@ -31,7 +31,6 @@ public class X11 { private static final Logger log = LoggerFactory.getLogger(X11.class); private static boolean native_library_loaded = false; - private static Digest [] algorithms; static { try { @@ -43,21 +42,19 @@ public class X11 { catch(UnsatisfiedLinkError x) { log.info("Loading x11 failed: " + x.getMessage()); - init(); } catch(Exception e) { native_library_loaded = false; log.info("Loading x11 failed: " + e.getMessage()); - init(); } } /** * create the hash objects only if the native library failed to load */ - static void init() { - algorithms = new Digest[]{ + static Digest [] initAlgorithms() { + return new Digest[] { new BLAKE512(), new BMW512(), new Groestl512(), @@ -88,6 +85,7 @@ public static byte[] x11Digest(byte[] input) { static native byte [] x11_native(byte [] input, int offset, int length); public static byte [] x11(byte input[], int offset, int length) { + Digest [] algorithms = initAlgorithms(); Digest algorithm = algorithms[0]; algorithm.reset(); algorithm.update(input, offset, length); diff --git a/core/src/test/java/com/hashengineering/crypto/X11Test.java b/core/src/test/java/com/hashengineering/crypto/X11Test.java new file mode 100644 index 000000000..0b09f1257 --- /dev/null +++ b/core/src/test/java/com/hashengineering/crypto/X11Test.java @@ -0,0 +1,63 @@ +package com.hashengineering.crypto; + +import org.bouncycastle.util.Arrays; +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertTrue; + +public class X11Test { + @Test + public void x11Test() { + byte [] message = "Hello World!".getBytes(); + + byte [] digestOne = X11.x11Digest(message); + byte [] digestTwo = X11.x11Digest(message, 0, message.length); + + assertArrayEquals(digestTwo, digestOne); + } + + @Test + public void x11ThreadTest() { + final byte [] message = "Hello World!".getBytes(); + byte [] expectedHash = X11.x11Digest(message); + + class X11Thread extends Thread { + public Boolean result = true; + X11Thread() { + super(); + } + + public void run() { + for (int i = 0; i < 10; ++i) { + byte[] digestOne = X11.x11Digest(message); + byte[] digestTwo = X11.x11Digest(message, 0, message.length); + + result = Arrays.compareUnsigned(expectedHash, digestOne) == 0; + result &= Arrays.compareUnsigned(expectedHash, digestTwo) == 0; + } + } + } + + + + X11Thread [] threads = new X11Thread [10]; + + for (int i = 0; i < 10; ++i) { + threads[i] = new X11Thread(); + } + + for (int i = 0; i < 10; ++i) { + threads[i].start(); + } + + for (int i = 0; i < 10; ++i) { + try { + threads[i].join(); + assertTrue(threads[i].result); + } catch (InterruptedException x) { + // do nothing + } + } + } +}