0

Well, I have always seen (and following) people saying to use hashing mechanism for storing passwords in database. I am really concerned is it secure?

Lets go with example.

Let's say I am hacker and I got your database name, id and password. Now I have FULL access to your database.

What people say passwords should be hashed because if someone hacks, they are visible to hackers.

so If I run query as select id, password FROM userDetails I will get data as below

Option 1 : Without hash

++++++++++++++++++++++++++++
+   id    +    password    +
++++++++++++++++++++++++++++
+  id01    +  password01   +
+  id02    +  password02   +
++++++++++++++++++++++++++++

Option 2 : With Hash

++++++++++++++++++++++++++++
+   id    +    password    +
++++++++++++++++++++++++++++
+  id01    +  hasValue01   +
+  id02    +  hasValue02   +
++++++++++++++++++++++++++++

Well, still I say, hashing is insecure. Why I will tell you below with Java code.

PreparedStatement pst = conn.prepareStatement("SELECT id, password FROM userDetails");
ResultSet rs = pst.executeQuery();
String randomPassword = "";
StringBuffer sb;
boolean myPassCheck = true;
while (rs.next()) {
    myPassCheck = true;
    while (myPassCheck) {
        // this will generate random password
        randomPassword = generateRandomPassword();
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(randomPassword.getBytes());
        sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
        }
        if (sb.toString().equals(rs.getString(2))) {
            // this is password
            myPassCheck = false;
            System.out.print("id=" + rs.getString(1) + ", password=" + sb.toString());
        }
    }
}

This way, I can print the username and password. (I know I will have to generate random password till I have not found the password). However in this way, hashing password mechanism also fails.

Also I believe there is decryptor present in this world which will convert the hash data to actual data.

Hence, I am thinking

Is Hashing Mechanism Is Secure?


Edit 1

I am not talking about MD5 only. I choose MD5 for example purpose ONLY. I am talking about any mechanism for secure password

4
  • if not MD5, what else should I use then? Nov 10, 2012 at 15:06
  • @dystroy : If I could have asked Mr. Google, I would have not asked this question here. Nov 10, 2012 at 15:07
  • 3
    Security is not binary. Hashing is more secure. Salted hashing is even more secure. Isolated databases with salted hashing is even more secure ...
    – cheeken
    Nov 10, 2012 at 15:08
  • 3 down vote? I am surprised... wow wow wow!!! Nov 11, 2012 at 13:29

4 Answers 4

4

No, just using hash, especially when just applying MD5 to the password, isn't secure because :

  • if you just convert a password (which is frequently almost a common word), it's easy to test it using a database of common passwords. So always use a salt and preferably a combination of other constant parts of the user record. Sometimes you may even find the origin of a hash by typing the hash in Google...
  • MD5 is prone to collisions, so even with a hash, prefer SHA 256 or better
  • MD5 is fast to compute, so fast to test in a brute force attack

Use the hashing mechanism but :

  • take a better hash function, for example SHA-256 or SHA-512
  • hash a constructed string, for example username+salt+password (you can use a constant salt as long as it's not used by other programs, it's enough for most uses)

The process when you register somebody (or somebody changes his password) is

1) to build the string s as s=username+salt+password (or a similar function)

2) to build the hash as SHA256(s)

3) to store in database the username, the salt (if not constant) and the hash

When authenticating a user, you build the hash in the same way (using the username and password given by the user and the salt you have in database) and you compare the username and the hash to what you have in database. You don't reverse the hash function, because that's not feasible.


Now regarding your code : your approach seems to be to generate all possible passwords until one has the same MD5 than the password you're trying to guess. This won't work for reasonable passwords because there isn't enough time to test all combinations of 15 characters.

9
  • I don't think SHA-256 or SHA-512 are also secure. I will explain WHY by editing my question. Before that, let me read your link first. :) Nov 10, 2012 at 15:15
  • I have one question : suppose my password is @p1a2s3s@ & when I used SHA-256, I get output as 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4. Now I believe whenever I give input to SHA-256 as @p1a2s3s@, I will get output as 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4. Right? Nov 10, 2012 at 15:19
  • I don't get your question in this comment. SHA-256 isn't reversible. Nov 10, 2012 at 15:20
  • If it is not reversible, how can I compare my password? When I enter my username and password, I will have to match them Nov 10, 2012 at 15:22
  • You won't compare the username and password : you will hash them (with a salt) and compare the hash to your stored hash. Nov 10, 2012 at 15:23
3

Passwords are never stored in plaintext. At least they shouldn't be, unless you're building the world's most insecure system using the world's most naive programmers.

Instead, passwords are stored as the output of a hash function. Even if an attacker gained access to the hashed version of your password, it's not possible to reconstitute the password from the hash value alone.

But it is possible to attack the hashed value of your password using rainbow tables: enormous, pre-computed hash values for every possible combination of characters. An attacking PC could certainly calculate all these hashes on the fly, but taking advantage of a massive table of pre-computed hash values enables the attack to proceed several orders of magnitude faster-- assuming the attacking machine has enough RAM to store the entire table (or at least most of it) in memory. A table like this is called a rainbow table

It's a classic time-memory tradeoff, exactly the sort of cheating shortcut you'd expect a black hat attacker to take.

A database for follwing char set:ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_+=~[]{}|\:;"'<>,.?/ with a max password length of 14 would have a size of 64GB.

If you've salted your password hashes, an attacker can't use general rainbow table attack against you the hash results from "password" and "deliciously-salty-password" won't match. Unless your hacker somehow knows that all your hashes are "delicously-salty-" ones.

Even then, he or she would have to generate a custom rainbow table specifically for you.

Please read Thomas Ptacek's excellent and informative article on this topic. It goes into much more detal about the nuts and bolts of password hashing.

And finally the answer to your Question:

NO it is not secure, but you can do your best to make it as secure as possible.

3
  • 1
    I disagree to this Even if an attacker gained access to the hashed version of your password, it's not possible to reconstitute the password from the hash value alone.. I showed above, how can I get password. Nov 10, 2012 at 15:09
  • 1
    I also give a way to do it... using a database, to be faster then your solution...
    – Frank
    Nov 10, 2012 at 15:10
  • @FahimParkar You didn't show how you can get the password. You showed that a brute force attack (i.e. hashing every possible password, until you get a matching hash) is possible. But that isn't something new - in fact it's not even surprising; that is the only attack that is possible against an ideal cryptographically secure hash function. I would suggest that you pick up the excellent book "Applied Cryptography" by Bruce Schneier and educate yourself a bit more. Check out: amazon.com/Applied-Cryptography-Protocols-Algorithms-Source/dp/… Nov 11, 2012 at 0:34
1

No. Hashing passwords is not secure, but it's more secure than not doing this. As you can see such hashed passwords might be easily decrypted if you have enough time and hardware resources. This is why we often use salt to make our passwords more secure. The salt is something that is somehow merged with the real password and you store the hashcode of such string. The attacker might be able to break the encryption but he receives the password mixed with salt. This is a quite good protection to people who use so called 'rainbow tables' to decrypt the passwords. Unfortunatelly it's still breakable. Here you can find more info about that: http://arstechnica.com/security/2012/08/passwords-under-assault/

1
  • Ok. My fault and you're right. I'm not the native English speaker and that's why I used this word. Nov 10, 2012 at 15:24
1

No, they are not 100% secure, specially if the attacker has a hash database. A better approach is to use salted passwords, as this make hash databases quite useless.

Another way to make the password a bit more secure is to hash it several times, which also reduces the effectiveness of a hash database.

As other posts mentioned, pick a more secure and expensive algorithm like sha-512.

5
  • thanks, could you provide me proper link where salted password are explained properly with example? Nov 10, 2012 at 15:11
  • I am not sure if algorithm also be secure. Why because if I generate random password and give it to sha-512, it will give me output something like MD5 is giving. From there also I could get password. So I believe hashing fails everywhere... Nov 10, 2012 at 15:14
  • 1
    Feel feel to crack my password: 9357d8611d03db1c54bb6bae5e9f1bfa857f655e523455593bc474bd67797fa42de4c0b51ef28caa8710bccbfd1ad780d2bf91311d9951c2d18f9cb868015e1c (hint, unsalted sha 512) :)
    – Augusto
    Nov 10, 2012 at 15:16
  • I have one question : suppose my password is @p1a2s3s@ & when I used SALTED, I get output as 9357d8611d03db1c54bb6bae5e9f1bfa857f655e523455593bc474bd67797fa42de4c0b51ef28caa‌ . Now I believe whenever I give input to SALTED, I will get output as 9357d8611d03db1c54bb6bae5e9f1bfa857f655e523455593bc474bd67797fa42de4c0b51ef28caa‌ . Right? Nov 10, 2012 at 15:21
  • Wish I could be hacker... I would have surely hacked your passwords. I love to play with data. Nov 10, 2012 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.