📄 digesttool.java
字号:
public final PeerBinaryID createPeerID(net.jxta.peergroup.PeerGroupID peerGroupID,String clearTextID, String function){ byte[] digest = generateHash(clearTextID, function); PeerBinaryID peerID = new PeerBinaryID(peerGroupID,digest,false); return peerID; } /** * Creates a new instance of DigestPipe. Because this is a utility, * this is private to prevent construction. */ /** * Generates a Base64 encoded string of an SHA-1 digest hash of the string: clearTextID.<p> * * @param clearTextID A string that is to be hashed. This can be any string used for hashing or hiding data. * * @return Base64 encoded string containing the hash of the string: clearTextID. */ public final String generateHashString(String clearTextID){ try { java.io.StringWriter base64 = new java.io.StringWriter(); net.jxta.impl.util.BASE64OutputStream encode = new net.jxta.impl.util.BASE64OutputStream( base64 ); encode.write( generateHash(clearTextID) ); encode.close(); return base64.toString(); } catch( Exception failed ) { LOG.error("Unable to encode hash value.", failed); throw new RuntimeException("Unable to encode hash value."); } } /** * Generates a Base64 encoded string of an SHA-1 digest hash of the string: clearTextID+"-"+function or: clearTextID if function was blank.<p> * * @param clearTextID A string that is to be hashed. This can be any string used for hashing or hiding data. * @param function A function related to the clearTextID string. This is used to create a hash associated with clearTextID so that it is a uique code. * * @return Base64 encoded string containing the hash of the string: clearTextID+"-"+function or clearTextID if function was blank. */ public final String generateHashString(String clearTextID, String function){ try { java.io.StringWriter base64 = new java.io.StringWriter(); net.jxta.impl.util.BASE64OutputStream encode = new net.jxta.impl.util.BASE64OutputStream( base64 ); encode.write( generateHash(clearTextID, function) ); encode.close(); return base64.toString(); } catch( Exception failed ) { LOG.error("Unable to encode hash value.", failed); throw new RuntimeException("Unable to encode hash value."); } } /** * Generates a SHA-1 digest hash of the string: clearTextID.<p> * * @param clearTextID A string that is to be hashed. This can be any string used for hashing or hiding data. * * @return String containing the hash of the string: clearTextID. */ public final byte[] generateHash(String clearTextID) { return generateHash(clearTextID, null); } /** * Generates an SHA-1 digest hash of the string: clearTextID+"-"+function or: clearTextID if function was blank.<p> * * Note that the SHA-1 used only creates a 20 byte hash.<p> * * @param clearTextID A string that is to be hashed. This can be any string used for hashing or hiding data. * @param function A function related to the clearTextID string. This is used to create a hash associated with clearTextID so that it is a uique code. * * @return array of bytes containing the hash of the string: clearTextID+"-"+function or clearTextID if function was blank. Can return null if SHA-1 does not exist on platform. */ public final byte[] generateHash(String clearTextID, String function) { String id; if (function == null) { id = clearTextID; } else { id = clearTextID + functionSeperator + function; } byte[] buffer = id.getBytes(); MessageDigest algorithm = null; try { algorithm = MessageDigest.getInstance(algorithmType); } catch (Exception e) { LOG.error("Cannot load selected Digest Hash implementation",e); return null; } // Generate the digest. algorithm.reset(); algorithm.update(buffer); try{ byte[] digest1 = algorithm.digest(); return digest1; }catch(Exception de){ LOG.error("Failed to creat a digest.",de); return null; } } /** * Generates an SHA-1 digest hash of the string: clearTextID.<p> * * @param clearTextID A string that is to be hashed. This can be any string used for hashing or hiding data. * * @return String containing the hash of the string: clearTextID. */ public final boolean test( String clearTextID, String function, String testHash) { String id = clearTextID + functionSeperator + function; return test(id,testHash); } /** Compares a clear text code or ID with a candidate hash code. * This is used to confirm that the clearTextID can be successfully converted to the hash. * @param clearTextID A string that is to be hashed. This can be any string used for hashing or hiding data. * @param testHash A string of hashed string. * @return true if the hash created from clearTextID is equal to the testHash string.Can return false if SHA-1 does not exist on platform. */ public final boolean test(String clearTextID, String testHash) { byte[] digest1 = generateHash(clearTextID); byte[] digest2; try{ java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream(); net.jxta.impl.util.BASE64InputStream decoder = new net.jxta.impl.util.BASE64InputStream( new java.io.StringReader(testHash) ); while( true ) { int c = decoder.read(); if( -1 == c ) { break; } bos.write( c ); } digest2 = bos.toByteArray(); }catch(Exception e){ LOG.error("Failed to create a digest.",e); return false; } if (digest1.length != digest2.length) { // Not a match! because of length. return false; } for (int i = 0; i < digest1.length; i++) { if (digest1[i] != digest2[i]) { // Not a match because of byte:"+i+" did not match return false; } } // Match was ok return true; } /** Compares a clear text code or ID with a candidate hash code. * This is used to confirm that the clearTextID can be successfully converted to the hash. * @param clearTextID A string that is to be hashed. This can be any string used for hashing or hiding data. * @param testHash A string of hashed string. * @return true if the hash created from clearTextID is equal to the testHash string.Can return false if SHA-1 does not exist on platform. */ public final boolean test(String clearTextID,byte[] testHash) { byte[] digest1 = generateHash(clearTextID); if (digest1.length != testHash.length) { // Not a match! because of length. return false; } for (int i = 0; i < testHash.length; i++) { if (digest1[i] != testHash[i]) { // Not a match because of byte:"+i+" did not match return false; } } // Match was ok return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -