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

private static String md5(final String s) {
    final String MD5 = "MD5";
    try {/*from   w  ww .  jav a 2 s.c o m*/
        MessageDigest digest = MessageDigest.getInstance(MD5);
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        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 md5(String s) {
    try {//from  ww  w  .jav a2  s .c om
        MessageDigest digester = MessageDigest.getInstance("MD5");
        digester.update(s.getBytes("UTF-8"));
        byte[] a = digester.digest();
        int len = a.length;
        StringBuilder sb = new StringBuilder(len << 1);

        for (int i = 0; i < len; i++) {
            sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
            sb.append(Character.forDigit(a[i] & 0x0f, 16));
        }

        return sb.toString();
    } catch (UnsupportedEncodingException e) {
        return "";
    } catch (NoSuchAlgorithmException e) {
        return "";
    }
}

From source file:Main.java

public static String md5s(String plainText) {
    String str = "";
    try {//from   ww w.  j a v a  2  s.com
        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));
        }
        str = buf.toString();

    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block   
        e.printStackTrace();
    }
    return str;
}

From source file:Main.java

/**
 * A hashing method that changes a string (like a URL) into a hash suitable
 * for using as a disk filename.// w  ww.jav a 2  s  .co  m
 */
public static String hashKeyForUrl(String key) {
    String cacheKey;
    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:Main.java

public static String createPubNubSafeBase64Hash(String input) {
    try {/*  ww w .  ja v  a  2  s .c  o  m*/
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.update(input.getBytes());
        String encodedChannelName = Base64.encodeToString(messageDigest.digest(), Base64.URL_SAFE);

        //pubnub channel names cannot be more than 92 characters
        if (encodedChannelName.length() > 92) {
            encodedChannelName = encodedChannelName.substring(0, 91);
        }
        //pubnub channel names cannot have whitespace characters
        return encodedChannelName.trim();
    } catch (Exception e) {
        Log.d("X", "Error in encoding: " + e.getMessage());
        return null;
    }
}

From source file:Main.java

private static byte[] getHash(@NonNull X509Certificate certificate)
        throws NoSuchAlgorithmException, CertificateEncodingException {
    MessageDigest digest = java.security.MessageDigest.getInstance("SHA1");
    digest.update(certificate.getEncoded());
    return digest.digest();
}

From source file:Main.java

public static String certHash(byte[] encoded, String digest) {
    try {// w  w  w.j  av a2  s  .  c  o m
        MessageDigest md = MessageDigest.getInstance(digest);
        md.update(encoded);
        return hexString(md.digest());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static String keyHash(Context context) {
    String key = "";
    try {/*  ww w  . j  av  a  2s . co m*/
        PackageInfo info = context.getPackageManager().getPackageInfo("org.tathva.triloaded.anubhava",
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            key = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            Log.d("anas", key);
        }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }

    return key;
}

From source file:Main.java

public static String encryption(String plainText) {
    String re_md5 = "";
    try {//from  w w  w  .  ja  va2  s . com
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(plainText.getBytes());
        byte b[] = md.digest();
        int i = 0;

        StringBuilder sb = new StringBuilder("");
        for (int offset = 0, len = b.length; offset < len; offset++) {
            i = b[offset];
            if (i < 0) {
                i += 256;
            }
            if (i < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(i));
        }
        re_md5 = sb.toString();

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

From source file:Main.java

/**
 * @param context//from   ww  w  .ja v  a  2  s.c  om
 * @return KeyHash
 * follow facebook developers link to get release key hash
 * https://developers.facebook.com/docs/android/getting-started#release-key-hash
 */
public static String getKeyHash(Context context) {
    PackageInfo packageInfo;
    String key = null;
    try {
        packageInfo = context.getPackageManager().getPackageInfo(
                context.getApplicationContext().getPackageName(), PackageManager.GET_SIGNATURES);
        for (Signature signature : packageInfo.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e1) {
    } catch (NoSuchAlgorithmException e) {
    } catch (Exception e) {
    }
    return key;
}