I am trying to calculate the info_hash
value for a torrent. I read the whole torrent into StringBuffer
, then cut it like the following:
d8:announce...info[d6:length...e]e
I can't seem to get the correct hash. Does reading the torrent into a StringBuffer
corrupt the byte string at the end? Am I missing something?
public void calculateInfoHash( ){
try{
int index = rawData.indexOf("4:info") + 6;
int end = rawData.length() - 1;
String info = rawData.substring( index , end );
MessageDigest md = MessageDigest.getInstance( "SHA" );
md.update( info.getBytes() );
byte[] digest = md.digest();
for ( byte b : digest ) {
// print byte as 2 hex digits with lead 0.
//Separate pairs of digits with space
//System.out.print( "%" );
System.out.printf( "%02X", b & 0xff );
}
System.out.println( );
}catch( Exception e ) {
System.out.println( e.toString() );
}
}