Skip to content

Why did I get the hash incorrectly? #5

Description

@zzyandzzy

java env

java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

critical code

/**
 * torrentFile see torrent.zip
 */
byte[] torrentData = FileUtils.readFileToByteArray(new File(torrentFile))
Bencode bencode = new Bencode();
Map<String, Object> torrentMap = bencode.decode(torrentData, Type.DICTIONARY);
Map<String, Object> info = (Map<String, Object>) torrentMap.get("info");
byte[] hashByte = hash(bencode.encode(info));
System.out.println(bytesToHex(hashByte));

public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_SYMBOLS[v >>> 4];
            hexChars[j * 2 + 1] = HEX_SYMBOLS[v & 0x0F];
        }
        return new String(hexChars);
    }

The correct hash is:
C8AE498D84E1ABC0EE9A72046E31E82B6A5C8C74
but i get hash is
D5FFE89589B9BACB195C2771C3FC328BF3880623

torrent.zip

thx, - ̗̀(๑ᵔ⌔ᵔ๑)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions