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:ar.gob.ambiente.servicios.gestionpersonas.entidades.util.CriptPass.java

/**
 * Mtodo para desencriptar una contrasea encriptada
 * @param textoEncriptado/*  w  ww  .  j  a  va 2 s.  c om*/
 * @return
 * @throws Exception 
 */
public static String desencriptar(String textoEncriptado) throws Exception {

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

    try {
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));
        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 decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key);

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

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

    } catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException
            | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
        System.out.println(ex.getMessage());
    }
    return base64EncryptedString;
}

From source file:ar.gob.ambiente.servicios.gestionpersonas.entidades.util.CriptPass.java

/**
 * Mtodo para encriptar las contraseas/* w w w . j a v  a  2 s .  c  om*/
 * @param texto
 * @return 
 */
public static String encriptar(String texto) {

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

    try {

        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 (NoSuchAlgorithmException | UnsupportedEncodingException | NoSuchPaddingException
            | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
        System.out.println(ex.getMessage());
    }
    return base64EncryptedString;
}

From source file:de.dominicscheurer.passwords.SafePwdGen.java

/**
 * Computes an SHA256 hash from the given input.
 * /*from  w  w w  . j  ava2  s.c om*/
 * @param input
 *            Input to compute a hash from.
 * @return The hashed input.
 * @throws NoSuchAlgorithmException
 *             Thrown if the algorithm "SHA-256" is not present in the
 *             system.
 */
private static String sha256(String input) throws NoSuchAlgorithmException {
    MessageDigest mDigest = MessageDigest.getInstance("SHA-256");
    byte[] result = mDigest.digest(input.getBytes());
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < result.length; i++) {
        sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
    }

    return sb.toString();
}

From source file:Componentes.EncryptionMD5.java

public static String Desencriptar(String encriptado) {
    System.out.println(encriptado);
    String secretKey = "qualityinfosolutions"; //llave para encriptar datos
    String base64EncryptedString = "";

    try {/*from w w w. j a v  a2  s  . c om*/
        System.out.println("h");
        byte[] message = Base64.decodeBase64(encriptado.getBytes("utf-8"));
        System.out.println("b");
        MessageDigest md = MessageDigest.getInstance("MD5");
        System.out.println("a");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        System.out.println("c");
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        System.out.println("g");
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        System.out.println("w");
        Cipher decipher = Cipher.getInstance("DESede");
        System.out.println("t");
        decipher.init(Cipher.DECRYPT_MODE, key);
        System.out.println("r");
        System.out.println(Arrays.toString(message));
        byte[] plainText = decipher.doFinal(message);
        System.out.println(Arrays.toString(plainText));
        base64EncryptedString = new String(plainText, "UTF-8");

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

From source file:io.personium.core.auth.AuthUtils.java

/**
 * ?Hash?./* w  ww . j a v  a2s .c  o m*/
 * @param passwd 
 * @return ???
 */
public static String hashPassword(final String passwd) {
    if (passwd == null) {
        return null;
    }

    // DC0 Ruby Code
    // Digest::SHA256.hexdigest(pw + "Password hash salt value")
    String str2hash = passwd + PersoniumUnitConfig.getAuthPasswordSalt();
    try {
        MessageDigest md = MessageDigest.getInstance(MD_ALGORITHM);
        byte[] digestBytes = md.digest(str2hash.getBytes(CharEncoding.UTF_8));
        // ???????????DC0?????????????
        return PersoniumCoreUtils.byteArray2HexString(digestBytes);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.sharetask.utility.HashCodeUtil.java

public static String getShortHashCode(final String data) {
    try {//from  w  ww .ja va  2  s.  c o m
        final MessageDigest mda = MessageDigest.getInstance("SHA-1");
        final String baseSalt = String.valueOf(System.currentTimeMillis());
        final byte[] digest = mda.digest(baseSalt.getBytes(Charset.forName("UTF-8")));
        return new String(Hex.encode(digest));
    } catch (final NoSuchAlgorithmException e) {
        throw new UnsupportedOperationException(e);
    }
}

From source file:Main.java

/**
 * //  w w w  . j  av  a2  s . c  o  m
 * @param word
 *            the string to be hashed
 * @return SHA1-hash of the word
 */
public static String hash(String word) {
    byte[] data = word.getBytes();
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    byte[] b = md.digest(data);
    String result = "";
    for (int i = 0; i < b.length; i++) {
        result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
    }
    return result;
}

From source file:org.sharetask.utility.HashCodeUtil.java

public static String getHashCode(final String data) {
    try {/* ww  w.  ja  v a2  s .  c o m*/
        final MessageDigest mda = MessageDigest.getInstance("SHA-512");
        final String baseSalt = String.valueOf(System.currentTimeMillis());
        final byte[] digest = mda.digest(baseSalt.getBytes(Charset.forName("UTF-8")));
        return new String(Hex.encode(digest));
    } catch (final NoSuchAlgorithmException e) {
        throw new UnsupportedOperationException(e);
    }
}

From source file:io.seldon.memcache.SecurityHashPeer.java

public static String md5(String value) {
    try {//w w  w. jav  a  2  s.c  om
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] result = md5.digest(value.getBytes());
        return hexEncode(result);
    } catch (NoSuchAlgorithmException e) {
        logger.error("Failed to get MD5 digest ", e);
        return value;
    }
}

From source file:com.wootric.androidsdk.utils.SHAUtil.java

public static String buildUniqueLink(String accountToken, String endUserEmail, long date, String randomString) {
    String unixTimestamp = String.valueOf(date);
    String randomText = randomString;

    String text = accountToken + endUserEmail + unixTimestamp + randomText;

    MessageDigest mDigest = null;

    String uniqueLink = null;// w ww.  j av a  2s .c  om
    try {
        mDigest = MessageDigest.getInstance("SHA-256");
        byte[] result = mDigest.digest(text.getBytes());
        uniqueLink = new String(Hex.encodeHex(result));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    return uniqueLink;
}