Example usage for java.security MessageDigest getInstance

List of usage examples for java.security MessageDigest getInstance

Introduction

In this page you can find the example usage for java.security MessageDigest getInstance.

Prototype

public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a MessageDigest object that implements the specified digest algorithm.

Usage

From source file:com.apexxs.neonblack.utilities.MD5Utilities.java

public static String getMD5Hash(String s) {
    String md5Hash = StringUtils.EMPTY;
    try {//from  w  w w .j a v  a 2 s . c  o m
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();
        byte[] bytes = md.digest(s.getBytes("UTF-8"));
        md5Hash = String.format("%032x", new BigInteger(1, bytes));
    } catch (Exception ex) {
        logger.error("Error generating MD5 hash from " + s + "\n" + ex.getMessage());
    }
    return md5Hash;
}

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

/**
 * , ?md5sha1.//from www. ja va 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) {
        return null;
    }
}

From source file:com.myb.portal.util.Digests.java

/**
 * , ?md5sha1.//from w  ww.j  a v  a2s.  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);
    }
    return salt;
}

From source file:com.appdynamicspilot.util.MD5.java

/**
 * Returns the hashed value of <code>clear</code>.
 *///  ww  w  . j  a v a2  s.c o m
public static String hash(String clear) throws Exception {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] b = md.digest(clear.getBytes());

        int size = b.length;
        StringBuffer h = new StringBuffer(size);
        for (int i = 0; i < size; i++) {
            int u = b[i] & 255; // unsigned conversion
            if (u < 16) {
                h.append("0" + Integer.toHexString(u));
            } else {
                h.append(Integer.toHexString(u));
            }
        }
        return h.toString();
    } catch (Exception e) {
        throw new Exception(e);
    }
}

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

/**
 * , ?md5sha1.//  w  w  w . j  a  v a2s  . 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:club.jmint.crossing.specs.Security.java

public static String MD5(String str) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    try {//from   w  w w. j  a  v  a2s  .c o  m
        byte[] btInput = str.getBytes();
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        mdInst.update(btInput);
        byte[] md = mdInst.digest();

        int j = md.length;
        char mdstr[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = md[i];
            mdstr[k++] = hexDigits[byte0 >>> 4 & 0xf];
            mdstr[k++] = hexDigits[byte0 & 0xf];
        }
        return new String(mdstr);
    } catch (Exception e) {
        CrossLog.printStackTrace(e);
        return null;
    }
}

From source file:com.ai.bss.util.user.DigestUtils.java

/**
 * Calculate the SHA1 hash for a given string.
 *
 * @param text the given text to hash to a SHA1
 * @return the SHA1 Hash/*from   w  ww .  j  ava2 s  .c o  m*/
 */
public static String sha1(String text) {
    Assert.notNull(text);
    try {
        MessageDigest md = MessageDigest.getInstance("SHA1");
        return hex(md.digest(text.getBytes("UTF-8")));
    } catch (NoSuchAlgorithmException ex) {
        throw new IllegalStateException(
                "Unable to calculate hash. No SHA1 hasher available in this Java implementation", ex);
    } catch (UnsupportedEncodingException ex) {
        throw new IllegalStateException(
                "Unable to calculate hash. UTF-8 encoding is not available in this Java implementation", ex);
    }
}

From source file:com.exam.mserver.common.persistence.util.Digests.java

/**
 * , ?md5sha1.// w w w. j a  v 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 new RuntimeException(e);
    }
}

From source file:com.codemarvels.ant.aptrepotask.utils.Utils.java

/**
 * Compute the given message digest for a file.
 * /*from   w w  w  . ja  v a 2 s .  co m*/
 * @param hashType algorithm to be used (as {@code String})
 * @param file File to compute the digest for (as {@code File}).
 * @return A {@code String} for the hex encoded digest.
 * @throws MojoExecutionException
 */
public static String getDigest(String hashType, File file) {
    try {
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        MessageDigest digest = MessageDigest.getInstance(hashType);
        DigestInputStream dis = new DigestInputStream(bis, digest);
        @SuppressWarnings("unused")
        int ch;
        while ((ch = dis.read()) != -1)
            ;
        String hex = new String(Hex.encodeHex(digest.digest()));
        fis.close();
        bis.close();
        dis.close();
        return hex;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("could not create digest", e);
    } catch (FileNotFoundException e) {
        throw new RuntimeException("could not create digest", e);
    } catch (IOException e) {
        throw new RuntimeException("could not create digest", e);
    }
}

From source file:com.cyphermessenger.utils.Utils.java

public static byte[] sha256(byte[] s) {
    try {/*from  ww  w  .  ja  v  a2  s  .  c om*/
        return MessageDigest.getInstance("SHA-256").digest(s);
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}