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:Main.java

/**
 * Generate the fingerprint for a dedicated type.
 * @param cert the certificate/* ww  w. ja  v  a  2  s. c  om*/
 * @param type the type
 * @return the fingerprint
 * @throws CertificateException
 */
private static byte[] generateFingerprint(X509Certificate cert, String type) throws CertificateException {
    final byte[] fingerprint;
    final MessageDigest md;
    try {
        md = MessageDigest.getInstance(type);
    } catch (Exception e) {
        // This really *really* shouldn't throw, as java should always have a SHA-256 and SHA-1 impl.
        throw new CertificateException(e);
    }

    fingerprint = md.digest(cert.getEncoded());

    return fingerprint;
}

From source file:org.mobile.mpos.util.Common.java

/**
 * shar//w  w w.j av a 2  s . c om
 * @param source
 * @return
 */
public static String encryptSHA(String source) {
    try {
        MessageDigest sha = MessageDigest.getInstance("SHA");
        sha.update(source.getBytes(Consts.UTF_8));
        return ISOUtil.hexString(sha.digest());
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

From source file:de.bluepair.sci.client.SHAUtils.java

public static MessageDigest getDigest() {
    try {/*w  w  w.ja v a  2s .co  m*/
        return MessageDigest.getInstance("SHA-512");

    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(FileAnalysis.class.getName()).log(Level.SEVERE, null, ex);
        // try rescue with sha
        try {
            return MessageDigest.getInstance("SHA");
        } catch (NoSuchAlgorithmException ex1) {
            Logger.getLogger(FileAnalysis.class.getName()).log(Level.SEVERE, null, ex1);
            return null;
        }
    }
}

From source file:edu.ku.brc.specify.plugins.ipadexporter.MD5Checksum.java

/**
 * @param file//from  www.j  a  v a2s.  co  m
 * @return
 * @throws Exception
 */
public static String getMD5Checksum(final File file) throws Exception {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.reset();
    InputStream fis = new FileInputStream(file);
    try {
        //ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fis);
        DigestInputStream digestInputStream = new DigestInputStream(fis, md);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int ch;
        while ((ch = digestInputStream.read()) >= 0) {
            byteArrayOutputStream.write(ch);
        }

        byte[] newInput = byteArrayOutputStream.toByteArray();
        return org.apache.commons.codec.digest.DigestUtils.md5Hex(newInput);
    } finally {
        fis.close();
    }
}

From source file:Main.java

public static void encrypt(String fileIn, String fileOut, byte key[])
        throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    // Here you read the cleartext.
    FileInputStream fis = new FileInputStream(fileIn);
    // This stream write the encrypted text. This stream will be wrapped by another stream.
    FileOutputStream fos = new FileOutputStream(fileOut);

    // Length is 32 bytes
    //byte key[] = "1010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100001111000011".getBytes("UTF-8");
    MessageDigest sha = MessageDigest.getInstance("SHA-256");
    key = sha.digest(key);/*  w w w . j  a v a 2 s  .c om*/
    SecretKeySpec sks = new SecretKeySpec(key, "AES");
    // Create cipher
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, sks);
    // Wrap the output stream
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    // Write bytes
    int b;
    byte[] d = new byte[8];
    while ((b = fis.read(d)) != -1) {
        cos.write(d, 0, b);
    }
    // Flush and close streams.
    cos.flush();
    cos.close();
    fis.close();
}

From source file:support.AuthManager.java

public static String md5Custom(String st) {
    MessageDigest messageDigest = null;
    byte[] digest = new byte[0];

    try {//from  w w w. j ava2 s  .  c  o m
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(st.getBytes(StandardCharsets.UTF_8));
        digest = messageDigest.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    BigInteger bigInt = new BigInteger(1, digest);
    String md5Hex = bigInt.toString(16);

    while (md5Hex.length() < 32) {
        md5Hex = "0" + md5Hex;
    }

    return md5Hex;
}

From source file:helper.Digester.java

/**
 * @param str the input string/*from   w  w w. j  a  v  a2  s. c  om*/
 * @return the digested string
 * @throws NoSuchAlgorithmException encryption algorithm not installed 
 */
public static String digest(String str) throws NoSuchAlgorithmException {
    Configuration config = Play.application().configuration();

    String salt = config.getString("application.secret");

    String saltedStr = str + salt;

    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(saltedStr.getBytes());

    byte byteData[] = md.digest();

    byte[] base64 = Base64.encodeBase64(byteData);

    String result = new String(base64, Charset.defaultCharset());

    return result;
}

From source file:net.sf.jml.util.DigestUtils.java

public static byte[] sha1(byte[] b) {
    try {/*from ww  w . j  av  a 2 s .  c  om*/
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        return digest.digest(b);
    } catch (NoSuchAlgorithmException e) {
        log.error(e, e);
        return null;
    }
}

From source file:com.honnix.yaacs.util.MD5.java

/**
 * Get MD5 sum of the input data.//from  w  w  w.j av  a  2  s.  com
 * 
 * @param data
 *            input data used to generate MD5 sum
 * @return MD5 sum of the input data, or null if no MD5 algorithm could be
 *         found
 */
public static String getMD5Sum(String data) {
    String encodedData = null;

    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");

        messageDigest.update(data.getBytes("US-ASCII"), 0, data.length());
        encodedData = new BigInteger(1, messageDigest.digest()).toString(16);
    } catch (NoSuchAlgorithmException e) {
        StringBuilder sb = new StringBuilder("No MD5 algorithm.").append(" Could not apply validation check.");

        LOG.error(sb.toString(), e);
    } catch (UnsupportedEncodingException e) {
        LOG.error("This should not happen anyway.", e);
    }

    return encodedData;
}

From source file:components.Rajamobil.java

public String md5(String md5) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(md5.getBytes());// w  w w.j  a  va  2 s .  c om
    byte[] digest = md.digest();
    StringBuffer sb = new StringBuffer();
    for (byte b : digest) {
        sb.append(String.format("%02x", b & 0xff));
    }
    return sb.toString();
}