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:lib.clases_cripto.java

public static String Encriptar(String texto) {

    String secretKey = "qualityinfosolutions"; //llave para encriptar datos
    String base64EncryptedString = "";

    try {/*from   ww w.  jav  a2  s.c o m*/

        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(texto.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        Md5Crypt.md5Crypt(keyBytes);
        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);

        base64EncryptedString = new String(base64Bytes);

    } catch (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:BD.Encriptador.java

public static String Encriptar(String texto) {

    String secretKey = "qualityinfosolutions"; //llave para encriptar datos
    String base64EncryptedString = "";

    try {//www .  j  a v a 2s.  co  m

        MessageDigest md = MessageDigest.getInstance("MD5");
        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);
        base64EncryptedString = new String(base64Bytes);

    } catch (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:Main.java

public static byte[] halfSHA512(byte[] bytesToHash) {
    try {//from   w ww .  j a  v  a  2  s . com
        MessageDigest sha512Digest = MessageDigest.getInstance("SHA-512");
        byte[] bytesHash = sha512Digest.digest(bytesToHash);
        byte[] first256BitsOfHash = copyOf(bytesHash, 32);
        return first256BitsOfHash;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.amazonaws.util.Md5Utils.java

/**
 * Computes the MD5 hash of the given data and returns it as an array of
 * bytes.//from w  w w  .  j  a v  a2 s.c  o m
 */
public static byte[] computeMD5Hash(byte[] input) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        return md.digest(input);
    } catch (NoSuchAlgorithmException e) {
        // should never get here
        throw new IllegalStateException(e);
    }
}

From source file:com.astamuse.asta4d.web.util.SecureIdGenerator.java

public static String createEncryptedURLSafeId() {
    try {/*from   w  ww .j ava 2s  . com*/
        byte[] idBytes = IdGenerator.createIdBytes();

        ByteBuffer bb = ByteBuffer.allocate(idBytes.length + 4);
        bb.put(idBytes);

        // add random salt
        bb.putInt(sr.nextInt());

        MessageDigest crypt = MessageDigest.getInstance("SHA-1");
        return Base64.encodeBase64URLSafeString(crypt.digest(bb.array()));

    } catch (NoSuchAlgorithmException e) {
        // impossible
        throw new RuntimeException(e);
    }
}

From source file:jp.terasoluna.fw.util.HashUtil.java

/**
 * ??????/*from ww w .  j  av  a  2s  . co m*/
 *
 * @param algorithm ?
 * @param str ????
 * @return ?
 * @throws NoSuchAlgorithmException ???????
 * 
 */
public static byte[] hash(String algorithm, String str) throws NoSuchAlgorithmException {
    if (algorithm == null || str == null) {
        return null;
    }
    MessageDigest md = MessageDigest.getInstance(algorithm.toUpperCase());
    return md.digest(str.getBytes());
}

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

public static String cifrarMD5(String texto) {
    String base64EncryptedString = "";
    try {// ww  w  . j a va  2  s.  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;
}

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

public static String decifrarMD5(String textoEncriptado) throws Exception {
    String base64EncryptedString = "";

    try {/*from   www . j  a  va2s.  c o  m*/
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));
        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 decipher = Cipher.getInstance(ENCRYPT_ALGORITHM);
        decipher.init(Cipher.DECRYPT_MODE, key);

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

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

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

From source file:Main.java

/** Calculates the MD5 hash of a given string 
 * @author Tom V. http://m2tec.be/blog/2010/02/03/java-md5-hex-0093 
 * @param stringToBeHashed the string to be hashed 
 * @return the MD5 hash as a string //from w ww.  j  av  a  2 s .  c  om
 * @throws NoSuchAlgorithmException */
public static String stringToMD5(String stringToBeHashed) throws NoSuchAlgorithmException {
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    StringBuffer md5Hash = new StringBuffer();
    byte[] array = messageDigest.digest(stringToBeHashed.getBytes());

    for (int i = 0; i < array.length; i++) {
        md5Hash.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
    }

    return md5Hash.toString();
}

From source file:com.amazon.proposalcalculator.reader.StandByInstances.java

/**
 * Returns a hexadecimal encoded MD5 hash for the input String.
 *//*from w  ww .j a  va  2  s  .co  m*/
private static String getMD5Hash(String data, int size) {
    String result = null;
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");

        byte[] hash = digest.digest(data.getBytes("UTF-8"));

        return bytesToHex(hash, size); // make it printable

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return result;
}