知识点整理之Java获取MD5或者SHA
2015-04-17 09:02
260 查看
获取MD5或者是SHA是经常需要用到的功能.
/** * MD5 */ public String getMd5(String msg) throws NoSuchAlgorithmException { return this.digest(msg, "MD5"); } /** * SHA */ public String getSha(String msg) throws NoSuchAlgorithmException { return this.digest(msg, "SHA-1"); } /** * 具体的生成MD5或SHA的过程 */ private String digest(String msg, String type) throws NoSuchAlgorithmException { String result = null; MessageDigest alg = MessageDigest.getInstance(type); alg.update(msg.getBytes()); byte[] resultBytes = alg.digest(); result = this.byte2hex(resultBytes); return result; } /** * 转16进制 */ private String byte2hex(byte[] bytes) { StringBuilder resultStr = new StringBuilder(""); for (byte b : bytes) { String onebyte = Integer.toHexString(b & 0xFF); if (onebyte.length() == 1) resultStr.append("0").append(onebyte); else resultStr.append(onebyte); } return resultStr.toString(); }
相关文章推荐
- 【JAVA】常用加解密算法总结及JAVA实现【BASE64,MD5,SHA,DES,3DES,AES,RSA】
- Java 获取文件的md5
- java或者android通过http获取服务端数据
- 一个小的知识点关于java获取系统时间的问题
- java汉字转拼音。获取首个字或者词
- Java Web知识点整理:第一章:Html入门、CSS介绍、Java和JavaScript区别
- Java Web知识点整理:第二章:Jquery学习、xml学习、XML的解析、Servlet学习
- java中消息摘要MD5与SHA-1
- Java知识点整理:第六章:访问控制修饰符、引用类型的转换、Java Bean规范
- Java生成摘要(MD5 SHA-1)
- java中的垃圾搜集知识点整理
- Java知识点全集合(Cage整理版)(最新更新080517)
- 获取Google Maps API 指纹证书时获取的是SHA1型的,而认证时需要的是MD5的解决方法【转】
- Java正确获取客户端真实IP方法整理
- java-信息安全(一)-BASE64,MD5,SHA,HMAC,RIPEMD算法
- java基础知识点、面试选择题归纳整理
- java中怎么在获取的系统时间上提前一周一月一年或推迟一周,一月或者一年
- Java加密技术之—BASE64,MD5,SHA,HMAC
- 知识点整理之Java时间格式化
- java写的日期工具,根据设置日期获取指定的一周或者一个月的所有日期