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

private static String md5(String origin) {
    try {//from w ww  . j  a va  2s. co  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(origin.getBytes("UTF-8"));
        BigInteger bi = new BigInteger(1, md.digest());

        return bi.toString(16);
    } catch (Exception e) {
        return getUuid();
    }
}

From source file:Main.java

public static String md5(String input) {
    try {//from  w  w w  .  j  av  a  2 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:Main.java

public static String encode(String input) {
    try {// w  w  w . ja v  a 2  s.  c  o  m
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] inputByteArray = input.getBytes();
        messageDigest.update(inputByteArray);
        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:net.cheesecan.cheeselobby.session.MD5Base64Hash.java

public static String encrypt(String str) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(str.getBytes());/*from www.j  a v  a2  s .c  om*/
    byte[] enc = md.digest();
    return new String(new Base64().encode(enc));
}

From source file:Main.java

/***
 * compute hash for text//ww  w . ja va 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

private static String generateSHA256(String offThis) {
    String result = "";
    try {/* w w w .  ja  va 2s  .  c om*/
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(offThis.getBytes());
        byte mdBytes[] = md.digest();
        result = bytesToHex(mdBytes);
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

/**
 * Hash a string//w ww .j a v  a 2  s  .c  o  m
 *
 * @param toHash String to be hashed.
 *
 * @return Hashed string.
 */
public static String hash(Object toHash) {
    String hashString = toHash.toString();
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        return hashString;
    }

    md.update(hashString.getBytes(), 0, hashString.length());
    return convertToHex(md.digest());
}

From source file:com.innoq.liqid.utils.SHACrypt.java

public static String encrypt(final String plaintext) {
    MessageDigest md = null;//from  w  w  w.ja  va 2 s  . co  m
    try {
        md = MessageDigest.getInstance("SHA");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage());
    }
    try {
        md.update(plaintext.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e.getMessage());
    }
    byte[] raw = md.digest();
    return Base64.encodeBase64String(raw);
}

From source file:Main.java

private static String SHA1(String text) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA-1");

    byte[] sha1hash = new byte[40];
    md.update(text.getBytes());/* w w w .j av  a2  s.  c om*/
    sha1hash = md.digest();

    return convertToHex(sha1hash);
}

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 www .jav  a 2 s  . com

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