Example usage for java.security MessageDigest getInstance

List of usage examples for java.security MessageDigest getInstance

Introduction

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

Prototype

public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a MessageDigest object that implements the specified digest algorithm.

Usage

From source file:Main.java

/**
 * Get current certificate fingerprint/*from  w w w  .  j  a  v a2 s .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:Componentes.EncryptionMD5.java

public static String getStringMessageDigest(String message, String algorithm) {
    byte[] digest = null;
    byte[] buffer = message.getBytes();
    try {/*from  w  ww  . j a  v  a2  s.co m*/
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        messageDigest.reset();
        messageDigest.update(buffer);
        digest = messageDigest.digest();
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Error creando Digest");
    }
    return toHexadecimal(digest);
}

From source file:Main.java

public static String getSha(String string, String encoding) {
    try {//from  w  w w. j a v  a2 s  .  c  o  m
        byte[] sha = MessageDigest.getInstance("SHA").digest(string.getBytes(encoding));
        return new String(encodeHex(sha));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return string;
}

From source file:com.cherong.mock.common.base.util.EncryptionUtil.java

public static String md5L32(String unencryptedText) {
    if (StringUtils.isEmpty(unencryptedText)) {
        LOGGER.info("unencryptedText is null.");
        return unencryptedText;
    }/* www  .j  av a 2  s .c o  m*/

    String ciphertext = "";
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] bytes = md5.digest(unencryptedText.getBytes("UTF-8"));
        StringBuffer buffer = new StringBuffer();
        for (byte b : bytes) {
            int bt = b & 0xff;
            if (bt < 16) {
                buffer.append(0);
            }
            buffer.append(Integer.toHexString(bt));
        }
        ciphertext = buffer.toString();
        LOGGER.info("encrypt string {} to {};", unencryptedText, ciphertext);
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
    }
    return ciphertext;
}

From source file:de.marx_labs.utilities.common.util.HashUtil.java

public static String hash(String value) {
    MessageDigest digest;//from w  w  w  .  ja v a  2s.  c om
    try {
        digest = MessageDigest.getInstance("SHA-256");
        digest.update(value.getBytes("UTF-8"));
        byte[] hash = digest.digest();

        return new String(hash);
    } catch (Exception e) {
        logger.error("", e);
    }
    return null;
}

From source file:Main.java

/**
 * Gets the key hash of application package's certificate signature.
 * @since   0.1.1/*from ww  w  .ja v a2 s  . com*/
 * @param   aContext The context from which the package manager is retrieved to get the signature's information.
 * @return   The list of signatures embedded to the application package.
 */
public static List<String> getKeyHash(Context aContext) {
    try {
        PackageInfo info = aContext.getPackageManager().getPackageInfo(getPackageName(aContext),
                PackageManager.GET_SIGNATURES);

        List<String> keyHashList = new ArrayList<String>(info.signatures.length);

        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            keyHashList.add(Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
        return keyHashList;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String getMD5EncryptedString(String encTarget) {
    MessageDigest mdEnc = null;//from www .  j av a  2  s.  co  m
    try {
        mdEnc = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Exception while encrypting to md5");
        e.printStackTrace();
    } // Encryption algorithm
    mdEnc.update(encTarget.getBytes(), 0, encTarget.length());
    String md5 = new BigInteger(1, mdEnc.digest()).toString(16);
    while (md5.length() < 32) {
        md5 = "0" + md5;
    }
    return md5;
}

From source file:Main.java

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

    MessageDigest m = null;//  w w w  .  j  a v  a 2s .com

    try {
        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());
}

From source file:Main.java

public static String getSha1(FileDescriptor fd) {
    MessageDigest md;/*w w w  . j av  a  2  s .co  m*/
    try {
        md = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    byte[] b = new byte[4096];
    FileInputStream fis = new FileInputStream(fd);
    InputStream is = new BufferedInputStream(fis, 4096);
    try {
        for (int n; (n = is.read(b)) != -1;) {
            md.update(b, 0, n);
        }
    } catch (IOException e) {
        Log.w(TAG, "IOException while computing SHA-1");
        return null;
    }
    byte[] sha1hash = new byte[40];
    sha1hash = md.digest();
    return getHex(sha1hash);
}

From source file:com.kumbaya.dht.Keys.java

public static KUID of(String key) {
    try {// www.j a v  a2 s  .co  m
        MessageDigest md = MessageDigest.getInstance("SHA1");
        KUID result = KUID.createWithBytes(md.digest(key.getBytes("UTF-8")));
        md.reset();
        return result;
    } catch (UnsupportedEncodingException e) {
        logger.warn("failed to encode key: " + key, e);
        return null;
    } catch (NoSuchAlgorithmException e) {
        logger.warn("failed to encode key: " + key, e);
        return null;
    }
}