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

/**
 * Prints your current certificate signature to the Logcat. Use this method to obtain your certificate signature.
 *
 * @param context The application context.
 *//*from  w w  w .  j  av a2s  . c  o m*/

public static void getCertificateSignature(Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SIGNATURES);

        // The APK is signed with multiple signatures, probably it was tampered.
        if (packageInfo.signatures.length > 1) {
            return;
        }

        for (Signature signature : packageInfo.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");

            md.update(signature.toByteArray());

            Log.d("TAMPERING_PROTECTION", "**" + Base64.encodeToString(md.digest(), Base64.DEFAULT) + "**");
        }
    } catch (Exception exception) {
        Log.d("TAMPERING_PROTECTION", exception.getStackTrace().toString());
    }
}

From source file:jp.co.opentone.bsol.framework.core.util.HashUtil.java

/**
 * ??(?)??(UUID.randomUUID()).//from  ww  w .  j  a v  a 2 s . co m
 * @param   parameter ????UUID??????
 * @return  ?
 */
public static String getRandomString(String parameter) {
    String result = null;
    String salt = parameter + UUID.randomUUID().toString();

    try {
        MessageDigest md = MessageDigest.getInstance(ALGORITHM);
        md.update(salt.getBytes());
        result = new String(Hex.encodeHex(md.digest()));
    } catch (NoSuchAlgorithmException e) {
        // ALGORITHM?????????????
        throw new RuntimeException(e);
    }
    return result;
}

From source file:Main.java

/**
 * A double check about app signature that was passed by MainActivity as facetID.
 * @param facetId a string value composed by app hash. I.e. android:apk-key-hash:Lir5oIjf552K/XN4bTul0VS3GfM
 * @param context Application Context//  w  w w .j a v  a 2s . co m
 * @return true if the signature executed on runtime matches if signature sent by MainActivity
 */
private static boolean checkAppSignature(String facetId, Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (Signature sign : packageInfo.signatures) {
            byte[] sB = sign.toByteArray();
            MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
            messageDigest.update(sign.toByteArray());
            String currentSignature = Base64.encodeToString(messageDigest.digest(), Base64.DEFAULT);
            if (currentSignature.toLowerCase().contains(facetId.split(":")[2].toLowerCase())) {
                return true;
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return false;
}

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 a2  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:ca.qc.adinfo.rouge.util.MD5.java

public static String hash(String value) {

    try {/*  www  .  ja  va  2 s.c  o  m*/

        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] md5hash = new byte[32];
        md.update(value.getBytes("iso-8859-1"), 0, value.length());
        md5hash = md.digest();

        return Hex.encodeHexString(md5hash);

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String md5(String input) {
    try {/*from   w w  w .ja  va2 s. c  o m*/
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(input.getBytes());
        byte[] resultByteArray = messageDigest.digest();
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] resultCharArray = new char[resultByteArray.length * 2];
        int index = 0;
        for (byte b : resultByteArray) {
            resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
            resultCharArray[index++] = hexDigits[b & 0xf];
        }
        return new String(resultCharArray);
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

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  ww  .jav  a  2 s. c o m
 * @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:com.wso2telco.gsma.authenticators.Utility.java

/**
 * Returns the sha256 digest for a given string
 * @param input for the retrieving the sha256 hash
 */// ww w.j av a2  s  .  c om
public static String generateSHA256Hash(String input) throws AuthenticationFailedException {
    String returnValue = null;
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(input.getBytes());
        byte byteData[] = md.digest();
        //convert the byte to hex format
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }
        returnValue = sb.toString();
    } catch (Exception e) {
        throw new AuthenticationFailedException("Failure while hashing the input value", e);
    }
    return returnValue;
}

From source file:br.com.manish.ahy.kernel.util.HashUtil.java

public static String getHash(String text) {
    String ret = null;/*from ww w. j  a v a 2s . c om*/
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(text.getBytes());
        ret = getHex(md.digest());
    } catch (NoSuchAlgorithmException e) {
        log.error(e);
        throw new OopsException(e, "Hash Error.");
    }
    return ret;
}

From source file:Main.java

public static String sha1Hash(String text) {
    String hash = null;/* w  w  w  . jav a 2 s .com*/
    try {
        final MessageDigest digest = MessageDigest.getInstance("SHA-1");
        final byte[] bytes = text.getBytes("UTF-8");
        digest.update(bytes, 0, bytes.length);
        hash = convertToHex(digest.digest());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return hash;
}