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

/**
 * Get current certificate fingerprint/* w  ww  .jav  a  2s  .c  o m*/
 *
 * @param ctx         context of application
 * @param packageName your package name
 * @return Base64 packed SHA fingerprint of your packet certificate
 */
public static String[] getCertificateFingerprint(Context ctx, String packageName) {
    try {
        if (ctx == null || ctx.getPackageManager() == null)
            return null;
        PackageInfo info = ctx.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
        assert info.signatures != null;
        String[] result = new String[info.signatures.length];
        int i = 0;
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            //                result[i++] = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            result[i++] = toHex(md.digest());
        }
        return result;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.drisoftie.cwdroid.util.CredentialUtils.java

public static String md5(final String s) {
    try {// w  w  w .j a v a  2s .c  om
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance(MD5);
        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:de.hybris.platform.b2b.punchout.services.impl.AsymmetricManager.java

/**
 * Generates a hash using asymmetric encryption.
 * /*w w w . j  ava 2  s.c o m*/
 * @param unsecureText
 *           The text to be hashed.
 * @param salt
 *           The salt used to defend against dictionary and rainbow table attacks.
 * @return The hash.
 */
public static String getHash(final String unsecureText, final String salt) {
    try {
        final MessageDigest digest = MessageDigest.getInstance(ALGORITHM);
        digest.update(unsecureText.getBytes(CHAR_SET));
        digest.update(salt.getBytes(CHAR_SET));
        final byte[] byteData = digest.digest();

        //convert the byte to hex format method 1
        final 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 (NoSuchAlgorithmException | UnsupportedEncodingException e) {
        // should never happen
        LOG.error("System was unable to generate the hash.", e);
    }
    return null;
}

From source file:Main.java

static String md5hash(String key) {
    MessageDigest hash = null;
    try {/*from w ww .  j  a  v  a2s  .  c om*/
        hash = MessageDigest.getInstance(HASH_ALGORITHM_MD5);
    } catch (NoSuchAlgorithmException e) {
        return null;
    }

    hash.update(key.getBytes());
    byte[] digest = hash.digest();
    StringBuilder builder = new StringBuilder();
    for (int b : digest) {
        builder.append(Integer.toHexString((b >> 4) & 0xf));
        builder.append(Integer.toHexString((b >> 0) & 0xf));
    }
    return builder.toString();
}

From source file:Main.java

private static String sha256Hex(final String filePath) throws NoSuchAlgorithmException, IOException {
    final InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
    final MessageDigest md = MessageDigest.getInstance("SHA-256");

    final byte[] dataBytes = new byte[1024];

    int nread;//from   w  w w. ja  v a  2s.co  m
    while ((nread = fis.read(dataBytes)) != -1)
        md.update(dataBytes, 0, nread);
    final byte[] mdbytes = md.digest();

    final StringBuilder sb = new StringBuilder();
    for (final byte b : mdbytes)
        sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));

    return sb.toString();
}

From source file:com.ikanow.infinit.e.api.authentication.PasswordEncryption.java

public static String md5checksum(String toHash) {
    try {//from  ww w .  j av  a 2 s  .  c o  m
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(toHash.getBytes(Charset.forName("UTF8")));
        byte[] digest = m.digest();
        return new String(Hex.encodeHex(digest));
    } catch (Exception ex) {
        return toHash;
    }
}

From source file:Main.java

/**
 * generates MD5 of input string//from w  w w  . j a v a 2s. c  om
 *
 * @param input string to be hashed
 * @return MD5 hash of string
 */
public static String getMD5(String input) {

    MessageDigest m = null;
    try {
        m = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    m.reset();
    m.update(input.getBytes());
    byte[] digest = m.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    String hashtext = bigInt.toString(16);
    //zero pad to get the full 32 chars.
    while (hashtext.length() < 32) {
        hashtext = "0" + hashtext;
    }
    return hashtext;
}

From source file:com.jredrain.base.utils.Digests.java

public static String sha1(String decript) {
    try {/*from   w w w . ja va2 s . com*/
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.update(decript.getBytes());
        byte messageDigest[] = digest.digest();
        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        // ? ?? 
        for (int i = 0; i < messageDigest.length; i++) {
            String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
            if (shaHex.length() < 2) {
                hexString.append(0);
            }
            hexString.append(shaHex);
        }
        return hexString.toString();

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

From source file:altermarkive.uploader.Report.java

private static String hash(String text) {
    String hash = null;/*ww  w  .  j  a  va  2s  .c om*/
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(text.getBytes());
        BigInteger integer = new BigInteger(1, md.digest());
        hash = String.format("%1$032X", integer);
    } catch (NoSuchAlgorithmException exception) {
        String trace = Log.getStackTraceString(exception);
        String message = String.format("MD5 is not available:\n%s", trace);
        Log.e(TAG, message);
    }
    return hash;
}

From source file:Main.java

public static String getMD5(String value) {
    if (value == null || value.length() == 0)
        return null;

    MessageDigest m = null;

    try {/*from  w  ww.j  a v  a2s.com*/
        m = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        return null;
    }

    m.update(value.getBytes(), 0, value.length());
    return new BigInteger(1, m.digest()).toString(16).toUpperCase(Locale.getDefault());
}