I am trying to calculate the value info_hashfor a torrent. I read the entire stream in StringBufferand then cut it as follows:
d8:announce...info[d6:length...e]e
I can not get the correct hash. Reading torrent in will StringBufferdamage byte string at the end? Did I miss 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 ) {
System.out.printf( "%02X", b & 0xff );
}
System.out.println( );
}catch( Exception e ) {
System.out.println( e.toString() );
}
}
source
share