Example usage for java.security MessageDigest digest

List of usage examples for java.security MessageDigest digest

Introduction

In this page you can find the example usage for java.security MessageDigest digest.

Prototype

public byte[] digest() 

Source Link

Document

Completes the hash computation by performing final operations such as padding.

Usage

From source file:com.aqnote.shared.cryptology.digest.SM.java

public final static String sm3(byte[] src) {
    if (src == null)
        return "";
    try {//w  ww .j  a va 2 s .c  o m
        MessageDigest md = MessageDigest.getInstance(OID_SM3, JCE_PROVIDER);
        md.update(src);
        return new String(Hex.encode(md.digest()));
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String getMd5Digest(String pInput) {
    try {// ww  w .j  ava  2s.co m
        MessageDigest lDigest = MessageDigest.getInstance("MD5");
        lDigest.update(getBytes(pInput));
        BigInteger lHashInt = new BigInteger(1, lDigest.digest());
        return String.format("%1$032X", lHashInt);
    } catch (NoSuchAlgorithmException lException) {
        throw new RuntimeException(lException);
    }
}

From source file:Main.java

public static String getSHA256(String mykey) {
    try {/*from   w w  w  . ja v a2 s  . c  om*/
        MessageDigest digest = java.security.MessageDigest.getInstance("SHA256");
        digest.update(mykey.getBytes(Charset.forName("UTF-8")));
        return bytesToHex(digest.digest()).toLowerCase();
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "SHA hash exception: " + e.toString());
        return null;
    }
}

From source file:HashUtil.java

public static String getMD5(final String data) {
    try {/*from  ww w  .  j a  v a  2  s.  c  o m*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(data.getBytes());
        BigInteger bigInt = new BigInteger(1, m.digest());
        String hashtext = bigInt.toString(16);
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
        return hashtext;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return e.getMessage();
    }
}

From source file:Main.java

public static String getSHA(byte[] message, int length) {
    if (message == null || length <= 0) {
        return null;
    }// www .j  a v  a 2  s  . c  o m
    try {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(message, 0, length);
        byte[] output = md.digest();
        String s = "";
        for (int i = 0; i < output.length; i++) {
            s += hexByte(output[i] + 128);
        }
        return s;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:MD5.java

/**
 * Encodes a string//from w ww  . j  a  v  a 2s  .  co m
 * 
 * @param str String to encode
 * @return Encoded String
 * @throws NoSuchAlgorithmException
 */
public static String crypt(String str) {
    if (str == null || str.length() == 0) {
        throw new IllegalArgumentException("String to encript cannot be null or zero length");
    }

    StringBuffer hexString = new StringBuffer();

    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        byte[] hash = md.digest();

        for (int i = 0; i < hash.length; i++) {
            if ((0xff & hash[i]) < 0x10) {
                hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
            } else {
                hexString.append(Integer.toHexString(0xFF & hash[i]));
            }
        }
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("" + e);
    }

    return hexString.toString();
}

From source file:Main.java

private static String md5(String in) {
    MessageDigest digest;
    try {//w w w . j  a  v a 2s.  com
        digest = MessageDigest.getInstance("MD5");
        digest.reset();
        digest.update(in.getBytes());
        byte[] a = digest.digest();
        int len = a.length;
        StringBuilder sb = new StringBuilder(len << 1);
        for (int i = 0; i < len; i++) {
            sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
            sb.append(Character.forDigit(a[i] & 0x0f, 16));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.mobile.mpos.util.Common.java

/**
 * shar//from www  . ja  v  a 2 s  . co  m
 * @param source
 * @return
 */
public static String encryptSHA(String source) {
    try {
        MessageDigest sha = MessageDigest.getInstance("SHA");
        sha.update(source.getBytes(Consts.UTF_8));
        return ISOUtil.hexString(sha.digest());
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

From source file:Main.java

public static String getMD5String(String str) {
    StringBuffer stringbuffer;//from w  ww. j a v a 2s. c o  m
    MessageDigest messagedigest;
    try {
        messagedigest = MessageDigest.getInstance("MD5");
        messagedigest.update(str.getBytes());
        byte abyte0[] = messagedigest.digest();
        stringbuffer = new StringBuffer();
        for (int i = 0; i < abyte0.length; i++) {
            stringbuffer.append(HEX_DIGITS[(abyte0[i] & 0xf0) >>> 4]);
            stringbuffer.append(HEX_DIGITS[abyte0[i] & 0x0f]);
        }
        return stringbuffer.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static String get_device_id(Context ctx) {

    final TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);

    String tmDevice = "";
    String tmSerial = null;/*w ww .  ja v a 2s .co m*/
    String androidId = null;

    try {
        tmDevice = "" + tm.getDeviceId();
    } catch (Exception ex) {
    }

    try {
        tmSerial = "" + tm.getSimSerialNumber();
    } catch (Exception ex) {
    }
    try {
        androidId = "" + android.provider.Settings.Secure.getString(ctx.getContentResolver(),
                android.provider.Settings.Secure.ANDROID_ID);
    } catch (Exception ex) {
    }
    try {
        UUID deviceUuid = new UUID(androidId.hashCode(),
                ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
        String deviceId = deviceUuid.toString();

        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(deviceId.getBytes());

        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));
        }
        deviceId = sb.toString();

        return deviceId;
    } catch (Exception ex) {
    }
    return "nodeviceid";
}