2017-10-27 18:58:46 +02:00
|
|
|
module calendarwebapp.passhash;
|
|
|
|
|
|
|
|
interface PasswordHasher
|
|
|
|
{
|
2017-11-10 18:20:55 +01:00
|
|
|
string generateHash(in string password) const @safe;
|
2017-10-27 19:52:49 +02:00
|
|
|
bool checkHash(in string password, in string hash) const @safe;
|
2017-10-27 18:58:46 +02:00
|
|
|
}
|
|
|
|
|
2017-11-10 18:20:55 +01:00
|
|
|
class StubPasswordHasher : PasswordHasher
|
2017-10-27 18:58:46 +02:00
|
|
|
{
|
2017-11-10 18:20:55 +01:00
|
|
|
string generateHash(in string password) const @safe pure nothrow
|
2017-10-27 18:58:46 +02:00
|
|
|
{
|
2017-11-10 18:20:55 +01:00
|
|
|
return password;
|
2017-10-27 18:58:46 +02:00
|
|
|
}
|
|
|
|
|
2017-11-10 18:20:55 +01:00
|
|
|
bool checkHash(in string password, in string hash) const @safe pure nothrow
|
2017-10-27 18:58:46 +02:00
|
|
|
{
|
2017-11-10 18:20:55 +01:00
|
|
|
return password == hash;
|
2017-10-27 18:58:46 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-27 19:52:49 +02:00
|
|
|
|
2017-11-10 18:20:55 +01:00
|
|
|
class SHA256PasswordHasher : PasswordHasher
|
2017-10-27 19:52:49 +02:00
|
|
|
{
|
2017-11-10 18:31:47 +01:00
|
|
|
import dauth : dupPassword, isSameHash, makeHash, parseHash;
|
2017-11-10 18:20:55 +01:00
|
|
|
import std.digest.sha : SHA256;
|
|
|
|
|
|
|
|
string generateHash(in string password) const @safe
|
2017-10-27 19:52:49 +02:00
|
|
|
{
|
2017-11-10 18:20:55 +01:00
|
|
|
return (() @trusted => password.dupPassword.makeHash!SHA256.toCryptString)();
|
2017-10-27 19:52:49 +02:00
|
|
|
}
|
|
|
|
|
2017-11-10 18:20:55 +01:00
|
|
|
bool checkHash(in string password, in string hash) const @safe
|
2017-10-27 19:52:49 +02:00
|
|
|
{
|
2017-11-10 18:20:55 +01:00
|
|
|
return (() @trusted => isSameHash(password.dupPassword, parseHash(hash)))();
|
2017-10-27 19:52:49 +02:00
|
|
|
}
|
|
|
|
}
|