Example usage for java.security MessageDigest getInstance

List of usage examples for java.security MessageDigest getInstance

Introduction

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

Prototype

public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a MessageDigest object that implements the specified digest algorithm.

Usage

From source file:Main.java

public static String md5(String senha) {
    String sen = "";
    MessageDigest md = null;// w  w w.j  a v a  2s.c o m
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        Log.w("Problemas ao gerar MD5", e);
    }
    BigInteger hash = new BigInteger(1, md.digest(senha.getBytes()));
    sen = hash.toString(16);
    return sen;
}

From source file:Main.java

static public String md5(String str) {
    MessageDigest algorithm = null;
    try {/*w w  w  . j  a va  2  s .  c  om*/
        algorithm = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    if (algorithm != null) {
        algorithm.reset();
        algorithm.update(str.getBytes());
        byte[] bytes = algorithm.digest();
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            if (Integer.toHexString(0xFF & b).length() == 1)
                hexString.append("0").append(Integer.toHexString(0xFF & b));
            else
                hexString.append(Integer.toHexString(0xFF & b));
        }
        return hexString.toString();
    }
    return "";

}

From source file:Main.java

public static String md5Hex(String message) {
    try {//from  w  w  w .j  av  a  2  s.  c  o m
        MessageDigest md = MessageDigest.getInstance("MD5");
        return hex(md.digest(message.getBytes("CP1252")));
    } catch (NoSuchAlgorithmException e) {
    } catch (UnsupportedEncodingException e) {
    }
    return null;
}

From source file:Main.java

public static String GetHash(byte[] data) {
    MessageDigest md;//from  w  w  w  . ja  v a  2 s  .c o m
    String hash = null;
    try {
        md = MessageDigest.getInstance("MD5");
        hash = new BigInteger(1, md.digest(data)).toString(16);
    } catch (NoSuchAlgorithmException e) {
        Log.i("BMSUtil", "Hashing Error!");
        e.printStackTrace();
    }
    return hash;
}

From source file:Main.java

public static byte[] md5(byte[] buff) throws NoSuchAlgorithmException {
    return MessageDigest.getInstance("MD5").digest(buff);
}

From source file:Main.java

public final static String getMessageDigest(byte[] buffer) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    try {/*from   www.j  a v  a2s .c o  m*/
        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
        mdTemp.update(buffer);
        byte[] md = mdTemp.digest();
        int j = md.length;
        char str[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = md[i];
            str[k++] = hexDigits[byte0 >>> 4 & 0xf];
            str[k++] = hexDigits[byte0 & 0xf];
        }
        return new String(str);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String getMD5(String str) {
    MessageDigest messageDigest = null;
    try {//  w ww  .  j  a  va2  s . com
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
    } catch (UnsupportedEncodingException e) {
    }
    byte[] byteArray = messageDigest.digest();
    StringBuffer md5StrBuff = new StringBuffer();
    for (int i = 0; i < byteArray.length; i++) {
        if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
            md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
        } else {
            md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
        }
    }
    return md5StrBuff.substring(8, 24).toString();
}

From source file:Main.java

private static String computeHash(String input) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.reset();/*from  w  ww .j  ava 2  s. co  m*/

    byte[] byteData = digest.digest(input.getBytes("UTF-8"));
    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < byteData.length; i++) {
        sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
    }

    return sb.toString();
}

From source file:Main.java

public static String MD5(String paramString) {
    if (paramString == null)
        return null;
    try {//from  www.  ja v a  2 s  .  c  om
        byte[] arrayOfByte1 = paramString.getBytes();
        MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
        localMessageDigest.reset();
        localMessageDigest.update(arrayOfByte1);
        byte[] arrayOfByte2 = localMessageDigest.digest();
        StringBuffer localStringBuffer = new StringBuffer();
        for (int i = 0; i < arrayOfByte2.length; i++) {
            Object[] arrayOfObject = new Object[1];
            arrayOfObject[0] = Byte.valueOf(arrayOfByte2[i]);
            localStringBuffer.append(String.format("%02X", arrayOfObject));
        }
        String str = localStringBuffer.toString();
        return str;
    } catch (Exception localException) {
    }
    return paramString.replaceAll("[^[a-z][A-Z][0-9][.][_]]", "");
}

From source file:Main.java

public static String SHA256Encrypt(String orignal) {
    MessageDigest md = null;/*from   w w  w  . jav a2s.  com*/
    try {
        md = MessageDigest.getInstance(ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    if (null != md) {
        byte[] origBytes = orignal.getBytes();
        md.update(origBytes);
        byte[] digestRes = md.digest();
        String digestStr = getDigestStr(digestRes);
        return digestStr;
    }

    return null;
}