view src/main/java/christie/blockchain/HashUtil.java @ 272:b592fe1d4a4e default tip

create example Attendance
author matac42 <matac@cr.ie.u-ryukyu.ac.jp>
date Thu, 01 Jul 2021 20:41:07 +0900
parents cd2fab84cd8b
children
line wrap: on
line source

package christie.blockchain;

import org.bouncycastle.crypto.digests.*;

import static java.util.Arrays.copyOfRange;


public class HashUtil {
    public static 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 static 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 static 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;
    }

    public static byte[] sha3omit12(byte[] input) {
        byte[] hash = sha3(input);
        return copyOfRange(hash, 12, hash.length);
    }



}