Example usage for java.security MessageDigest update

List of usage examples for java.security MessageDigest update

Introduction

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

Prototype

public final void update(ByteBuffer input) 

Source Link

Document

Update the digest using the specified ByteBuffer.

Usage

From source file:Main.java

public static String signature(String source) {

    try {//  w ww .  j  av a 2  s  . c  om
        MessageDigest md = MessageDigest.getInstance("MD5");

        md.reset();
        md.update(source.getBytes());

        byte[] mdbytes = md.digest();

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

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

From source file:Main.java

public static String imageDigest(BufferedImage img) {

    // write image to byte stream
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {/* ww w  . j  a va 2 s . co  m*/
        ImageIO.write(img, "png", os);
        os.flush();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    byte[] data = os.toByteArray();

    // compute md5 hash
    byte[] hash = null;
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(data);
        hash = md.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }

    // convert to string
    String hexString = "";
    for (int i = 0; i < hash.length; i++) {
        hexString += Integer.toString((hash[i] & 0xff) + 0x100, 16).substring(1);
    }
    return hexString;
}

From source file:Main.java

/**
 * @param s String to MD5 hash//from   w  w w.j  a  va  2  s . com
 * @return MD5 hashed string
 */
public static String md5(String s) {
    if (s == null) {
        return "";
    }
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte[] messageDigest = digest.digest();
        return bytesToHex(messageDigest);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

static public String md5(String string) {
    try {/*from  w  w  w .  j ava2s  .co m*/
        // Create MD5 Hash
        java.security.MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(string.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuilder hexString = new StringBuilder();
        for (byte aMessageDigest : messageDigest) {
            String h = Integer.toHexString(0xFF & aMessageDigest);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();

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

From source file:Main.java

/**
 * @param s String to SHA-1 hash/*from  ww w  . j a v a 2s .  c om*/
 * @return SHA-1 hashed string
 */
public static String sha1(String s) {
    if (s == null) {
        return "";
    }
    try {
        // Create SHA-1 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
        digest.update(s.getBytes());
        byte[] messageDigest = digest.digest();
        return bytesToHex(messageDigest);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:org.mobile.mpos.util.Common.java

/**
 * shar/*from  w  ww . j  av  a 2 s  .c  o m*/
 * @param source
 * @return
 */
public static String encryptSHA(String source) {
    try {
        MessageDigest sha = MessageDigest.getInstance("SHA");
        sha.update(source.getBytes(Consts.UTF_8));
        return ISOUtil.hexString(sha.digest());
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

From source file:Main.java

public static String SHA1(String str) {
    try {/* w  ww . j  av a 2s  . c  o m*/
        MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
        digest.update(str.getBytes());
        byte messageDigest[] = digest.digest();
        // Create Hex String
        StringBuffer hexString = new StringBuffer();

        for (byte i : messageDigest) {
            String shaHex = Integer.toHexString(i & 0xFF);
            if (shaHex.length() < 2) {
                hexString.append(0);
            }
            hexString.append(shaHex);
        }
        return hexString.toString();

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

From source file:Main.java

public static String md5(String input) {
    String res = "";
    try {//  www  .j  av a 2s. c o  m
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(input.getBytes());
        byte[] md5 = algorithm.digest();
        String tmp = "";
        for (int i = 0; i < md5.length; i++) {
            tmp = (Integer.toHexString(0xFF & md5[i]));
            if (tmp.length() == 1) {
                res += "0" + tmp;
            } else {
                res += tmp;
            }
        }
    } catch (NoSuchAlgorithmException ex) {
    }
    return res;
}

From source file:Main.java

/**
 * @param s String to SHA-256 hash/*from w w  w . j a v  a  2  s  .  c  o m*/
 * @return SHA-256 hashed string
 */
public static String sha256(String s) {
    if (s == null) {
        return "";
    }
    try {
        // Create SHA-256 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("SHA-256");
        digest.update(s.getBytes());
        byte[] messageDigest = digest.digest();
        return bytesToHex(messageDigest);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:HashUtil.java

public static String getMD5(final String data) {
    try {//from w  ww.j av  a  2s  .  co  m
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(data.getBytes());
        BigInteger bigInt = new BigInteger(1, m.digest());
        String hashtext = bigInt.toString(16);
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
        return hashtext;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return e.getMessage();
    }
}