List of usage examples for java.security MessageDigest getInstance
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:Main.java
private static byte[] generateDigest(String algorithm, String... params) throws NoSuchAlgorithmException { StringBuilder sb = new StringBuilder(512); for (String s : params) { sb.append(s);//from w w w . j a v a2 s . c om } return MessageDigest.getInstance(algorithm).digest(sb.toString().getBytes()); }
From source file:Main.java
public static final String hash(final String s, final String algorithm) { try {//from www . j ava 2 s. c o m // Create MD5 or SHA-256 Hash MessageDigest digest = MessageDigest.getInstance(algorithm); digest.update(s.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuilder hexString = new StringBuilder(); for (byte aMessageDigest : messageDigest) { String h = Integer.toHexString(0xFF & aMessageDigest); while (h.length() < 2) h = "0" + h; hexString.append(h); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; }
From source file:Main.java
private static MessageDigest createSHA1Digest() { MessageDigest digest = null;/*from w w w. j a v a 2s. c o m*/ try { digest = MessageDigest.getInstance("SHA-1"); } catch (Exception ex) { ex.printStackTrace(); } return digest; }
From source file:Main.java
private static byte[] hashTemplate(final byte[] data, final String algorithm) { if (data == null || data.length <= 0) return null; try {//ww w .jav a 2s. c o m MessageDigest md = MessageDigest.getInstance(algorithm); md.update(data); return md.digest(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static String toSHA1(String s) { MessageDigest md = null;//from w ww . j a va 2s . c o m byte[] sha1hash = null; try { md = MessageDigest.getInstance("SHA-1"); sha1hash = new byte[40]; md.update(s.getBytes(UTF_8), 0, s.length()); } catch (Exception e) { return ERROR_SHA1; } sha1hash = md.digest(); Formatter formatter = new Formatter(); for (byte b : sha1hash) { formatter.format("%02x", b); } return formatter.toString(); }
From source file:Main.java
public static String getMd5Digest(String pInput) { if (pInput == null) return ""; try {/*from w w w.j a v a 2 s.com*/ MessageDigest lDigest = MessageDigest.getInstance("MD5"); lDigest.update(getBytes(pInput)); BigInteger lHashInt = new BigInteger(1, lDigest.digest()); return String.format("%1$032X", lHashInt); } catch (NoSuchAlgorithmException lException) { throw new RuntimeException(lException); } }
From source file:Main.java
static MessageDigest createDigestInstance(String digestName, String provider) throws NoSuchAlgorithmException, NoSuchProviderException { if (provider == null) { return MessageDigest.getInstance(digestName); }//from w ww. ja v a 2s .c o m return MessageDigest.getInstance(digestName, provider); }
From source file:Main.java
public static String getSHA1Digest(final String pInput) { if (pInput != null) { try {//from w w w. ja va2s. c o m final MessageDigest lDigest = MessageDigest.getInstance("SHA1"); lDigest.update(getBytes(pInput)); final BigInteger lHashInt = new BigInteger(1, lDigest.digest()); return String.format("%1$032X", lHashInt); } catch (final NoSuchAlgorithmException lException) { return pInput; } } return null; }
From source file:Main.java
public static String getMd5Digest(final String pInput) { if (pInput != null) { try {/*w w w . ja v a 2s.c o m*/ final MessageDigest lDigest = MessageDigest.getInstance("MD5"); lDigest.update(getBytes(pInput)); final BigInteger lHashInt = new BigInteger(1, lDigest.digest()); return String.format("%1$032X", lHashInt); } catch (final NoSuchAlgorithmException lException) { return pInput; } } return null; }
From source file:Main.java
public static String getHash(String stringToHash) { MessageDigest digest = null;/*from w ww .j a v a 2 s . com*/ try { digest = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } byte[] result = null; try { result = digest.digest(stringToHash.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } StringBuilder sb = new StringBuilder(); for (byte b : result) { sb.append(String.format("%02X", b)); } String messageDigest = sb.toString(); return messageDigest; }