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 byte[] doubleSHA256(byte[] data) {
    if (digest == null)
        try {//from  w  w  w. jav a2 s.c o m
            digest = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            return null;
        }

    return digest.digest(digest.digest(data));
}

From source file:Main.java

public static String encode(String code) {
    MessageDigest md;//from  w  w w.  j  a  va  2 s  .c  o m
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    md.update(code.getBytes());

    byte[] buffer = md.digest();
    StringBuffer hexString = new StringBuffer(2 * buffer.length);

    for (int i = 0; i < buffer.length; i++) {
        appendHexPair(buffer[i], hexString);
    }
    return hexString.toString();
}

From source file:Main.java

public static String md5(final String c) {
    if (md == null) {
        try {/*from   w w w. java  2s. c o  m*/
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    if (md != null) {
        md.update(c.getBytes());
        return byte2hex(md.digest());
    }
    return "";
}

From source file:Main.java

private static byte[] generateKey(String strSrc) {

    MessageDigest digest;/*  w ww .  j a  v  a  2 s .c  o m*/

    byte[] keyBytes = new byte[32];
    try {
        digest = MessageDigest.getInstance("SHA-256");
        digest.update(strSrc.getBytes("UTF-8"));
        System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return keyBytes;
}

From source file:Main.java

public final static String Md5(String s) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    try {/*www .j a v  a  2s.  com*/
        byte[] strTemp = s.getBytes("UTF-8");
        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
        mdTemp.update(strTemp);
        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

private static String md5(String message) throws Exception {
    MessageDigest md = MessageDigest.getInstance("md5");
    return bytesToHex(md.digest(message.getBytes("utf-8")));
}

From source file:Main.java

public static byte[] createMD5(FileInputStream is) throws IOException {
    MessageDigest digester = null;
    try {//  w w  w . jav a2 s.co m
        digester = MessageDigest.getInstance("MD5");
        byte[] buffer = new byte[16 * 1024]; // 16k
        int length = 0;
        while ((length = is.read(buffer)) > 0) {
            digester.update(buffer, 0, length);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        is.close();
    }

    if (digester != null) {
        return digester.digest();
    }

    return null;
}

From source file:Main.java

private static byte[] encryptAlgorithm(byte[] data, String algorithm) {
    try {//from   ww  w .  j  ava2  s.  c  om
        MessageDigest md = MessageDigest.getInstance(algorithm);
        md.update(data);
        return md.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return new byte[0];
}

From source file:Main.java

public static Bitmap createIdenticon(String data) {
    try {/*from w  w  w  .  j  av  a2  s  . c o m*/
        MessageDigest dig = MessageDigest.getInstance("MD5");
        dig.update(data.getBytes());
        return createIdenticon(dig.digest());
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

From source file:MainClass.java

public static byte[] makeDigest(String user, String password, long t1, double q1)
        throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(user.getBytes());//from  ww  w.j a  va  2s. co  m
    md.update(password.getBytes());
    md.update(makeBytes(t1, q1));
    return md.digest();
}