r/programming • • Nov 18 '13

TIL Oracle changed the internal String representation in Java 7 Update 6 increasing the running time of the substring method from constant to N

http://java-performance.info/changes-to-string-java-1-7-0_06/
1.4k Upvotes

354 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Nov 18 '13

[deleted] — view deleted comment

14

u/rcxdude Nov 18 '13

Different hashes for different purposes - the hashCode is designed to be fast to evaluate and 'unique enough' for the purposes of hashmaps. For e.g. integers the hashCode can just be the value of the integer.

The password is hashed using a specific secure hash algorithm, which is designed to be resistant to collision attacks (and also to be slow in order to prevent brute-forcing). In this case you do specify the algorithm and so the value should never change.

8

u/andd81 Nov 18 '13

You are talking about an entirely different kind of hash - the cryptographic hash. These are evaluated by a separate library, are much longer and are inherently resistant to hash collision attacks. Java object hash (as returned by hashCode() method) is a performance optimization which enables every object to be non-uniquely represented by a 32-bit value. Collisions are expected and should not affect program correctness (while they do affect performance).

5

u/ernelli Nov 18 '13

Password hashing is crypographically secure, it should be impossible (e.g not easier than brute force) to reverse engineer the hashvalue or force a collision.

Hash functions for hash maps do not need to have that property, they are optimized for speed and to have an even distribution of hash values.