// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA256 {
    private static final int REP = 100000;
    public static void main(String[] args) throws NoSuchAlgorithmException {
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < REP; i++) {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            String hash = toHex(md.digest("1234567890".getBytes()));
            if(!hash.equals("c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646")) {
                throw new RuntimeException("Ooops");
            }
        }
        long t2 = System.currentTimeMillis();
        System.out.println("SHA-256: " + (t2 - t1) / 1000.0);
    }
    private static String toHex(byte[] ba) {
        char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < ba.length; i++) {
           sb.append(hexdigit[(ba[i] >> 4) & 0x0F]);
           sb.append(hexdigit[ba[i] & 0x0F]);
        }
        return sb.toString();
     }
}
