List of usage examples for java.security MessageDigest getInstance
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:Main.java
public static String generateMarvelHash(String publicKey, String privateKey) { String marvelHash = ""; try {//from w w w . jav a2 s . c o m String timeStamp = getUnixTimeStamp(); String marvelData = timeStamp + privateKey + publicKey; MessageDigest messageDigest = MessageDigest.getInstance("MD5"); byte[] hash = messageDigest.digest(marvelData.getBytes()); StringBuilder stringBuilder = new StringBuilder(2 * hash.length); for (byte b : hash) { stringBuilder.append(String.format("%02x", b & 0xff)); } marvelHash = stringBuilder.toString(); } catch (NoSuchAlgorithmException e) { System.out.println("[DEBUG]" + " MarvelApiUtils generateMarvelHash - " + "NoSuchAlgorithmException"); } return marvelHash; }
From source file:Main.java
public static String hashStream(InputStream in, String hash) throws IOException { MessageDigest md5 = null;/*from ww w .j av a2 s . co m*/ try { md5 = MessageDigest.getInstance(hash); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } int bsize = 4096; byte[] buffer = new byte[bsize]; int length; while ((length = in.read(buffer, 0, bsize)) > 0) { md5.update(buffer, 0, length); } return toHex(md5.digest()); }
From source file:Main.java
public static String generateMarvelHash(String publicKey, String privateKey) { String marvelHash = ""; try {/* w w w. j a v a 2s. c o m*/ String timeStamp = getUnixTimeStamp(); String marvelData = timeStamp + privateKey + publicKey; MessageDigest messageDigest = MessageDigest.getInstance("MD5"); byte[] hash = messageDigest.digest(marvelData.getBytes()); StringBuilder stringBuilder = new StringBuilder(2 * hash.length); for (byte b : hash) stringBuilder.append(String.format("%02x", b & 0xff)); marvelHash = stringBuilder.toString(); } catch (NoSuchAlgorithmException e) { System.out.println("[DEBUG]" + " MarvelApiUtils generateMarvelHash - " + "NoSuchAlgorithmException"); } return marvelHash; }
From source file:Main.java
public static String getMD5(String file) { String md5 = ""; try {/* w w w.ja v a2 s. c om*/ MessageDigest md = MessageDigest.getInstance("MD5"); InputStream is; is = new FileInputStream(file); DigestInputStream dis = new DigestInputStream(is, md); byte data[] = new byte[1024]; @SuppressWarnings("unused") int count; while ((count = dis.read(data)) != -1) { } byte[] digest = md.digest(); for (int i = 0; i < digest.length; i++) { md5 += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1); } return md5; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return md5; }
From source file:Main.java
/** * Returns a String Object which contains the SHA256 Hash value of the input * * @param input a String object for which SHA256 should be calculated * @return SHA256 of the input String/*from ww w . j a va 2 s .c o m*/ */ public static String getSHA256(String input) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(input.getBytes("UTF-8")); StringBuilder hexString = new StringBuilder(); for (int i = 0; i < hash.length; i++) { String hex = Integer.toHexString(0xff & hash[i]); if (hex.length() == 1) hexString.append('0'); hexString.append(hex); } return hexString.toString(); } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:Main.java
public static String md5Summary(String str) { if (str == null) { return null; }// w w w .j a v a 2s .c o m MessageDigest messageDigest = null; try { messageDigest = MessageDigest.getInstance("MD5"); messageDigest.reset(); messageDigest.update(str.getBytes("utf-8")); } catch (NoSuchAlgorithmException e) { return str; } catch (UnsupportedEncodingException e) { return str; } byte[] byteArray = messageDigest.digest(); StringBuffer md5StrBuff = new StringBuffer(); for (int i = 0; i < byteArray.length; i++) { if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i])); else md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i])); } return md5StrBuff.toString(); }
From source file:Main.java
/** * generates MD5 of input string//from w w w .j av a 2 s . c o m * * @param input string to be hashed * @return MD5 hash of string */ public static String getMD5(String input) { MessageDigest m = null; try { m = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } m.reset(); m.update(input.getBytes()); byte[] digest = m.digest(); BigInteger bigInt = new BigInteger(1, digest); String hashtext = bigInt.toString(16); //zero pad to get the full 32 chars. while (hashtext.length() < 32) { hashtext = "0" + hashtext; } return hashtext; }
From source file:Main.java
public static String checkSum(String path) { String checksum = null;//from w ww.j ava2 s .c o m try { FileInputStream fis = new FileInputStream(path); MessageDigest md = MessageDigest.getInstance("MD5"); //Using MessageDigest update() method to provide input byte[] buffer = new byte[8192]; int numOfBytesRead; while ((numOfBytesRead = fis.read(buffer)) > 0) { md.update(buffer, 0, numOfBytesRead); } byte[] hash = md.digest(); checksum = new BigInteger(1, hash).toString(16); //don't use this, truncates leading zero } catch (IOException | NoSuchAlgorithmException ignored) { } assert checksum != null; return checksum.trim(); }
From source file:Main.java
public static String getFileMD5(File file) { if (!file.isFile()) { return null; }//from w w w. j a v a2s . c o m MessageDigest digest = null; FileInputStream in = null; byte buffer[] = new byte[1024]; int len; try { digest = MessageDigest.getInstance("MD5"); in = new FileInputStream(file); while ((len = in.read(buffer, 0, 1024)) != -1) { digest.update(buffer, 0, len); } in.close(); } catch (Exception e) { e.printStackTrace(); return null; } BigInteger bigInt = new BigInteger(1, digest.digest()); return bigInt.toString(16); }
From source file:Main.java
public static String getMd5Hash(String buffer) { MessageDigest md;/* w w w . j ava 2 s.c o m*/ try { md = MessageDigest.getInstance("MD5"); return bytesToHex(md.digest(buffer.getBytes("UTF-8"))); } catch (NoSuchAlgorithmException | UnsupportedEncodingException ignore) { } return ""; }