6 ms·
Salted Password Hashing – Doing It Right
- sarciszewski 12y agoThis page is old. Nowadays, PHP developers are encouraged to use password_hash() and password_verify(). But if you want PBKDF2, defuse's github has more up-to-date code that, IIRC, he hasn't taken the time to copy/paste into the page linked. https://github.com/defuse/password-hashing https://github.com/defuse/password-hashing
- McGlockenshire 12y agoIf you want PBKDF2 and are using PHP >= 5.5, there's the hash_pbkdf2 function built in to ext/hash, which should be available by default in most installations. There's also another implementation in PHP-CryptLib by Anthony Ferrara, the gentlemen that brought the current password API to PHP. https://github.com/ircmaxell/PHP-CryptLib https://github.com/ircmaxell/PHP-CryptLib
- sarciszewski 12y agoThe PBKDF2 code that defuse wrote will use hash_pbkdf2() if it's available. :)
- billturner 12y agoPage was timing out for me. Here's the google cache: http://webcache.googleusercontent.com/search?q=cache:S76Yg0P_ArEJ:https://crackstation.net/hashing-security.htm+&cd=1&hl=en&ct=clnk&gl=us http://webcache.googleusercontent.com/search?q=cache:S76Yg0P...
- atoponce 12y agoOverall, the article is solid. It is only missing key stretching with fast cryptographic hashing algorithms like SHA-1 or SHA-2. Instead, it jumps right into PBKDF2 and bcrypt. That's fine, but it's not the full story, and as such, does a disservice for those wanting to know about securely storing passwords on disk. PBKDF2 and bcrypt can be a performance killer for authentication applications, if the host is a busy host. Instead, changing the number of rotations on a fast hashing algorithm gives you finer granularity on brute force speed versus application and host reliability.
- eterm 12y agoThe whole point of bcrypt is to be slow. You can set the number of rounds to fine-tune that too, and can even do it on a user by user basis since the number of rounds and version is stored within the hash output.
- atoponce 12y agoOf course bcrypt is supposed to be slow. However, the "fastest" bcrypt can operate, might be a local DDoS for your sever, if the authentication app is a busy one. Key stretching cryptographic hashing algorithms gives you more granular control in that regard.
- tptacek 12y agoSo does every database lookup every application does. The solution to that problem is rate limiting, not (weird that I have to say this) weakening your password authenticators!
- atoponce 12y agoSecurity margins here (IE: bcrypt versus SHA-2) are estimated with entropy, which is also completely missing in the article. A weak password is a weak password, bcrypt or not. If there is sufficient entropy behind the password, 500,000 rotations of SHA-512 is not "weaker" than bcrypt, Only faster. You should know this (weird that I had to say that).
- ghshephard 12y agoSpeeding up your password hash mechanism makes it weaker. The faster your password hash system, the more quickly your password list can be broken. This is probably the best commentary I've seen on the topic of how many rounds you should incorporate into your KDF (Scrypt/Bcrypt/PBKDF2 all let you dial as many as you wish), so I won't try and improve on it: http://security.stackexchange.com/a/3993 http://security.stackexchange.com/a/3993
- drinchev 12y agoI like this article. If every developer read it. Significant part of any database would be useless for attackers. Cudos to the author.
- solomone 12y agoHere's a node way of doing it: https://github.com/solomon23/solomon-angular-node-seed/blob/master/server/utils/hash.coffee https://github.com/solomon23/solomon-angular-node-seed/blob/...
- sehrope 12y agoFrom the link: ITERATIONS = 600 ... crypto.pbkdf2 pwd, salt, ITERATIONS, LEN, (err, hash) -> That's way too small for the number of iterations. Something like 100K would be a better choice. Alternatively here's a version that uses bcrypt: bcrypt = require 'bcrypt' rounds = Number(process.env.BCRYPT_ROUNDS || 12) module.exports = hash: (password, cb) -> bcrypt.hash password, rounds, cb compare: (password, hashedPassword, cb) -> bcrypt.compare password, hashedPassword, cb
- solomone 12y agoFrom my testing 600 took about 15ms. 100k would take about 2.5 seconds.
- sehrope 12y agoFor me 600 iterations takes about 3ms (I guess my laptop is a bit faster). A decent range to shoot for is .5-1 sec. Test program: crypto = require 'crypto' password = 'testing' len = 128 salt = crypto.randomBytes(len) iters = Number(process.argv[2] || 600) console.log 'Testing iters=%s', iters for i in [1..10] start = Date.now() crypto.pbkdf2Sync password, salt, iters, len elapsed = Date.now() - start console.log ' Test #%s - %s ms', i, elapsed Output: $ coffee pbkdf2-test.coffee 100000 Testing iters=100000 Test #1 - 497 ms Test #2 - 510 ms Test #3 - 496 ms Test #4 - 525 ms Test #5 - 510 ms Test #6 - 493 ms Test #7 - 521 ms Test #8 - 518 ms Test #9 - 510 ms Test #10 - 498 ms $ coffee pbkdf2-test.coffee 10000 Testing iters=10000 Test #1 - 54 ms Test #2 - 50 ms Test #3 - 50 ms Test #4 - 55 ms Test #5 - 51 ms Test #6 - 52 ms Test #7 - 50 ms Test #8 - 49 ms Test #9 - 51 ms Test #10 - 50 ms $ coffee pbkdf2-test.coffee 600 Testing iters=600 Test #1 - 3 ms Test #2 - 3 ms Test #3 - 3 ms Test #4 - 3 ms Test #5 - 3 ms Test #6 - 4 ms Test #7 - 3 ms Test #8 - 4 ms Test #9 - 3 ms Test #10 - 3 ms
- eterm 12y agoObligatory link to "How to safely store a password". http://codahale.com/how-to-safely-store-a-password/ http://codahale.com/how-to-safely-store-a-password/
- tptacek 12y agoIf you're simply trying to solve the problem of storing passwords, Coda's article is better than this one.
- sarciszewski 12y agoYeah. I think defuse wrote that page 3-4 years ago and hasn't really maintained it since then. For PHP Devs, these are the two best options available: * https://github.com/ircmaxell/password_compat https://github.com/ircmaxell/password_compat * https://github.com/DomBlack/php-scrypt https://github.com/DomBlack/php-scrypt
- kefka 12y agoQuoted: "In step 4, never tell the user if it was the username or password they got wrong. Always display a generic message like "Invalid username or password." This prevents attackers from enumerating valid usernames without knowing their passwords. " Completely, utterly wrong. It 'looks' secure. We're not telling the user if it's a login or password. But if you try to create an account with the username, it tells you "CANNOT CREATE, USER ALREADY EXISTS". So, it's an anti-pattern that serves to confuse the user but fails to stop a scripted attack.
- rurounijones 12y agoBut if you try to create an account with the username, it tells you "CANNOT CREATE, USER ALREADY EXISTS". Then don't do it, give a generic error message and send an email to the accounts email address saying "Did you try to sign-up, you already have an account, here is the reset link in case you forgot the password"
- dag11 12y agoSending an email to the registered account is not a solution, because that doesn't tell the registering user that the username is unavailable.
- kefka 12y agoIf a username isn't obtainable, it would be fair to assume the username is in use. I know of no situations that seed "unobtainable usernames" in a DB. Although now, I would expect that.
- amckenna 12y agoThey are correct, it is important to show a generic message on the login. The login is the first place an attacker will look for user enumeration. Just because you may be able to find user enumeration somewhere else doesn't mean you should leave it open everywhere. For user enumeration on registration you have two choices: 1. Provide a unique registration link to an email for signup. The prospective user goes to the registration page, submits and email, checks their email, then follows the link to a registration page. On this page it's easy to see if someone is enumerating usernames, and every request contains the token from their unique signup link so the token can be invalidated quickly. 2. Provide a slow rate limited lookup to verify an existing username, every 2-3 seconds, which shouldn't negatively affect users as they signup because they have to fill out a registration form in the mean time. This will however greatly slow down a bot.
- dperfect 12y agoIf you're using Rails (or ActiveRecord/ActiveModel specifically), you should probably skip the suggested Ruby example and just use has_secure_password. It's built in, uses bcrypt, and greatly reduces the likelihood of making mistakes in a custom implementation.
- kerkeslager 12y agoThe fact that we're still talking about password hashing shows how much we're losing the war on security. Whether or not a service stores passwords correctly cannot be verified by the user. There is no up-front cost to storing passwords incorrectly, so sites have inadequate incentive to store their passwords safely. As such users must use different passwords for every site. This simply is too technical an issue for most users: no amount of education will fix this, and password managers are too much work for most users. The end result is that even if you hash your passwords correctly, your user's passwords will be compromised on other systems and this will lead to accounts on your system being compromised. The solution to this is zero-knowledge password proofs[1]. Essentially, you should never be passing your password to another party. Instead, you should be providing the authenticating party with a prover (analogous to a public key in asymmetric-key encryption, but based off your password) when you sign up. Then when you want to authenticate, you provide the server with a proof based off your password which exposes no information about your password but can be used to verify that you have your password using the prover (analogous to a signature in asymmetric-key encryption, but again based off your password). There are a number of ways to generate these provers and proofs using existing asymmetric-key algorithms such as RSA, ElGamal, or ECDSA. Once this becomes pervasive, browsers and other network applications can provide obnoxious security warnings similar to those currently provided for untrusted certificates. This will create an up-front cost for people still using password hashing and shame them into moving to ZKPP, while providing a user-friendly way for users to tell if their password is being used insecurely. Only once people are no longer giving their passwords to anyone with a login page can we be sure that poor security on other sites won't compromise our own servers. EDIT: Additionally, ZKPP mitigates some of the damage caused by man-in-the-middle attacks. Currently, if an attacker can gain access to communications between a user and server, they can access the password in plaintext when it is passing over the wire, even if it is adequately hashed on the server. This has both permanence (the password continues to be useful after the vulnerability is discovered--until the user changes their password) and wide application (the password can be used on multiple sites). With ZKPP the attacker only has access to the proof of the password. This might allow the attacker to authenticate with the server during the attack. However, a proper ZKPP implementation uses unique nonces from the server to create the proofs and rejects non-unique proofs, so the proof cannot be used after the vulnerability has been fixed (the server will check that it is unique) and it cannot be used on other sites (the nonce is provided by the server). [1] http://en.wikipedia.org/wiki/Zero-knowledge_password_proof http://en.wikipedia.org/wiki/Zero-knowledge_password_proof
- FabianBeiner 12y agoIf you’re using PHP 5.5+, this should be all you need to securely save passwords: $sHash = password_hash('ThePassword', PASSWORD_BCRYPT, ['cost' => 12]); Checking is as easily as: if (password_verify('ThePassword', $sHash)) {…} Or do you guys have other thoughts?
- kxo 12y agobase64 encode the password, reverse the encoded result.
- cpach 12y agoWhat security advantage would that yield?
- sarciszewski 12y agohttp://cryptofails.com http://cryptofails.com -- I believe they're referring to 46ESAB the worst password hashing algorithm ever
- cpach 12y agoThat’s hilarious! :D
- Sami_Lehtinen 12y agoWhole hashing and salting etc stuff is pointless. What does matter? Is the fact that passwords aren't reused anyway. Do I really care if you know that my password for service X was: c'EyqXnrq-bCyfF_dK67$j I don't really couldn't care less, if you get it hashed or not, really. If they owned the system, and they were after my data, they got it already. Password(s) is just minute and meaningless detail. I've always wondered this pointless discussion about passwords. It just doesn't make any sense.
- tedunangst 12y ago> Is the fact that passwords aren't reused anyway. Completely wrong. People reuse passwords all the time.