Reputation: 77
I'm currently learning Qt5 for GUI applications. My first project is to make an authentication script and connect it to our database.
The problem is the password column is populated by PHP's crypt()
, which generates a hash string that starts with $1$
.
(e.g. echo crypt("password");
prints $1$d41.iA3.$XfuFXpCJfxSduzidGnKBR0
How do I use Qt to compare the password inputted in my GUI application to the password column in database that's generated by crypt()
?
Upvotes: 0
Views: 372
Reputation: 11418
TL;DR: Use a key derivation function for password storing.
PHP's crypt()
is horrible to use from other programming languages, because
Apart from that you're using plain md5 as a password hash algorithm. Never use md5 for password hashing. Read more. And more. And more.
But let's get our handy dirty then.
crypt()
s output is the following: $1$
with no algorithm options means MD5.
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
or re-implementing base64 if performance matters.d41.iA3.
is your case).foo = password || $1$ || salt
where ||
is the string concatenation. Use QByteArray as type for foo
.md5(password || salt || password)
and call it bar
.bar = '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0'
length(password)
bytes from bar
(in binary representation) and append it to foo
. Repeat bar
as often as needed if length(password) > 16
.Uff, let me quote the original source Then something really weird...
for (j = 0, i = length(password); i; i >>= 1)
if (i & 1)
foo += bar[j]
else
foo += password[j]
which I hope i ready properly from the source.
bar = md5(foo)
.Do that
for (i = 0; i < 1000; i++) {
moo = ""
if (i & 1) {
moo += password
}
else {
moo += bar
}
if (i % 3) {
moo += salt
}
if (i % 7) {
moo += password
}
if (i & 1) {
moo += bar
}
else {
moo += password
}
bar = md5(moo)
}
Glue everything together: $1$ || salt || $ || base64(bar)
.
Upvotes: 4