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:br.com.gerenciapessoal.service.CadastroUsuarioService.java

@SuppressWarnings("null")
public static String md5(String input) {
    String md5 = null;/*from  w w w . ja  va2  s  .  c  o  m*/
    if (!StringUtils.isNotBlank(input)) {
        return null;
    }
    try {
        //Create MessageDigest object for MD5           
        MessageDigest digest = MessageDigest.getInstance("MD5");
        //Update input string in message digest           
        digest.update(input.getBytes(), 0, input.length());
        //Converts message digest value in base 16 (hex)            
        md5 = new BigInteger(1, digest.digest()).toString(16);
    } catch (NoSuchAlgorithmException e) {
    }
    return md5.trim();
}

From source file:Main.java

public static String get_device_id(Context ctx) {

    final TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);

    String tmDevice = "";
    String tmSerial = null;/*from   w ww  . j a  v a 2s. c  o m*/
    String androidId = null;

    try {
        tmDevice = "" + tm.getDeviceId();
    } catch (Exception ex) {
    }

    try {
        tmSerial = "" + tm.getSimSerialNumber();
    } catch (Exception ex) {
    }
    try {
        androidId = "" + android.provider.Settings.Secure.getString(ctx.getContentResolver(),
                android.provider.Settings.Secure.ANDROID_ID);
    } catch (Exception ex) {
    }
    try {
        UUID deviceUuid = new UUID(androidId.hashCode(),
                ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
        String deviceId = deviceUuid.toString();

        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(deviceId.getBytes());

        byte byteData[] = md.digest();

        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }
        deviceId = sb.toString();

        return deviceId;
    } catch (Exception ex) {
    }
    return "nodeviceid";
}

From source file:Main.java

public static String getMessageDigest(byte[] paramArrayOfByte) {
    char[] arrayOfChar1 = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102 };
    String str;// w  w w.  jav a2s.  com
    try {
        MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
        localMessageDigest.update(paramArrayOfByte);
        byte[] arrayOfByte = localMessageDigest.digest();
        int i = arrayOfByte.length;
        char[] arrayOfChar2 = new char[i * 2];
        int j = 0;
        int k = 0;
        while (true) {
            if (j >= i) {
                str = new String(arrayOfChar2);
                break;
            }
            int m = arrayOfByte[j];
            int n = k + 1;
            arrayOfChar2[k] = arrayOfChar1[(0xF & m >>> 4)];
            k = n + 1;
            arrayOfChar2[n] = arrayOfChar1[(m & 0xF)];
            j++;
        }
    } catch (Exception localException) {
        str = null;
    }
    return str;
}

From source file:com.yilang.commons.utils.util.Digests.java

private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {/*from  w w w.  j  ava2 s. c o  m*/
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:HashUtil.java

public static String getMD5(final String data) {
    try {/* ww w  . j  a  v a 2  s  .  c  o m*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(data.getBytes());
        BigInteger bigInt = new BigInteger(1, m.digest());
        String hashtext = bigInt.toString(16);
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
        return hashtext;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return e.getMessage();
    }
}

From source file:cn.zhuqi.mavenssh.ext.util.Coder.java

/**
 * MD5/*from  w w  w.  ja  v  a  2 s  .  co  m*/
 *
 * @param data
 * @return
 * @throws Exception
 */
public static byte[] encryptMD5(byte[] data) throws Exception {
    MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
    md5.update(data);
    return md5.digest();
}

From source file:org.starfishrespect.myconsumption.android.util.CryptoUtils.java

/**
 * Return a Base64 encoded String of the hash(input) (SHA 256)
 * @param input a String to encode/*from  www.  java  2s.co  m*/
 * @return a Base64 encoded String of the hash(input) (SHA 256)
 */
public static String sha256(String input) {
    MessageDigest digest = null;
    try {
        digest = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        LOGE(TAG, e.toString());
    }
    byte[] hash = new byte[0];
    if (digest != null) {
        try {
            hash = digest.digest(input.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            LOGE(TAG, e.toString());
        }

    }
    return Base64.encodeToString(hash, Base64.NO_WRAP);
}

From source file:Main.java

public static String SHA1(String text) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(text.getBytes());/*w w  w  . j  a va2 s. co m*/

    byte byteData[] = md.digest();

    //convert the byte to hex format method 1
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < byteData.length; i++) {
        sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
    }

    //System.out.println("Hex format : " + sb.toString());

    //convert the byte to hex format method 2
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < byteData.length; i++) {
        String hex = Integer.toHexString(0xff & byteData[i]);
        if (hex.length() == 1)
            hexString.append('0');
        hexString.append(hex);
    }
    return hexString.toString();
}

From source file:ch.windmobile.server.social.mongodb.util.AuthenticationServiceUtil.java

public static String createSHA1(String email, byte[] pwd) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA1");
    String base = email + ":" + new String(pwd);
    byte[] result = md.digest(base.getBytes());
    return new String(Base64.encode(result));
}

From source file:com.formatAdmin.utils.UtilsSecurity.java

public static String cifrarMD5(String texto) {
    String base64EncryptedString = "";
    try {//from www.j  av  a  2s .  c  o  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(SECRET_MD5_KEY.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

        SecretKey key = new SecretKeySpec(keyBytes, ENCRYPT_ALGORITHM);
        Cipher cipher = Cipher.getInstance(ENCRYPT_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = texto.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        base64EncryptedString = new String(base64Bytes);

    } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException | BadPaddingException
            | IllegalBlockSizeException | NoSuchPaddingException ex) {
    }
    return base64EncryptedString;
}