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 getMD5Str(String password) {
    String strResult = "";
    MessageDigest md5;/* w ww. j a  va 2  s  .com*/
    try {
        md5 = MessageDigest.getInstance("MD5");
        md5.update(password.getBytes("UTF-8"));
        byte[] bzpassword_1 = md5.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bzpassword_1.length; ++i) {
            sb.append(String.format("%02x", bzpassword_1[i]));
        }
        md5.update(sb.toString().getBytes("UTF-8"));
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:MainClass.java

public static byte[] makeDigest(byte[] mush, long t2, double q2) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(mush);//  w w w. ja v a2s .com
    md.update(makeBytes(t2, q2));
    return md.digest();
}

From source file:Main.java

public static String md5(String s) {
    try {/*from  ww  w. java  2  s .c  o  m*/
        MessageDigest digester = MessageDigest.getInstance("MD5");
        digester.update(s.getBytes("UTF-8"));
        byte[] a = digester.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 (UnsupportedEncodingException e) {
        return "";
    } catch (NoSuchAlgorithmException e) {
        return "";
    }
}

From source file:Main.java

public static String getMD5Str(String str) {
    MessageDigest messageDigest = null;
    try {/*  w ww  . ja  va2  s  .c  o m*/
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    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.toString();
}

From source file:Main.java

public static String md5(String string) {
    byte[] hash;//www. j  ava  2 s. com
    try {
        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Huh, MD5 should be supported?", e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Huh, UTF-8 should be supported?", e);
    }

    StringBuilder hex = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        if ((b & 0xFF) < 0x10)
            hex.append("0");
        hex.append(Integer.toHexString(b & 0xFF));
    }
    return hex.toString();
}

From source file:Main.java

public static String sha1(String s) {
    if (mSha1Digest == null) {
        try {//from  www  .  j av  a2 s  .c  o m
            mSha1Digest = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    byte[] data = mSha1Digest.digest(s.getBytes());
    return String.format("%0" + (data.length * 2) + "X", new BigInteger(1, data));
}

From source file:Main.java

public static String hashString(String input) {
    MessageDigest md5;//from www .j a v  a2s .  com
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return String.valueOf(input.hashCode());
    }
    md5.update(input.getBytes());
    byte[] hash = md5.digest();
    return toHex(hash);
}

From source file:Main.java

public static byte[] sha256(byte[] data) {
    if (digest == null)
        try {/*from  w  w  w .  j av a  2s  .co  m*/
            digest = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            return null;
        }

    return digest.digest(data);
}

From source file:Main.java

public static String md5hash(String string) {
    String generatedString = null;
    try {/*from   ww  w. j  a v a 2 s  . c om*/
        // Create MessageDigest instance for MD5
        MessageDigest md = MessageDigest.getInstance("MD5");
        // Add password bytes to digest
        md.update(string.getBytes());
        // Get the hash's bytes
        byte[] bytes = md.digest();
        // This bytes[] has bytes in decimal format;
        // Convert it to hexadecimal format
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        // Get complete hashed password in hex format
        generatedString = sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return generatedString;
}

From source file:Main.java

public static String getHash(String input) {
    if (input == null || input.equals("") || input.isEmpty()) {
        return "";
    }/*from w w w .ja  v  a 2 s  .c o m*/

    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] messageDigest = md.digest(input.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            sb.append(Integer.toString((messageDigest[i] & 0xff) + 0x100, 16).substring(1));
        }

        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return "";
    }
}