compute String Sha Hash - Android java.lang

Android examples for java.lang:String Hash

Description

compute String Sha Hash

Demo Code


//package com.java2s;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String computeSha2Hash(String data)
            throws NoSuchAlgorithmException {

        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(data.getBytes());/*from w  w  w  .  j  a v  a2  s .  co  m*/

        byte byteData[] = md.digest();

        // convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16)
                    .substring(1));
        }

        System.out.println("Hex format : " + sb.toString());
        return sb.toString();
    }
}

Related Tutorials