// пример получения хеш алгоритмом ГОСТ2012
package kz.gamma;

import kz.gamma.core.UtilCM;
import kz.gamma.jce.provider.GammaTechProvider;
import kz.gamma.jce.provider.JDKMessageDigest;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;

public class Hash2012Test {

    public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException {
        Security.addProvider(new GammaTechProvider());
        byte[] data = "text data".getBytes();
        byte[] hash2012_512 = getHash(data, "GOST3411-2012-512", "GAMMA");
        byte[] hash2012_256 = getHash(data, "GOST3411-2012-256", "GAMMA");
        System.out.println("hash2012_512: "+ UtilCM.array2hex(hash2012_512));
        System.out.println("hash2012_256: "+ UtilCM.array2hex(hash2012_256));
    }

    public static byte[] getHash(byte[] data, String alg, String provider) throws NoSuchAlgorithmException, NoSuchProviderException {
        MessageDigest messageDigest = MessageDigest.getInstance(alg, provider);
        byte[] hash = messageDigest.digest(data);
        if (messageDigest instanceof JDKMessageDigest) {
            ((JDKMessageDigest) messageDigest).close();
        }
        return hash;
    }
}
