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.arrow.acn.client.utils.MD5Util.java

public static byte[] calcMD5Checksum(Path path) throws NoSuchAlgorithmException, IOException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    try (SeekableByteChannel sbc = Files.newByteChannel(path)) {
        ByteBuffer buf = ByteBuffer.allocateDirect(BUFFER_SIZE);
        while (sbc.read(buf) > 0) {
            buf.flip();/*from w w  w.  ja  va2s  . c  o  m*/
            md.update(buf);
            buf.clear();
        }
    }
    return md.digest();
}

From source file:Main.java

private static byte[] MD5EncodeFileByte(File filename) {
    if (filename != null) {
        int i;/*  w  w w  .j  a va2 s .c o m*/
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
        byte[] data = new byte[4096];
        FileInputStream fis;
        try {
            fis = new FileInputStream(filename);
        } catch (FileNotFoundException e2) {
            e2.printStackTrace();
            return null;
        }
        while (true) {
            try {
                i = fis.read(data);
                if (i != -1) {
                    md.update(data, 0, i);
                } else {
                    fis.close();
                    return md.digest();
                }
            } catch (IOException e) {
                e.printStackTrace();
                try {
                    fis.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                return null;
            }
        }
    }
    return null;
}

From source file:Main.java

public static String makeMD5Hash(byte[] bytes) {
    try {/* ww w  .  j  a  v  a2 s  . co  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(bytes, 0, bytes.length);
        byte[] sha1hash = md.digest();
        return convertToHex(sha1hash);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String certHash(byte[] encoded, String digest) {
    try {/*from w  w w  .ja v a2 s .  c o m*/
        MessageDigest md = MessageDigest.getInstance(digest);
        md.update(encoded);
        return hexString(md.digest());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static String makeSHA1Hash(byte[] bytes) {
    try {/*w ww.  jav a2 s .c  o m*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(bytes, 0, bytes.length);
        byte[] sha1hash = md.digest();
        return convertToHex(sha1hash);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.juicioenlinea.application.secutiry.Security.java

public static String encriptar(String texto) {

    final String secretKey = "hunter"; //llave para encriptar datos
    String encryptedString = null;

    try {//  w w w. ja va 2  s.c  om

        MessageDigest md = MessageDigest.getInstance("SHA");
        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 = texto.getBytes("UTF-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        encryptedString = new String(base64Bytes);

    } catch (NoSuchAlgorithmException | UnsupportedEncodingException | NoSuchPaddingException
            | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
        Logger.getLogger(Security.class.getName()).log(Level.SEVERE, null, ex);
    }
    return encryptedString;
}

From source file:Main.java

public static String hashBytes(byte[] input, String algo) {
    try {//  ww  w  . jav a  2 s .  c  o m
        MessageDigest md = MessageDigest.getInstance(algo);
        byte[] hashBytes = md.digest(input);
        String hash = toHexString(hashBytes);

        md.reset();
        return hash;
    } catch (NoSuchAlgorithmException e) {
        Log.e("FDroid", "Device does not support " + algo + " MessageDisgest algorithm");
        return null;
    }
}

From source file:Main.java

public static String hashKey(String key) {
    String cacheKey;//from w  w  w. j a  v a2s .c  o m
    try {
        final MessageDigest mDigest = MessageDigest.getInstance("MD5");
        mDigest.update(key.getBytes());
        cacheKey = bytesToHexString(mDigest.digest());
    } catch (NoSuchAlgorithmException e) {
        cacheKey = String.valueOf(key.hashCode());
    }
    return cacheKey;
}

From source file:Main.java

public static String md5Encoder(String content, boolean isCapital) {
    try {//w  ww . j  a  v a 2  s  . c  o  m
        byte[] strTemp = content.getBytes();
        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
        mdTemp.update(strTemp);
        byte[] mds = mdTemp.digest();
        int j = mds.length;
        char str[] = new char[j * 2];
        int k = 0;
        for (byte md : mds) {
            if (isCapital) {
                str[k++] = CAPITAL_HEX_DIGITS[md >> 4 & 0xf];
                str[k++] = CAPITAL_HEX_DIGITS[md & 0xf];
            } else {
                str[k++] = LOWER_HEX_DIGITS[md >> 4 & 0xf];
                str[k++] = LOWER_HEX_DIGITS[md & 0xf];
            }
        }
        return new String(str);
    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

/**
 * @param data the data to hash/*from w  w w .j  ava  2 s  .  c  o  m*/
 * @param byteDelimiter an optional delimiter between bytes.
 * @return a sha1 digest of the data, as a string with bytes delimited by byteDelimiter
 */
private static String getHexHash(final byte[] data, final Character byteDelimiter) {
    try {
        final byte[] hash = MessageDigest.getInstance("SHA1").digest(data);

        return convertBytesToHex(hash, byteDelimiter);
    } catch (final NoSuchAlgorithmException e) {
        // this should never occur
        throw new RuntimeException(e);
    }
}