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 hashKey(String key) {
    String cacheKey;/*from w ww  . j  a  v  a  2  s.c o m*/
    try {
        final MessageDigest mDigest = MessageDigest.getInstance("MD5");
        mDigest.update(key.getBytes());
        cacheKey = bytesToHexString(mDigest.digest());
    } catch (NoSuchAlgorithmException e) {
        cacheKey = String.valueOf(key.hashCode());
    }
    return cacheKey;
}

From source file:cn.zhuqi.mavenssh.ext.util.Coder.java

/**
 * MD5//from w  ww. ja v a  2  s.co m
 *
 * @param data
 * @return
 * @throws Exception
 */
public static byte[] encryptMD5(byte[] data) throws Exception {
    MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
    md5.update(data);
    return md5.digest();
}

From source file:cn.zhuqi.mavenssh.ext.util.Coder.java

/**
 * SHA//from   w  ww  . ja va  2s  .  c om
 *
 * @param data
 * @return
 * @throws Exception
 */
public static byte[] encryptSHA(byte[] data) throws Exception {
    MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
    sha.update(data);
    return sha.digest();
}

From source file:Main.java

public static final String MD5(String paramString) {
    char[] arrayOfChar1 = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70 };
    try {//  w w  w .j  a  v  a2  s  . co  m
        byte[] arrayOfByte1 = paramString.getBytes();
        MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
        localMessageDigest.update(arrayOfByte1);
        byte[] arrayOfByte2 = localMessageDigest.digest();
        int i = arrayOfByte2.length;
        char[] arrayOfChar2 = new char[i * 2];
        int j = 0;
        int k = 0;
        while (true) {
            if (j >= i)
                return new String(arrayOfChar2);
            int m = arrayOfByte2[j];
            int n = k + 1;
            arrayOfChar2[k] = arrayOfChar1[(0xF & m >>> 4)];
            k = n + 1;
            arrayOfChar2[n] = arrayOfChar1[(m & 0xF)];
            j++;
        }
    } catch (Exception localException) {
        localException.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String hashMD5Generate(String s) {
    try {/*from   ww  w  .j a  va 2s .c  o m*/
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            String h = Integer.toHexString(0xFF & messageDigest[i]);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        Log.d(DEBUG, hexString.toString());
        return hexString.toString();

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

From source file:StringUtil.java

/**
 * Encrypt password by using SHA-256 algorithm, encryptedPassword length is 32 bits
 * @param clearTextPassword/*  ww w .  ja v  a 2s .c  om*/
 * @return
 * @throws NoSuchAlgorithmException
 * reference http://java.sun.com/j2se/1.4.2/docs/api/java/security/MessageDigest.html
 */
public static String getEncryptedPassword(String clearTextPassword) {

    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(clearTextPassword.getBytes());
        return new sun.misc.BASE64Encoder().encode(md.digest());
    } catch (NoSuchAlgorithmException e) {
        //_log.error("Failed to encrypt password.", e);
    }
    return "";
}

From source file:Main.java

public static String md5_32(String plainText) {
    String re_md5 = new String();
    try {/*www .  j  a va2  s  .co  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(plainText.getBytes());
        byte b[] = md.digest();

        int i;

        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < b.length; offset++) {
            i = b[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }

        re_md5 = buf.toString();

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

From source file:it.cilea.osd.jdyna.utils.HashUtil.java

public static String hashMD5(String passw) {
    String passwHash = "";
    try {// www .  j  a  v a2s.  c  o  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(passw.getBytes());
        byte[] result = md.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < result.length; i++) {
            String tmpStr = "0" + Integer.toHexString((0xff & result[i]));
            sb.append(tmpStr.substring(tmpStr.length() - 2));
        }
        passwHash = sb.toString();
    } catch (NoSuchAlgorithmException ecc) {
        log.error("Errore algoritmo " + ecc);
    }
    return passwHash;
}

From source file:Main.java

public static byte[] md5(String strObj) {
    MessageDigest md;
    try {/*from  w w w.j  a  v  a 2s  .c o m*/
        md = MessageDigest.getInstance("MD5");
        md.update(strObj.getBytes());
        byte[] pwd = md.digest();
        return pwd;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getSHA256(byte[] mydata) {
    try {/*from  www.j  av a2  s  .  com*/
        MessageDigest digest = java.security.MessageDigest.getInstance("SHA256");
        digest.update(mydata);
        return bytesToHex(digest.digest()).toLowerCase();
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "SHA hash exception: " + e.toString());
        return null;
    }
}