MD5是一种加密、压缩算法。在爬虫项目中奖URL压缩为128位的散列值。在Java 中,java.security.MessageDigest 中已经定义了MD5 的计算,只需要简单地调用即可得到MD5 的128 位整数。然后将此128 位(16 个字节)转换成十六进制表示即可。
java代码实现:
import java.math.BigInteger; import java.security.MessageDigest; public class MD5 { public static String getMD5(String source) { // TODO Auto-generated method stub String result=null; try{ //获取摘要 MessageDigest md=MessageDigest.getInstance("MD5"); //更新摘要,参数为String转换成的byte数组 md.update(source.getBytes()); //用digest函数处理数据并返回密文结果,密文结果也是byte数组。 //用BigInteger转为16进制字符串,参数1表示正数,toString的参数表示转换后的进制 result= new BigInteger(1,md.digest()).toString(16); }catch(Exception e){ e.printStackTrace(); } return result; } }参考文献: 自己动手写网络爬虫 java MD5工具类
