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(byte[] input) 

Source Link

Document

Performs a final update on the digest using the specified array of bytes, then completes the digest computation.

Usage

From source file:Main.java

public static byte[] getMessageDigest(byte[] data, String algorithm) {
    try {/*  w ww  .  jav a  2 s .  c o m*/
        final MessageDigest md = MessageDigest.getInstance(algorithm);
        final byte[] digest = md.digest(data);
        return digest;
    } catch (final NoSuchAlgorithmException e) {
        Log.e(e.getMessage(), e.getMessage());
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static byte[] doubleSha256(byte[] bytes) {
    try {/*from w w w  .j a v  a 2 s .  c  o m*/
        MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
        return sha256.digest(sha256.digest(bytes));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String SHA1(String input) throws NoSuchAlgorithmException {
    MessageDigest mDigest = MessageDigest.getInstance("SHA1");
    byte[] result = mDigest.digest(input.getBytes());
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < result.length; i++) {
        sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
    }//w  ww  .j ava 2s  .co  m

    return sb.toString();
}

From source file:Main.java

public static String getMD5(String input) {
    try {//from w  ww .j a va2 s. co m
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(input.getBytes());
        BigInteger number = new BigInteger(1, messageDigest);
        String hashtext = number.toString(16);
        // Now we need to zero pad it if you actually want the full 32 chars.
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
        return hashtext;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Converts a java.lang.String in to a MD5 hashed String
 * @param source the source String/*w w w  .ja v  a2s  .  c  o  m*/
 * @return the MD5 hashed version of the string
 */
public static String md5(String source) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] bytes = md.digest(source.getBytes());
        return getString(bytes);
    } catch (Exception e) {
        return null;
    }
}

From source file:com.appdynamicspilot.util.MD5.java

/**
 * Returns the hashed value of <code>clear</code>.
 *//* w  w  w  .  j a v a 2 s  .c  om*/
public static String hash(String clear) throws Exception {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] b = md.digest(clear.getBytes());

        int size = b.length;
        StringBuffer h = new StringBuffer(size);
        for (int i = 0; i < size; i++) {
            int u = b[i] & 255; // unsigned conversion
            if (u < 16) {
                h.append("0" + Integer.toHexString(u));
            } else {
                h.append(Integer.toHexString(u));
            }
        }
        return h.toString();
    } catch (Exception e) {
        throw new Exception(e);
    }
}

From source file:Main.java

public static String sha256(String base) {
    try {// w ww  .  j a v a  2s  .  com
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hash = digest.digest(base.getBytes("UTF-8"));
        StringBuffer hexString = new StringBuffer();

        for (int i = 0; i < hash.length; i++) {
            String hex = Integer.toHexString(0xff & hash[i]);
            if (hex.length() == 1)
                hexString.append('0');
            hexString.append(hex);
        }

        return hexString.toString();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

/**
 * Converts a java.lang.String in to a SHA hashed String
 * @param source the source String//  w w  w. j  av  a 2  s. c  o m
 * @return the MD5 hashed version of the string
 */
public static String sha(String source) {
    try {
        MessageDigest md = MessageDigest.getInstance("SHA");
        byte[] bytes = md.digest(source.getBytes());
        return getString(bytes);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String md5(String value) {
    String result;// w w  w.jav  a  2  s .  c  o  m
    try {
        MessageDigest md = MessageDigest.getInstance("md5");
        result = byteToHexString(md.digest(value.getBytes("utf-8")));
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}

From source file:Main.java

public final static String md5(String value) {
    try {//from   w ww.  ja  v  a2  s  . c o  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(value.getBytes());
        BigInteger number = new BigInteger(1, messageDigest);
        String md5 = number.toString(16);

        while (md5.length() < 32) {
            md5 = "0" + md5;
        }
        return md5;

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}