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:com.buddycloud.friendfinder.HashUtils.java

public static String encodeSHA256(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(str.getBytes("UTF-8"));
    byte[] digest = md.digest();
    return Base64.encodeBase64String(digest);
}

From source file:Main.java

public static String md5hash(String string) {
    String generatedString = null;
    try {/* www  .ja  va2  s  .  com*/
        // Create MessageDigest instance for MD5
        MessageDigest md = MessageDigest.getInstance("MD5");
        // Add password bytes to digest
        md.update(string.getBytes());
        // Get the hash's bytes
        byte[] bytes = md.digest();
        // This bytes[] has bytes in decimal format;
        // Convert it to hexadecimal format
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        // Get complete hashed password in hex format
        generatedString = sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return generatedString;
}

From source file:com.buddycloud.friendfinder.HashUtils.java

public static String encodeSHA256(String provider, String contactId)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update((provider + ":" + contactId).getBytes("UTF-8"));
    byte[] digest = md.digest();
    return Base64.encodeBase64String(digest);
}

From source file:Main.java

@SuppressWarnings("finally")
public static String getMD5(String str) {
    StringBuffer strBuf = new StringBuffer();
    try {/*from ww  w . j av a  2s  . c  o m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        byte[] result16 = md.digest();
        char[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        for (int i = 0; i < result16.length; i++) {
            char[] c = new char[2];
            c[0] = digit[result16[i] >>> 4 & 0x0f];
            c[1] = digit[result16[i] & 0x0f];
            strBuf.append(c);
        }
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } finally {
        return strBuf.toString();
    }
}

From source file:Main.java

/**
 * Returns the key hashes of the signatures of the app with the specified package name
 * which may be needed when integrating with 3rd party services such as Facebook. Note that
 * Android apps usually only have one signature.
 *
 * @param context//from  w w  w. ja v  a 2  s.c  om
 * @param packageName The package name of the app
 * @return The key hashes
 */
public static List<String> getKeyHashes(Context context, String packageName) {
    try {
        List<String> hashes = new ArrayList<>();
        PackageInfo info = context.getPackageManager().getPackageInfo(packageName,
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            hashes.add(Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
        return hashes;
    } catch (PackageManager.NameNotFoundException e) {
    } catch (NoSuchAlgorithmException e) {
    }
    return null;
}

From source file:auguste.client.command.client.AccountCreate.java

private static String hashPassword(String password) {
    try {/* w  w  w.  ja va 2 s  . co m*/
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.reset();
        digest.update(password.getBytes());
        return new String(Hex.encodeHex(digest.digest()));
    } catch (NoSuchAlgorithmException e) {
        // Algorithme indisponible
        System.out.println(e.getMessage());
        return new String();
    }
}

From source file:Main.java

private static String SHA1(String text) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA-1");

    byte[] sha1hash = new byte[40];
    md.update(text.getBytes());
    sha1hash = md.digest();/* w  w w  .ja  v  a2 s  . c  om*/

    return convertToHex(sha1hash);
}

From source file:Main.java

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

From source file:StringUtil.java

/**
 * Generates a hash code for a given source code. 
 * This method ignores whitespace in generating the hash code.
 * @param source//from  w  w w  . jav a2s.c  om
 * @return
 */
public static String hashSourceCode(String source) {
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("MD5");
        md.update(source.getBytes());
        return new sun.misc.BASE64Encoder().encode(md.digest());
    } catch (NoSuchAlgorithmException e) {
        //_log.error("Failed to generate hashcode.", e);
    }
    return null;
}

From source file:apidemo.APIDemo.java

public static String md5String(String str) {
    try {/* w w  w  .  j av  a  2s .c  om*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());

        byte byteData[] = md.digest();

        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }

        return sb.toString();
    } catch (Exception ex) {
        return "";
    }
}