List of usage examples for java.security MessageDigest getInstance
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:net.sf.jml.util.DigestUtils.java
public static byte[] md5(byte[] b) { try {//from ww w . j a v a 2s . c o m MessageDigest digest = MessageDigest.getInstance("MD5"); return digest.digest(b); } catch (NoSuchAlgorithmException e) { log.error(e, e); return null; } }
From source file:com.zen.androidhtmleditor.util.TextUtil.java
public static String MD5(String str, String encoding) { MessageDigest messageDigest = null; try {//from www. jav a 2s .co m messageDigest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } messageDigest.reset(); try { messageDigest.update(str.getBytes(encoding)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } 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:com.sammyun.util.MD5.java
/** * @param str// ww w.j ava2 s .com * @return * @throws NoSuchAlgorithmException */ public static String crypt(String str) throws NoSuchAlgorithmException { if (str == null || str.length() == 0) { throw new IllegalArgumentException("String to encript cannot be null or zero length"); } StringBuffer hexString = new StringBuffer(); MessageDigest md = MessageDigest.getInstance("MD5"); md.update(str.getBytes()); byte[] hash = md.digest(); for (byte aHash : hash) { if ((OXFF & aHash) < OX10) { hexString.append("0").append(Integer.toHexString((OXFF & aHash))); } else { hexString.append(Integer.toHexString(OXFF & aHash)); } } return hexString.toString(); }
From source file:Main.java
private static String digest(final String value) { byte[] digested; try {//from w w w. j av a 2 s . c om digested = MessageDigest.getInstance(HASH_ALGORITHM).digest(value.getBytes(CHARSET)); } catch (NoSuchAlgorithmException e) { return null; } catch (UnsupportedEncodingException e) { return null; } String hashed = new BigInteger(1, digested).toString(16); int padding = HASH_LENGTH - hashed.length(); if (padding == 0) return hashed; char[] zeros = new char[padding]; Arrays.fill(zeros, '0'); return new StringBuilder(HASH_LENGTH).append(zeros).append(hashed).toString(); }
From source file:Main.java
/** Returns a 32 character string containing a hash of {@code s}. */ public static String hash(String s) { try {// w w w . jav a2 s. c o m MessageDigest messageDigest = MessageDigest.getInstance("MD5"); byte[] md5bytes = messageDigest.digest(s.getBytes("UTF-8")); return bytesToHexString(md5bytes); } catch (NoSuchAlgorithmException e) { throw new AssertionError(e); } catch (UnsupportedEncodingException e) { throw new AssertionError(e); } }
From source file:Main.java
/** * A double check about app signature that was passed by MainActivity as facetID. * @param facetId a string value composed by app hash. I.e. android:apk-key-hash:Lir5oIjf552K/XN4bTul0VS3GfM * @param context Application Context/*from w w w . j a va2s . co m*/ * @return true if the signature executed on runtime matches if signature sent by MainActivity */ private static boolean checkAppSignature(String facetId, Context context) { try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES); for (Signature sign : packageInfo.signatures) { byte[] sB = sign.toByteArray(); MessageDigest messageDigest = MessageDigest.getInstance("SHA1"); messageDigest.update(sign.toByteArray()); String currentSignature = Base64.encodeToString(messageDigest.digest(), Base64.DEFAULT); if (currentSignature.toLowerCase().contains(facetId.split(":")[2].toLowerCase())) { return true; } } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return false; }
From source file:com.algodefu.yeti.md5.MD5HashGenerator.java
public static String generateKeyByObject(Object object) { MessageDigest m = null;/*from www . j a va 2 s . c o m*/ try { m = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } m.reset(); //m.update(objectToByteArray(object)); m.update(SerializationUtils.serialize((Serializable) object)); byte[] digest = m.digest(); BigInteger bigInt = new BigInteger(1, digest); String hashtext = bigInt.toString(16); while (hashtext.length() < 32) { hashtext = "0" + hashtext; } return hashtext; }
From source file:Main.java
public static String getApkSignatureMD5String(Signature signature) { MessageDigest md;//from ww w. j ava 2 s .c o m try { md = MessageDigest.getInstance("MD5"); md.update(signature.toByteArray()); byte[] digest = md.digest(); return toHexString(digest); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; }
From source file:Main.java
public static byte[] getFileDigest(File file, String algorithm) { InputStream fis = null;//www . j a v a 2 s . c o m byte[] buffer = new byte[1024]; int numRead; MessageDigest md; try { fis = new FileInputStream(file); md = MessageDigest.getInstance(algorithm); while ((numRead = fis.read(buffer)) > 0) { md.update(buffer, 0, numRead); } fis.close(); return md.digest(); } catch (Exception e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { } } } return null; }
From source file:Main.java
public static String calcHash(File file, String algo) throws Exception { MessageDigest digester = MessageDigest.getInstance(algo); FileInputStream is = new FileInputStream(file); DigestInputStream dis;//from w w w . j a va 2 s.co m try { dis = new DigestInputStream(is, digester); for (byte[] buffer = new byte[1024 * 4]; dis.read(buffer) >= 0;) { // just read it } } finally { is.close(); } byte[] digest = digester.digest(); StringBuffer hash = new StringBuffer(digest.length * 2); for (int i = 0; i < digest.length; i++) { int b = digest[i] & 0xFF; if (b < 0x10) { hash.append('0'); } hash.append(Integer.toHexString(b)); } return hash.toString(); }