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 generateShortUuid() { UUID uuid = UUID.randomUUID(); MessageDigest md = null;//w ww. ja v a 2s .co m try { md = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return uuid.toString(); } md.update(uuid.toString().getBytes()); byte[] digest = md.digest(); return Base64.encodeToString(digest, Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING).substring(0, 20); }
From source file:Main.java
public static String getMD5Value(String value) { byte[] hash;/*from w ww . jav a2s .co m*/ try { hash = MessageDigest.getInstance(MD5).digest(value.getBytes(UTF8)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Huh, MD5 should be supported?", e); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Huh, UTF-8 should be supported?", e); } StringBuilder hex = new StringBuilder(hash.length * 2); for (byte b : hash) { if ((b & 0xFF) < 0x10) hex.append("0"); hex.append(Integer.toHexString(b & 0xFF)); } return hex.toString(); }
From source file:Main.java
public static String hexSHA1(String value) { try {/* w w w . j av a 2s.co m*/ MessageDigest md = MessageDigest.getInstance("SHA-1"); md.update(value.getBytes("utf-8")); byte[] digest = md.digest(); return byteToHexString(digest); } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:Main.java
public static String md5One(String s) { MessageDigest md = null;/* ww w . j av a 2 s.c o m*/ try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage()); } md.update(s.getBytes()); return byteArrayToHexString(md.digest()); }
From source file:Main.java
private static String hashAlgorithm(String hash, String text) throws NoSuchAlgorithmException, UnsupportedEncodingException { MessageDigest md = MessageDigest.getInstance(hash); md.update(text.getBytes("iso-8859-1"), 0, text.length()); return convertToHex(md.digest()); }
From source file:Main.java
public static String sha1(String str) { if (str == null || str.length() == 0) { return null; }/*from w w w . ja v a 2 s . co m*/ char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; try { MessageDigest mdTemp = MessageDigest.getInstance("SHA1"); mdTemp.update(str.getBytes()); byte[] md = mdTemp.digest(); int j = md.length; char buf[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; buf[k++] = hexDigits[byte0 >>> 4 & 0xf]; buf[k++] = hexDigits[byte0 & 0xf]; } return new String(buf); } catch (Exception e) { return null; } }
From source file:Main.java
/** * Converts a java.lang.String in to a MD5 hashed String * @param source the source String//from www. j a v a 2s . c o m * @return the MD5 hashed version of the string */ public static String md5(String source) { try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] bytes = md.digest(source.getBytes()); return getString(bytes); } catch (Exception e) { return null; } }
From source file:Main.java
public static String sha1Hash(String text) { String hash = null;// ww w. j a v a 2s .c o m try { final MessageDigest digest = MessageDigest.getInstance("SHA-1"); final byte[] bytes = text.getBytes("UTF-8"); digest.update(bytes, 0, bytes.length); hash = convertToHex(digest.digest()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return hash; }
From source file:Main.java
/** * Converts a java.lang.String in to a SHA hashed String * @param source the source String//from ww w . ja v a 2 s. co m * @return the MD5 hashed version of the string */ public static String sha(String source) { try { MessageDigest md = MessageDigest.getInstance("SHA"); byte[] bytes = md.digest(source.getBytes()); return getString(bytes); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static byte[] calculateMd5(byte[] binaryData) { MessageDigest messageDigest = null; try {/*ww w . j a v a 2 s . co m*/ messageDigest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("MD5 algorithm not found."); } messageDigest.update(binaryData); return messageDigest.digest(); }