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 md5(String input) {
    String result = input;/*w ww.  j  a v  a  2  s .  c  o  m*/
    if (input != null) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(input.getBytes());
            BigInteger hash = new BigInteger(1, md.digest());
            result = hash.toString(16);
            if ((result.length() % 2) != 0) {
                result = "0" + result;
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:Main.java

public static byte[] encryptMD5File(File file) {
    FileInputStream in = null;//w ww.j  av a2s  .co m
    try {
        in = new FileInputStream(file);
        FileChannel channel = in.getChannel();
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(buffer);
        return md.digest();
    } catch (NoSuchAlgorithmException | IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }
    }
    return null;
}

From source file:Main.java

public static String getSHA1Digest(final String pInput) {
    if (pInput != null) {
        try {//from w w w. j a  v  a2 s .com
            final MessageDigest lDigest = MessageDigest.getInstance("SHA1");
            lDigest.update(getBytes(pInput));
            final BigInteger lHashInt = new BigInteger(1, lDigest.digest());
            return String.format("%1$032X", lHashInt);
        } catch (final NoSuchAlgorithmException lException) {
            return pInput;
        }
    }
    return null;
}

From source file:Main.java

public static final String hash(final String s, final String algorithm) {
    try {//from  w  ww .  ja va  2 s.  c  om
        // Create MD5 or SHA-256 Hash
        MessageDigest digest = MessageDigest.getInstance(algorithm);
        digest.update(s.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

public static String getMd5Digest(final String pInput) {
    if (pInput != null) {
        try {/*from   w  w  w.j  a  va2s.  c o  m*/
            final MessageDigest lDigest = MessageDigest.getInstance("MD5");
            lDigest.update(getBytes(pInput));
            final BigInteger lHashInt = new BigInteger(1, lDigest.digest());
            return String.format("%1$032X", lHashInt);
        } catch (final NoSuchAlgorithmException lException) {
            return pInput;
        }
    }
    return null;

}

From source file:Main.java

public static String sha1(String str) {
    if (str == null || str.length() == 0) {
        return null;
    }//from  ww  w. j  a v  a  2s .  c  o  m

    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    try {
        MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
        mdTemp.update(str.getBytes());

        byte[] md = mdTemp.digest();
        int j = md.length;
        char buf[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = md[i];
            buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
            buf[k++] = hexDigits[byte0 & 0xf];
        }
        return new String(buf);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

private static String md5(String origin) {
    try {//w w  w.ja v a2 s .  c  om
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(origin.getBytes("UTF-8"));
        BigInteger bi = new BigInteger(1, md.digest());

        return bi.toString(16);
    } catch (Exception e) {
        return getUuid();
    }
}

From source file:Main.java

public static String getMD5(String mykey) {
    try {/*from  ww  w  .  j  a  v  a2  s  . c  o m*/
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(mykey.getBytes(Charset.forName("UTF-8")));
        return bytesToHex(digest.digest()).toLowerCase();
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "MD5 hash exception: " + e.toString());
        return null;
    }
}

From source file:Main.java

public static String generateHash(String pText) throws Exception {
    String hashValue;//from  www .  j  av a 2s  .  com
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.reset();
    md.update(pText.getBytes("ASCII"));
    hashValue = encodeHex(md.digest());
    return hashValue;
}

From source file:Main.java

public static String stringToMD5(String str) {

    try {//  w ww.j  a v a 2  s.  c  o  m
        byte[] strTemp = str.getBytes();
        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
        mdTemp.update(strTemp);
        return toHexString(mdTemp.digest());
    } catch (Exception e) {
        return null;
    }
}