List of usage examples for java.security MessageDigest digest
public byte[] digest()
From source file:Main.java
public static final String encryptMd5String(String s) { try {/* w w w.ja va2 s . c o m*/ byte[] btInput = s.getBytes(); MessageDigest mdInst = MessageDigest.getInstance("MD5"); mdInst.update(btInput); byte[] md = mdInst.digest(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < md.length; i++) { int val = ((int) md[i]) & 0xff; if (val < 16) { sb.append("0"); } sb.append(Integer.toHexString(val)); } return sb.toString(); } catch (Exception e) { return null; } }
From source file:Main.java
public static String cryptoStr2SHA1(String str) { try {//from w w w . ja va 2 s. c o m MessageDigest md = MessageDigest.getInstance("SHA-1"); md.update(str.getBytes()); byte[] bytes = md.digest(); return byte2Hex(bytes); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } }
From source file:Main.java
public final static String Md5(String s) { char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; try {/*ww w . j ava2 s . com*/ byte[] strTemp = s.getBytes("UTF-8"); MessageDigest mdTemp = MessageDigest.getInstance("MD5"); mdTemp.update(strTemp); byte[] md = mdTemp.digest(); int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } catch (Exception e) { return null; } }
From source file:Main.java
/** * Generate the SHA1 hash of a message //w w w. j a va2s . c o m * @param message * @return * @throws NoSuchAlgorithmException * @throws UnsupportedEncodingException */ public static byte[] SHA1(byte[] message) throws NoSuchAlgorithmException, UnsupportedEncodingException { MessageDigest md; md = MessageDigest.getInstance("SHA-1"); md.update(message); return md.digest(); }
From source file:Main.java
public static String md5(String str) { try {//w ww. java 2s . co m MessageDigest md = MessageDigest.getInstance("MD5"); md.update(str.getBytes()); byte[] b = md.digest(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < b.length; i++) { int v = (int) b[i]; v = v < 0 ? 0x100 + v : v; String cc = Integer.toHexString(v); if (cc.length() == 1) sb.append('0'); sb.append(cc); } return sb.toString(); } catch (Exception e) { } return ""; }
From source file:Main.java
public static byte[] encryptMD5File(File file) { FileInputStream in = null;//from w ww . ja va 2 s . c o m try { in = new FileInputStream(file); FileChannel channel = in.getChannel(); MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); MessageDigest md = MessageDigest.getInstance("MD5"); md.update(buffer); return md.digest(); } catch (NoSuchAlgorithmException | IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException ignored) { } } } return null; }
From source file:Main.java
public static String MD5(String s) { try {// w ww .jav a2 s. c o m MessageDigest md = MessageDigest.getInstance("MD5"); md.update(s.getBytes()); byte b[] = md.digest(); int i; StringBuilder buf = new StringBuilder(""); 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)); } return buf.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return "null"; }
From source file:Main.java
public static String md5String(String val) { if (val == null) return null; byte[] resBytes; try {/* www. j a va2s .c o m*/ MessageDigest md = MessageDigest.getInstance("MD5"); md.update(val.getBytes("UTF-8")); resBytes = md.digest(); return hexString(resBytes); } catch (Exception e) { return null; } }
From source file:Main.java
private static StringBuffer getMd5Buffer(String text) { StringBuffer buf = new StringBuffer(); try {// w w w. j av a 2 s .co m MessageDigest md = MessageDigest.getInstance("MD5"); md.update(text.getBytes()); byte[] b = md.digest(); int i; 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)); } } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return buf; }
From source file:MD5.java
/** * MD5 BASE64 checksum for the specified input string. * //w ww .j a v a2 s . c o m * @param input - * Specified input string * @return String - MD5 BASE64 sum */ public static String checkMD5(String input) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(input.getBytes()); byte[] enc = md.digest(); String md5Sum = new sun.misc.BASE64Encoder().encode(enc); return md5Sum; } catch (NoSuchAlgorithmException nsae) { System.out.println(nsae.getMessage()); return null; } }