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(byte[] input) 

Source Link

Document

Performs a final update on the digest using the specified array of bytes, then completes the digest computation.

Usage

From source file:net.firejack.aws.license.LicenseHelper.java

private static void signature(License license) throws UnsupportedEncodingException, NoSuchAlgorithmException {
    StringBuilder builder = new StringBuilder();
    builder.append(license.getExpired()).append(license.getName()).append(license.getSession());
    byte[] bytes = builder.toString().getBytes("UTF-8");

    for (int i = 0; i < bytes.length; i += 2)
        bytes[i] += i;//from w  ww  .jav  a 2s  . c  o m

    MessageDigest digest = MessageDigest.getInstance("MD5");
    license.setSignature(new String(Base64.encodeBase64(digest.digest(bytes))));
}

From source file:ID.java

public static byte[] getMD5Bytes(String content) {
    try {//from w w  w.  j a v a  2 s  .  co m
        MessageDigest digest = MessageDigest.getInstance("MD5");
        return digest.digest(content.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (java.io.UnsupportedEncodingException uee) {
        throw new IllegalStateException(uee);
    }
}

From source file:Main.java

public static String MD5(String md5) {
    try {/*from  w  w  w. jav  a2 s . c o  m*/
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array;
        try {
            array = md.digest(md5.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            array = md.digest(md5.getBytes());
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
    }
    return null;
}

From source file:Main.java

public static String generateDeviceUUID(Context context) {
    String serial = android.os.Build.SERIAL;
    String androidID = Settings.Secure.ANDROID_ID;
    String deviceUUID = serial + androidID;

    /*//w ww  .  j  a  va 2  s. com
     * SHA-1
     */
    MessageDigest digest;
    byte[] result;
    try {
        digest = MessageDigest.getInstance("SHA-1");
        result = digest.digest(deviceUUID.getBytes("UTF-8"));
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    StringBuilder sb = new StringBuilder();
    for (byte b : result) {
        sb.append(String.format("%02X", b));
    }

    return sb.toString();
}

From source file:com.ai.bss.util.user.DigestUtils.java

/**
 * Calculate the MD5 hash for a given string.
 *
 * @param text The string to calculate the hash for
 * @return The hex representation of the hash value
 *///from  www .j av  a2  s  .c  om
public static String md5(String text) {
    Assert.notNull(text);
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        return hex(md.digest(text.getBytes()));
    } catch (NoSuchAlgorithmException ex) {
        throw new IllegalStateException(
                "Unable to calculate hash. No MD5 hasher available in this Java implementation", ex);
    }
}

From source file:dk.teachus.backend.dao.hibernate.PasswordUserType.java

public static String sha1(String message) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    return hex(md.digest(message.getBytes("CP1252")));
}

From source file:com.juicioenlinea.application.secutiry.Security.java

public static String encriptar(String texto) {

    final String secretKey = "hunter"; //llave para encriptar datos
    String encryptedString = null;

    try {// ww  w. ja  v  a  2 s  . c  o m

        MessageDigest md = MessageDigest.getInstance("SHA");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("UTF-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

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

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

    } catch (NoSuchAlgorithmException | UnsupportedEncodingException | NoSuchPaddingException
            | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
        Logger.getLogger(Security.class.getName()).log(Level.SEVERE, null, ex);
    }
    return encryptedString;
}

From source file:com.juicioenlinea.application.secutiry.Security.java

public static String desencriptar(String textoEncriptado) {

    final String secretKey = "hunter"; //llave para encriptar datos
    String encryptedString = null;

    try {/* w  w w.ja v a 2s. c om*/
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("UTF-8"));
        MessageDigest md = MessageDigest.getInstance("SHA");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("UTF-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");

        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainText = decipher.doFinal(message);

        encryptedString = new String(plainText, "UTF-8");

    } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException
            | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException ex) {
        Logger.getLogger(Security.class.getName()).log(Level.SEVERE, null, ex);
    }

    return encryptedString;
}

From source file:controlpac.EncryptHelper.java

public static String Encriptar(String texto) {
    String base64EncryptedString = "";
    try {//from  ww  w. java 2  s  .co  m
        MessageDigest md = MessageDigest.getInstance("MD5"); //Crea un hash con la clave elegida
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede"); //Crea la clave
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);//Inicializa el cifrado con la clave
        byte[] plainTextBytes = texto.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);//Cifra el texto
        byte[] base64Bytes = Base64.encodeBase64(buf);//Encodea el texto en base64
        base64EncryptedString = new String(base64Bytes);
    } catch (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:com.ai.bss.util.user.DigestUtils.java

/**
 * Calculate the SHA1 hash for a given string.
 *
 * @param text the given text to hash to a SHA1
 * @return the SHA1 Hash//  w w  w. j a va 2s .com
 */
public static String sha1(String text) {
    Assert.notNull(text);
    try {
        MessageDigest md = MessageDigest.getInstance("SHA1");
        return hex(md.digest(text.getBytes("UTF-8")));
    } catch (NoSuchAlgorithmException ex) {
        throw new IllegalStateException(
                "Unable to calculate hash. No SHA1 hasher available in this Java implementation", ex);
    } catch (UnsupportedEncodingException ex) {
        throw new IllegalStateException(
                "Unable to calculate hash. UTF-8 encoding is not available in this Java implementation", ex);
    }
}