diff src/main/java/christie/blockchain/HashUtil.java @ 106:691017a550d0

add Hash
author akahori
date Sun, 07 Oct 2018 12:23:31 +0900
parents
children eab161e557bd
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;
+    }
+
+}