hash SHA-256 - Android java.security

Android examples for java.security:Sha

Description

hash SHA-256

Demo Code


//package com.java2s;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    private static final String HASH_ALGORITHM = "SHA-256";

    public static String hash(String value) {

        MessageDigest digest;/* w w w  .  j av  a2s . com*/
        try {
            digest = MessageDigest.getInstance(HASH_ALGORITHM);
            byte[] data = digest.digest(value.getBytes("UTF-8"));
            String hash = String.format("%0" + (data.length * 2) + "X",
                    new BigInteger(1, data));
            return hash;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related Tutorials