changeset 106:691017a550d0

add Hash
author akahori
date Sun, 07 Oct 2018 12:23:31 +0900
parents 06c46b696b41
children 825e01825ad3
files src/main/java/christie/blockchain/HashUtil.java src/test/java/christie/blockchain/HashUtilTest.java
diffstat 2 files changed, 125 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/christie/blockchain/HashUtil.java	Sun Oct 07 12:23:31 2018 +0900
@@ -0,0 +1,45 @@
+package christie.blockchain;
+
+import org.bouncycastle.crypto.digests.*;
+
+
+public class HashUtil {
+    public byte[] sha256(byte[] input) {
+        SHA256Digest digest = new SHA256Digest();
+        byte[] resBuf = new byte[digest.getDigestSize()];
+        if(input != null) {
+            digest.update(input, 0, input.length);
+            digest.doFinal(resBuf, 0);
+        }else{
+            throw new NullPointerException("Can't sha256 input is null.");
+        }
+        return resBuf;
+
+    }
+
+    public byte[] sha3(byte[] input){
+        SHA3Digest digest = new SHA3Digest();
+        byte[] resBuf = new byte[digest.getDigestSize()];
+        if(input != null) {
+            digest.update(input, 0, input.length);
+            digest.doFinal(resBuf, 0);
+        }else{
+            throw new NullPointerException("Can't sha3 input is null.");
+        }
+        return resBuf;
+
+    }
+
+    public byte[] ripemd160(byte[] input){
+        RIPEMD160Digest digest = new RIPEMD160Digest();
+        byte[] resBuf = new byte[digest.getDigestSize()];
+        if(input != null) {
+            digest.update(input, 0, input.length);
+            digest.doFinal(resBuf, 0);
+        }else{
+            throw new NullPointerException("Can't ripemd160 input is null.");
+        }
+        return resBuf;
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/christie/blockchain/HashUtilTest.java	Sun Oct 07 12:23:31 2018 +0900
@@ -0,0 +1,80 @@
+package christie.blockchain;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+import java.util.Arrays;
+
+class HashUtilTest {
+
+    @Test
+    void sha256() {
+        HashUtil hashUtil1 = new HashUtil();
+        HashUtil hashUtil2 = new HashUtil();
+        String[] testStringArray = new String[]{"a", "b"};
+
+        // オブジェクトは同じじゃない
+        assertNotSame(hashUtil1, hashUtil2);
+
+        // 総当たりで, hash値が同じか調べる.
+        for(String str1: testStringArray){
+            byte[] hash1 = hashUtil1.sha256(str1.getBytes());
+            for(String str2: testStringArray){
+                byte[] hash2 = hashUtil2.sha256(str2.getBytes());
+
+                if(str1.equals(str2))
+                    assertEquals(Arrays.toString(hash1), Arrays.toString(hash2));
+                else
+                    assertNotEquals(Arrays.toString(hash1), Arrays.toString(hash2));
+            }
+        }
+
+    }
+
+    @Test
+    void sha3() {
+        HashUtil hashUtil1 = new HashUtil();
+        HashUtil hashUtil2 = new HashUtil();
+        String[] testStringArray = new String[]{"a", "b"};
+
+        // オブジェクトは同じじゃない
+        assertNotSame(hashUtil1, hashUtil2);
+
+        // 総当たりで, hash値が同じか調べる.
+        for(String str1: testStringArray){
+            byte[] hash1 = hashUtil1.sha3(str1.getBytes());
+            for(String str2: testStringArray){
+                byte[] hash2 = hashUtil2.sha3(str2.getBytes());
+
+                if(str1.equals(str2))
+                    assertEquals(Arrays.toString(hash1), Arrays.toString(hash2));
+                else
+                    assertNotEquals(Arrays.toString(hash1), Arrays.toString(hash2));
+            }
+        }
+    }
+
+    @Test
+    void ripemd160() {
+        HashUtil hashUtil1 = new HashUtil();
+        HashUtil hashUtil2 = new HashUtil();
+        String[] testStringArray = new String[]{"a", "b"};
+
+        // オブジェクトは同じじゃない
+        assertNotSame(hashUtil1, hashUtil2);
+
+        // 総当たりで, hash値が同じか調べる.
+        for(String str1: testStringArray){
+            byte[] hash1 = hashUtil1.ripemd160(str1.getBytes());
+            for(String str2: testStringArray){
+                byte[] hash2 = hashUtil2.ripemd160(str2.getBytes());
+
+                if(str1.equals(str2))
+                    assertEquals(Arrays.toString(hash1), Arrays.toString(hash2));
+                else
+                    assertNotEquals(Arrays.toString(hash1), Arrays.toString(hash2));
+            }
+        }
+    }
+
+}
\ No newline at end of file