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:com.exam.mserver.common.persistence.util.Digests.java

/**
 * , ?md5sha1./*from  w  w  w . ja v a 2 s . com*/
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.idea.quickstart.common.security.Digests.java

/**
 * , ?md5sha1.//from  w  w  w. ja  v  a 2 s  .  co  m
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
    }
    return null;
}

From source file:com.pfw.popsicle.common.Digests.java

/**
 * , ?md5sha1.//from  w  w w  . j  a  v a  2s .co m
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        return null;
    }
}

From source file:com.cmri.bpt.common.util.DigestUtil.java

/**
 * , ?md5sha1./*from  w  w w.j a v  a2 s.c  o  m*/
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        throw ExceptionUtil.asUnChecked(e);
    }
}

From source file:com.modoop.core.security.utils.Digests.java

/**
 * , ?md5sha1./*from   w w  w  .  j ava 2 s .  co  m*/
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        throw ExceptionUtils.unchecked(e);
    }
}

From source file:com.topsem.common.security.utils.Digests.java

/**
 * , ?md5sha1./*  w w  w  .  j  a v  a  2 s. c  o m*/
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        throw Throwables.propagate(e);
    }
}

From source file:apm.common.security.Digests.java

/**
 * , ?md5sha1.//  w  w w.jav a 2s  .c o  m
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:Main.java

private static byte[] getHash(String password) {
    MessageDigest digest = null;
    try {//w w w.j av a 2 s.  c  o  m
        digest = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    }
    if (digest != null) {
        digest.reset();
        return digest.digest(password.getBytes());
    } else {
        return null;
    }

}

From source file:fr.ortolang.diffusion.security.authentication.TicketHelper.java

public static Ticket decodeTicket(String ticket) {
    byte[] encryptedBytes = Base64.decodeBase64(ticket.getBytes());
    try {// w ww  .j a va 2 s .c o m
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        // Extract digest from decryptedBytes (MD5 length is 16 bytes)
        byte[] digest = Arrays.copyOfRange(decryptedBytes, decryptedBytes.length - 16, decryptedBytes.length);
        byte[] serializedMap = Arrays.copyOfRange(decryptedBytes, 0, decryptedBytes.length - 16);
        MessageDigest md = MessageDigest.getInstance(MESSAGE_DIGEST_ALGORITHM);
        if (!Arrays.equals(digest, md.digest(serializedMap))) {
            throw new DigestException();
        }
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedMap));
        return (Ticket) ois.readObject();
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException
            | IllegalBlockSizeException | IOException | ClassNotFoundException | DigestException e) {
        LOGGER.log(Level.SEVERE, "Error when decoding ticket: " + e.getMessage());
    }
    return null;
}

From source file:com.glaf.core.security.DigestUtil.java

public static String digestString(String password, String algorithm) {
    if (password == null || password.trim().length() == 0) {
        return password;
    }/*from w  w  w.j a v  a  2 s  .c o  m*/
    if (conf.getBoolean("password.enc", true)) {
        MessageDigest md = null;
        ByteArrayOutputStream bos = null;
        OutputStream encodedStream = null;
        try {
            md = MessageDigest.getInstance(algorithm);
            byte[] digest = md.digest(password.getBytes("UTF-8"));
            bos = new ByteArrayOutputStream();
            encodedStream = MimeUtility.encode(bos, "base64");
            encodedStream.write(digest);
            return bos.toString("UTF-8");
        } catch (IOException ioe) {
            throw new RuntimeException("Fatal error: " + ioe);
        } catch (NoSuchAlgorithmException ae) {
            throw new RuntimeException("Fatal error: " + ae);
        } catch (MessagingException me) {
            throw new RuntimeException("Fatal error: " + me);
        } finally {
            IOUtils.closeQuietly(bos);
            IOUtils.closeQuietly(encodedStream);
        }
    }
    return password;
}