views:

478

answers:

2

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() );
}
}
A: 

You could just grab the source code for Azureus and see how they do it.

CoverosGene
+1  A: 

I don't know about the correct algorithm in this case, but from a code perspective whenever you call getBytes() on a String you should always specify a character set, otherwise it uses the system default which often isn't what you want. Replace it with:

md.update( info.getBytes("UTF-8") );

and see if that helps.

Marc Novakowski