Search in sources :

Example 21 with MessageDigest

use of java.security.MessageDigest in project cas by apereo.

the class DigestUtils method rawDigest.

/**
     * Computes digest.
     *
     * @param alg  Digest algorithm to use
     * @param data data to be hashed
     * @return hash
     */
public static byte[] rawDigest(final String alg, final byte[] data) {
    try {
        final MessageDigest digest = MessageDigest.getInstance(alg);
        digest.reset();
        return digest.digest(data);
    } catch (final Exception cause) {
        throw new SecurityException(cause);
    }
}
Also used : MessageDigest(java.security.MessageDigest)

Example 22 with MessageDigest

use of java.security.MessageDigest in project crate by crate.

the class Blobs method digest.

public static byte[] digest(String content) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.reset();
        digest.update(content.getBytes(StandardCharsets.UTF_8));
        return digest.digest();
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 23 with MessageDigest

use of java.security.MessageDigest in project sockjs-netty by cgbystrom.

the class IframePage method generateMd5.

private static String generateMd5(String value) {
    String encryptedString = null;
    byte[] bytesToBeEncrypted;
    try {
        // convert string to bytes using a encoding scheme
        bytesToBeEncrypted = value.getBytes("UTF-8");
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] theDigest = md.digest(bytesToBeEncrypted);
        // convert each byte to a hexadecimal digit
        Formatter formatter = new Formatter();
        for (byte b : theDigest) {
            formatter.format("%02x", b);
        }
        encryptedString = formatter.toString().toLowerCase();
    } catch (UnsupportedEncodingException e) {
        // FIXME: Return proper HTTP error
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // FIXME: Return proper HTTP error
        e.printStackTrace();
    }
    return encryptedString;
}
Also used : Formatter(java.util.Formatter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 24 with MessageDigest

use of java.security.MessageDigest in project UltimateAndroid by cymcsg.

the class TripleDES method decrypt.

/**
     * Decrypt the message with TripleDES
     *
     * @param message
     * @return
     * @throws Exception
     */
public static String decrypt(String message) throws Exception {
    if (message == null || message == "")
        return "";
    byte[] values = Base64decoding(message, 0);
    final MessageDigest md = MessageDigest.getInstance("SHA-1");
    final byte[] digestOfPassword = md.digest(token.getBytes("utf-8"));
    final byte[] keyBytes = copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8; ) {
        keyBytes[k++] = keyBytes[j++];
    }
    final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    String s1 = "12345678";
    byte[] bytes = s1.getBytes();
    final IvParameterSpec iv = new IvParameterSpec(bytes);
    final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    decipher.init(Cipher.DECRYPT_MODE, key, iv);
    final byte[] plainText = decipher.doFinal(values);
    return new String(plainText, "UTF-8");
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest)

Example 25 with MessageDigest

use of java.security.MessageDigest in project UltimateAndroid by cymcsg.

the class TripleDES method decrypt.

/**
     * Decrypt the message with TripleDES
     *
     * @param message
     * @return
     * @throws Exception
     */
public static String decrypt(byte[] message) throws Exception {
    byte[] values = Base64decodingByte(message, 0);
    final MessageDigest md = MessageDigest.getInstance("SHA-1");
    final byte[] digestOfPassword = md.digest(token.getBytes("utf-8"));
    final byte[] keyBytes = copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8; ) {
        keyBytes[k++] = keyBytes[j++];
    }
    final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    String s1 = "12345678";
    byte[] bytes = s1.getBytes();
    final IvParameterSpec iv = new IvParameterSpec(bytes);
    final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    decipher.init(Cipher.DECRYPT_MODE, key, iv);
    final byte[] plainText = decipher.doFinal(values);
    return new String(plainText, "UTF-8");
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest)

Aggregations

MessageDigest (java.security.MessageDigest)1122 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)562 IOException (java.io.IOException)161 UnsupportedEncodingException (java.io.UnsupportedEncodingException)93 BigInteger (java.math.BigInteger)93 FileInputStream (java.io.FileInputStream)68 InputStream (java.io.InputStream)66 File (java.io.File)58 DigestInputStream (java.security.DigestInputStream)56 Test (org.junit.Test)55 ByteArrayOutputStream (java.io.ByteArrayOutputStream)46 DigestOutputStream (java.security.DigestOutputStream)43 ArrayList (java.util.ArrayList)32 ByteArrayInputStream (java.io.ByteArrayInputStream)29 OutputStream (java.io.OutputStream)27 X509Certificate (java.security.cert.X509Certificate)27 GeneralSecurityException (java.security.GeneralSecurityException)25 Cipher (javax.crypto.Cipher)23 Provider (java.security.Provider)22 SecretKeySpec (javax.crypto.spec.SecretKeySpec)22