1.8.0和1.8.1的乌龙

来源:互联网 发布:淘宝云客服返岗论坛 编辑:程序博客网 时间:2024/04/29 15:43
下Subversion的时候看见他家主页上的SHA1验证心血来潮,当是复习Java自己写一个SHA1的验证程序。调用的当然是非常简单易用的MessageDigest。
public class MessageDigestCheckSumValidator extends CheckSumValidator {private String algorithm="";private DigestInputStream digestInputStream =null;private MessageDigest messageDigest = null;protected DigestInputStream getDigestInputStream() {return digestInputStream;}protected void setDigestInputStream(DigestInputStream dis) {this.digestInputStream = dis;}protected MessageDigest getMessageDigest() {return messageDigest;}protected void setMessagedigest(MessageDigest md) {this.messageDigest = md;}protected String getAlgorithm() {return algorithm;}protected void setAlgorithm(String algorithm) {this.algorithm = algorithm;}public MessageDigestCheckSumValidator (String targetCheckSum, InputStream inputStream, String algorithm){this.setTargetCheckSum(targetCheckSum);this.setInputStream(inputStream);this.setAlgorithm(algorithm);}protected String byteArrtoHexaDecimal(byte[] bytes){StringBuffer sb = new StringBuffer();for (int i=0; i<bytes.length; i++){sb.append(Integer.toString((bytes[i]&0xff)+0x100,16).substring(1));}return sb.toString();}@Overrideprotected boolean validateCheckSum() {BufferedInputStream bis =null;try{this.setMessagedigest(MessageDigest.getInstance(this.getAlgorithm()));bis = new BufferedInputStream(this.getInputStream());this.setDigestInputStream(new DigestInputStream(bis,this.getMessageDigest()));while(this.digestInputStream.read()!=-1);byte[] bytes = this.getMessageDigest().digest();String hexaDemicalCheckSumStr = this.byteArrtoHexaDecimal(bytes);if (this.getTargetCheckSum().equals(hexaDemicalCheckSumStr)){return true;}else{return false;}}catch(NoSuchAlgorithmException e){e.printStackTrace();return false;}catch(IOException e){e.printStackTrace();return false;}finally{if(bis!=null){try{bis.close();}catch(IOException e){e.printStackTrace();}}}}}

结果用Junit跑了个测试竟然木有通过。。。人家网站上的校验码是12261a97df5cdc53175cba813ea451937a226bca,可跑出来是6f2b4476b8d8b9f2700ae101252bdf6e67366302。换了两三种ByteArray2HexaDecimal的方法,结果还是一样。

百思不得骑姐的情况下,从网上搜索了两个程序进行对照,结果竟然也同样是6f2b4476b8d8b9f2700ae101252bdf6e67366302。

看来要出动google大神了,突然就想到用这个“错误”的校验码看看能搜到什么东西。


竟然有一个版本的校验法就是6f2b4476b8d8b9f2700ae101252bdf6e67366302,再凝神一看svn的版本号,再对一下svn官方网站上的版本号,乌龙就这么诞生了。。。一直以为下的是1.8.1结果神使鬼差的一直在对1.8.0做校验。重新下载,再跑,通过了。


原创粉丝点击