Example usage for java.security MessageDigest digest

List of usage examples for java.security MessageDigest digest

Introduction

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

Prototype

public byte[] digest() 

Source Link

Document

Completes the hash computation by performing final operations such as padding.

Usage

From source file:Main.java

/**
 * @param context//from   w  w w .j  a v a 2 s.co m
 * @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;
}

From source file:com.agiletec.plugins.jpnewsletter.aps.system.services.newsletter.util.ShaEncoder.java

public static String encodeWord(String word) throws NoSuchAlgorithmException {
    if (word != null) {
        MessageDigest digest = MessageDigest.getInstance("SHA");
        digest.update(word.getBytes());/*from   ww  w  .  ja  v  a 2  s.  c  o  m*/
        byte bytes[] = digest.digest();
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            int b = bytes[i] & 0xff;
            if (b < 16) {
                buffer.append("0");
            }
            buffer.append(Integer.toHexString(b));
        }
        word = buffer.toString();
    }
    return word;
}

From source file:Main.java

/**
 * Encrypt the plain text with MD5 algorithm.
 *
 * @param plainText The plain text.//from   w  w  w.j a v  a 2  s. co m
 * @return The Encrypt content.
 */
public static String md5Encrypt(String plainText) {
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(plainText.getBytes(Charset.defaultCharset()));
        return new String(toHex(digest.digest()));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

static public String md5(String string) {
    try {//w w  w. ja  va  2 s  .c o 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: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

/***
 * compute hash for text// w w  w.  jav a 2  s .c o  m
 * 
 * @param text
 * @return
 * @throws NoSuchAlgorithmException
 * @throws UnsupportedEncodingException
 */
public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    byte[] sha1hash = md.digest();
    return convertToHex(sha1hash);
}

From source file:Main.java

public static String md5(String input) {
    String res = "";

    try {//w  ww. j av a 2 s.  co  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) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }

    return res;
}

From source file:Main.java

@SuppressWarnings("finally")
public static String getMD5(String str) {
    StringBuffer strBuf = new StringBuffer();
    try {//from w w  w.  ja  va 2 s.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

public static String getMD5Str(String password) {
    String strResult = "";
    MessageDigest md5;
    try {//  w ww  . ja  v  a 2 s . c  om
        md5 = MessageDigest.getInstance("MD5");
        md5.update(password.getBytes("UTF-8"));
        byte[] bzpassword_1 = md5.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bzpassword_1.length; ++i) {
            sb.append(String.format("%02x", bzpassword_1[i]));
        }
        md5.update(sb.toString().getBytes("UTF-8"));
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:baldrickv.s3streamingtool.Hash.java

/**
 * Cause apparently a hex encoded MD5 isn't good enough for S3.
 * They want a base64 encode of the raw binary hash.  blech.
 *//*from   www .j a v  a2 s  .c om*/
public static String getMd5ForS3(byte b[]) {
    try {
        MessageDigest sig = MessageDigest.getInstance("MD5");
        sig.update(b, 0, b.length);
        byte d[] = sig.digest();

        byte encoded[] = Base64.encodeBase64(d, false);

        return new String(encoded);

    } catch (Exception e) {

        e.printStackTrace();
        System.exit(-1);
        return null;
    }

}