List of usage examples for java.security MessageDigest getInstance
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:Main.java
private static MessageDigest getMd5Instance() { try {//from w w w . j a va 2s.c om return MessageDigest.getInstance("md5"); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.beginner.core.utils.MD5.java
public static String md5(String str) { try {// ww w . j av a2 s. c o m MessageDigest md = MessageDigest.getInstance("MD5"); md.update(str.getBytes()); byte b[] = md.digest(); int i; StringBuffer buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) i += 256; if (i < 16) buf.append("0"); buf.append(Integer.toHexString(i)); } str = buf.toString(); } catch (Exception e) { e.printStackTrace(); } return str; }
From source file:Main.java
public static String hexdigest(byte[] paramArrayOfByte) { final char[] hexDigits = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102 }; try {//from w w w. j a va2 s . co m MessageDigest localMessageDigest = MessageDigest.getInstance("MD5"); localMessageDigest.update(paramArrayOfByte); byte[] arrayOfByte = localMessageDigest.digest(); char[] arrayOfChar = new char[32]; for (int i = 0, j = 0;; i++, j++) { if (i >= 16) { return new String(arrayOfChar); } int k = arrayOfByte[i]; arrayOfChar[j] = hexDigits[(0xF & k >>> 4)]; arrayOfChar[++j] = hexDigits[(k & 0xF)]; } } catch (Exception e) { e.printStackTrace(); } return ""; }
From source file:Main.java
/** * @param context// www .j a v a 2s.c o m * @return KeyHash * follow facebook developers link to get release key hash * https://developers.facebook.com/docs/android/getting-started#release-key-hash */ public static String getKeyHash(Context context) { PackageInfo packageInfo; String key = null; try { packageInfo = context.getPackageManager().getPackageInfo( context.getApplicationContext().getPackageName(), PackageManager.GET_SIGNATURES); for (Signature signature : packageInfo.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT)); } } catch (PackageManager.NameNotFoundException e1) { } catch (NoSuchAlgorithmException e) { } catch (Exception e) { } return key; }
From source file:Main.java
public static String sha1(String strSrc) { MessageDigest md = null;/*from w ww .j a v a 2s . c o m*/ String strDes = ""; byte[] bt = strSrc.getBytes(); try { md = MessageDigest.getInstance("SHA-1"); md.update(bt); byte[] encryptStr = md.digest(); String tmp = null; for (int i = 0; i < encryptStr.length; i++) { tmp = (Integer.toHexString(encryptStr[i] & 0xFF)); if (tmp.length() == 1) { strDes += "0"; } strDes += tmp; } } catch (NoSuchAlgorithmException e) { System.out.println("Invalid algorithm."); return null; } return strDes; }
From source file:MainClass.java
static Cipher createCipher(int mode) throws Exception { PBEKeySpec keySpec = new PBEKeySpec("test".toCharArray()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(keySpec); MessageDigest md = MessageDigest.getInstance("MD5"); md.update("input".getBytes()); byte[] digest = md.digest(); byte[] salt = new byte[8]; for (int i = 0; i < 8; ++i) salt[i] = digest[i];/*w w w. jav a 2s . co m*/ PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 20); Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); cipher.init(mode, key, paramSpec); return cipher; }
From source file:com.barchart.netty.rest.server.util.SigningUtil.java
public static byte[] bytesToSign(final HttpServerRequest request) throws Exception { final ByteBuf content = request.getContent(); content.markReaderIndex();//from ww w . jav a2 s .c o m final byte[] contentBytes = IOUtils.toByteArray(new ByteBufInputStream(content)); content.resetReaderIndex(); final String md5 = KerberosUtilities.bytesToHex(MessageDigest.getInstance("MD5").digest(contentBytes)); final StringBuilder sb = new StringBuilder(); sb.append(request.getMethod().name()).append("\n").append(request.getUri()).append("\n").append(md5) .append("\n").append(nullCheck(request.headers().get(HttpHeaders.Names.CONTENT_TYPE))).append("\n") .append(nullCheck(request.headers().get(HttpHeaders.Names.DATE))).append("\n"); return sb.toString().getBytes("UTF-8"); }
From source file:Main.java
public static String getMD5Hash(String s) throws NoSuchAlgorithmException { MessageDigest m = MessageDigest.getInstance("MD5"); byte[] data = s.getBytes(); m.update(data, 0, data.length);// w ww . jav a 2 s .co m BigInteger i = new BigInteger(1, m.digest()); return String.format("%1$032X", i); }
From source file:MD5.java
public static String calculateMD5(File updateFile) { MessageDigest digest;/*w w w .ja v a 2 s .co m*/ try { digest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { return null; } InputStream is; try { is = new FileInputStream(updateFile); } catch (FileNotFoundException e) { return null; } byte[] buffer = new byte[8192]; int read; try { while ((read = is.read(buffer)) > 0) { digest.update(buffer, 0, read); } byte[] md5sum = digest.digest(); BigInteger bigInt = new BigInteger(1, md5sum); String output = bigInt.toString(16); // Fill to 32 chars output = String.format("%32s", output).replace(' ', '0'); return output; } catch (IOException e) { throw new RuntimeException("Unable to process file for MD5", e); } finally { try { is.close(); } catch (IOException e) { System.out.println(e); } } }
From source file:Main.java
public static String stringToMD5(String str) { try {/* w w w. j a va2 s.c o m*/ byte[] strTemp = str.getBytes(); MessageDigest mdTemp = MessageDigest.getInstance("MD5"); mdTemp.update(strTemp); return toHexString(mdTemp.digest()); } catch (Exception e) { return null; } }