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

private static String getDigestValue(String s, String digestType) {
    String result = "";
    byte strTemp[] = s.getBytes();
    MessageDigest digestTemp;/*from   ww w  .j  a va 2  s  .c o m*/

    try {
        digestTemp = MessageDigest.getInstance(digestType);
        digestTemp.update(strTemp);

        byte[] digestValue = digestTemp.digest();

        result = bytesToHexString(digestValue);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
    return result;
}

From source file:Main.java

/**
 * use md5 algorithm//  w w  w  .  j  a v a2s .  c o m
 * @param string the String need to encrypt
 * @return the encrypted result
 */
public static String md5(final String string) {
    byte[] hash;
    try {
        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("MD5 should be supported?", e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("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

/**
 * For hashing the master password/*from w ww . j a  v  a 2s.co  m*/
 *
 * @param password
 * @return
 */
public static String hashPassword(String password)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(password.getBytes("UTF-8")); // Change this to "UTF-16" if needed
    byte[] digest = md.digest();
    return new String(digest);
}

From source file:Main.java

public static String checksum(String path, String algorithm) {
    MessageDigest md = null;/*  w w w.j  a va2  s. c om*/

    try {
        md = MessageDigest.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }

    FileInputStream i = null;

    try {
        i = new FileInputStream(path);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }

    byte[] buf = new byte[1024];
    int len = 0;

    try {
        while ((len = i.read(buf)) != -1) {
            md.update(buf, 0, len);
        }
    } catch (IOException e) {

    }

    byte[] digest = md.digest();

    // HEX
    StringBuilder sb = new StringBuilder();
    for (byte b : digest) {
        sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
    }

    return sb.toString();

}

From source file:MainClass.java

public static String md(String f) throws Exception {
    BufferedInputStream file = new BufferedInputStream(new FileInputStream(f));
    MessageDigest md = MessageDigest.getInstance("MD5");
    DigestInputStream in = new DigestInputStream(file, md);
    int i;/*  w  w w .  j  a v a2s .c  o m*/
    byte[] buffer = new byte[BUFFER_SIZE];
    do {
        i = in.read(buffer, 0, BUFFER_SIZE);
    } while (i == BUFFER_SIZE);
    md = in.getMessageDigest();
    in.close();

    return new String(md.digest());
}

From source file:MainClass.java

public static String md(String f) throws Exception {
    BufferedInputStream file = new BufferedInputStream(new FileInputStream(f));
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    DigestInputStream in = new DigestInputStream(file, md);
    int i;//from  ww w. j a  v  a2s. c  o  m
    byte[] buffer = new byte[BUFFER_SIZE];
    do {
        i = in.read(buffer, 0, BUFFER_SIZE);
    } while (i == BUFFER_SIZE);
    md = in.getMessageDigest();
    in.close();

    return new String(md.digest());
}

From source file:Main.java

public static String convertStringToMd5(String valor) {

    MessageDigest mDigest;/*w w  w. ja va2s.c  o  m*/
    StringBuffer sb;

    if (valor == "") {
        return null;
    }
    try {
        mDigest = MessageDigest.getInstance("MD5");
        byte[] valorMD5 = mDigest.digest(valor.getBytes("UTF-8"));
        sb = new StringBuffer();

        for (byte b : valorMD5) {
            sb.append(Integer.toHexString((b & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
        Logger.getLogger(MessageDigest.class.getName()).log(Level.SEVERE, null, ex);
        Logger.getLogger(StringBuffer.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:Main.java

public static synchronized boolean downloadFileMD5Check(File f, String expectedMD5) {
    boolean flag = false;
    try {//from w  w w.  ja va 2  s .com
        MessageDigest md = MessageDigest.getInstance("MD5");
        FileInputStream fis = new FileInputStream(f);
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = fis.read(b)) != -1) {
            md.update(b, 0, len);
        }

        if (md5(md).equals(expectedMD5)) {
            flag = true;
        }
        fis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return flag;
}

From source file:Main.java

public static String encode(String string) {
    try {//  w ww. j a v  a 2s . c  om
        MessageDigest e = MessageDigest.getInstance("MD5");
        return bytesToHexString(e.digest(string.getBytes()));
    } catch (NoSuchAlgorithmException var2) {
        var2.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String getMD5String(String str) {
    StringBuffer stringbuffer;/*from  w ww  .  j  a  v a2  s .  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 "";
    }
}