Changeset 87953c4


Ignore:
Timestamp:
May 27, 2009 4:59:38 PM (11 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
665b691
Parents:
be480d5
Message:
  • Peer Selector: Make strict order opaque to hash value
File:
1 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/tunnel/pool/TunnelPeerSelector.java

    rbe480d5 r87953c4  
    1212
    1313import net.i2p.I2PAppContext;
     14import net.i2p.crypto.SHA256Generator;
    1415import net.i2p.data.DataFormatException;
    1516import net.i2p.data.Hash;
     
    478479    }
    479480
     481    /**
     482     *  Implement a deterministic comparison that cannot be predicted by
     483     *  others. A naive implementation (using the distance from a random key)
     484     *  allows an attacker who runs two routers with hashes far apart
     485     *  to maximize his chances of those two routers being at opposite
     486     *  ends of a tunnel.
     487     *
     488     *  Previous:
     489     *     d(l, h) - d(r, h)
     490     *
     491     *  Now:
     492     *     d((H(l+h), h) - d(H(r+h), h)
     493     */
    480494    private class HashComparator implements Comparator {
    481495        private Hash _hash;
     
    485499        }
    486500        public int compare(Object l, Object r) {
    487             BigInteger ll = PeerSelector.getDistance(_hash, (Hash) l);
    488             BigInteger rr = PeerSelector.getDistance(_hash, (Hash) r);
     501            byte[] data = new byte[2*Hash.HASH_LENGTH];
     502            System.arraycopy(_hash.getData(), 0, data, Hash.HASH_LENGTH, Hash.HASH_LENGTH);
     503            System.arraycopy(((Hash) l).getData(), 0, data, 0, Hash.HASH_LENGTH);
     504            Hash lh = SHA256Generator.getInstance().calculateHash(data);
     505            System.arraycopy(((Hash) r).getData(), 0, data, 0, Hash.HASH_LENGTH);
     506            Hash rh = SHA256Generator.getInstance().calculateHash(data);
     507            BigInteger ll = PeerSelector.getDistance(_hash, lh);
     508            BigInteger rr = PeerSelector.getDistance(_hash, rh);
    489509            return ll.compareTo(rr);
    490510        }
Note: See TracChangeset for help on using the changeset viewer.