encrypt By SHA - Android java.security

Android examples for java.security:Sha

Description

encrypt By SHA

Demo Code


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;
import android.util.Log;

public class Main{
    private static final String LOG_TAG = CryptUtil.class.getSimpleName();
    //from   ww  w. jav  a2 s  .co  m
    public static String encryptBySHA(String info) {
        byte[] digesta = null;

        try {
            MessageDigest alga = MessageDigest.getInstance("SHA-1");

            alga.update(info.getBytes());

            digesta = alga.digest();
        } catch (NoSuchAlgorithmException e) {
            Log.e(LOG_TAG, e.getMessage());
        }
        return byte2hex(digesta);
    }
    
    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs.toUpperCase(Locale.CHINESE);
    }
}

Related Tutorials