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:in.mtap.iincube.mongoser.codec.crypto.Psyfer.java

public static Psyfer getInstance(String secretKey) throws NoSuchAlgorithmException,
        UnsupportedEncodingException, NoSuchPaddingException, InvalidKeyException {
    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    byte[] key = digest.digest(secretKey.getBytes("UTF-8"));
    key = Arrays.copyOf(key, 16);
    SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
    Cipher eCipher = Cipher.getInstance("AES");
    Cipher deCipher = Cipher.getInstance("AES");
    eCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    deCipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    return new Psyfer(eCipher, deCipher);
}

From source file:de.topicmapslab.majortom.server.http.util.MD5Util.java

/**
 * Calculates the md5 sum of the given string
 * //w  ww  .  j a  v a  2 s .c  om
 * @param source the source string used to calculate
 * @return the String representing the MD5 sum
 * @throws NoSuchAlgorithmException  if the MD5 algorithm is not available
 */
public static final String calculateMD5(String source) {
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] bytes = digest.digest(source.getBytes());

        final int nBytes = bytes.length;
        char[] result = new char[2 * nBytes];

        int j = 0;
        for (int i = 0; i < nBytes; i++) {
            // Char for top 4 bits
            result[j++] = HEX[(0xF0 & bytes[i]) >>> 4];
            // Bottom 4
            result[j++] = HEX[(0x0F & bytes[i])];
        }

        return new String(result);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Could not calculate md5", e);
    }
}

From source file:info.track_mate.util.EncryptionUtils.java

/**
 * Encrypt the specified String./*  w  w  w . ja  v a  2 s.c  om*/
 * @param toEncrypt The String to encrypt.
 * @param algorithm The algorithm to use for the encryption. See Appendix A in the
 * <a href="../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">Java Cryptography Architecture API Specification &amp; Reference </a>
 * for information about standard algorithm names.
 * @param encodeAsBase64 If true then encode the output of the encryption algorithm as Base64.
 * @return The encrypted String.
 * @throws NoSuchAlgorithmException If the specified algorithm doesn't exist or is unavailable.
 */
public static String encryptString(String toEncrypt, String algorithm, boolean encodeAsBase64)
        throws NoSuchAlgorithmException {
    MessageDigest md5Digest = MessageDigest.getInstance(algorithm);
    byte[] encryptedBytes = md5Digest.digest(toEncrypt.getBytes());
    if (encodeAsBase64) {
        encryptedBytes = Base64.encodeBase64(encryptedBytes);
    }
    String encryptedString = new String(encryptedBytes);
    return encryptedString;
}

From source file:com.eschava.forevernote.EvernoteUtil.java

public static Resource createResource(InputStream stream, String contentType) throws IOException {
    try {/*from  www . j  a v a2 s  . c  om*/
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        FileCopyUtils.copy(stream, outputStream);
        byte[] bytes = outputStream.toByteArray();

        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] hash = messageDigest.digest(bytes);

        Data data = new Data();
        data.setBody(bytes);
        data.setBodyHash(hash);

        Resource resource = new Resource();
        resource.setMime(contentType);
        resource.setData(data);

        return resource;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e); // is not possible
    }
}

From source file:com.basp.trabajo_al_minuto.model.business.BusinessSecurity.java

/**
 * Se encarga de encriptar la contrasea ingresada por el usuario *
 *///w w w.  ja  va 2 s .  c o m
public static String encrypt(String value) throws BusinessException {
    String secretKey = "e-business";
    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 = value.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        base64EncryptedString = new String(base64Bytes);
    } catch (Exception ex) {
        throw new BusinessException(ex);
    }
    return base64EncryptedString;
}

From source file:com.basp.trabajo_al_minuto.model.business.BusinessSecurity.java

/**
 * Se encarga de desencriptar la contrasea ingresada por el usuario *
 *//*from w  w w  .j  av a  2s . co  m*/
public static String decrypt(String encryptValue) throws BusinessException {
    String secretKey = "e-business";
    String base64EncryptedString = "";

    try {
        byte[] message = Base64.decodeBase64(encryptValue.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 (Exception ex) {
        throw new BusinessException(ex);
    }
    return base64EncryptedString;
}

From source file:Main.java

/**
 * Returns the {@link Certificate} fingerprint as returned by <code>keytool</code>.
 *
 * @param certificate//  w ww .  j a v a2 s . co  m
 * @param hashAlgorithm
 */
public static String getFingerprint(Certificate cert, String hashAlgorithm) {
    if (cert == null) {
        return null;
    }
    try {
        MessageDigest digest = MessageDigest.getInstance(hashAlgorithm);
        return toHexadecimalString(digest.digest(cert.getEncoded()));
    } catch (NoSuchAlgorithmException e) {
        // ignore
    } catch (CertificateEncodingException e) {
        // ignore
    }
    return null;
}

From source file:Main.java

public static String GetHash(byte[] data) {
    MessageDigest md;
    String hash = null;/*from  w w w  . j  ava 2s  .c  om*/
    try {
        md = MessageDigest.getInstance("MD5");
        hash = new BigInteger(1, md.digest(data)).toString(16);
    } catch (NoSuchAlgorithmException e) {
        Log.i("BMSUtil", "Hashing Error!");
        e.printStackTrace();
    }
    return hash;
}

From source file:lib.clases_cripto.java

public static String Desencriptar(String textoEncriptado) throws Exception {

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

    try {/*w  w w . j  a  v a 2s .co m*/
        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 (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:BD.Encriptador.java

public static String Desencriptar(String textoEncriptado) throws Exception {

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

    try {//from w  w  w. j a va  2 s . c  om
        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 (Exception ex) {
    }
    return base64EncryptedString;
}