马上放假了,提前祝大家国庆快乐,也不知道为什么最近喜欢写博客了,看到点击量一点点的上来还是感觉挺开心的,就是不知道大家喜欢什么类型一点的文章。

OK,先分享个MD5的工具类吧!这个做开发的应该都不陌生,就是用来解密加密的。

public class MD5Utils {

	private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

	public static String toHexString(byte[] b) {
		StringBuilder sb = new StringBuilder(b.length * 2);
		for (int i = 0; i < b.length; i++) {
			sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);
			sb.append(HEX_DIGITS[b[i] & 0x0f]);
		}
		return sb.toString();
	}

	public static String md5Str(String string) {
		MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
			byte[] bytes = string.getBytes();
			String md5Str = toHexString(md5.digest(bytes));
			return TextUtils.isEmpty(md5Str) ? "" : md5Str;
		} catch (Exception e) {
			System.out.println("error");
			return "";
		}
	}

	public static String md5Sum(String filename) {
		InputStream fis;
		byte[] buffer = new byte[1024];
		int numRead = 0;
		MessageDigest md5;
		try {
			fis = new FileInputStream(filename);
			md5 = MessageDigest.getInstance("MD5");
			while ((numRead = fis.read(buffer)) > 0) {
				md5.update(buffer, 0, numRead);
			}
			fis.close();
			String md5Str = toHexString(md5.digest());
			return TextUtils.isEmpty(md5Str) ? "" : md5Str;
		} catch (Exception e) {
			System.out.println("error");
			return "";
		}
	}
}

END 

没啥技术含量,全当打发时间用

Android中MD5的工具类---给祖国庆生

编辑:吴明辉

相关文章:

  • 2022-02-26
  • 2022-01-07
  • 2022-12-23
  • 2021-04-17
  • 2021-06-16
猜你喜欢
  • 2021-05-27
  • 2022-12-23
  • 2022-01-19
  • 2021-04-05
  • 2021-10-03
  • 2021-12-02
  • 2021-12-02
相关资源
相似解决方案