2017-10-27 18:58:46 +02:00
|
|
|
module calendarwebapp.passhash;
|
|
|
|
|
|
|
|
import poodinis;
|
|
|
|
|
|
|
|
interface PasswordHasher
|
|
|
|
{
|
|
|
|
string generateHash(in string password) @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
|
|
|
}
|
|
|
|
|
|
|
|
class BcryptPasswordHasher : PasswordHasher
|
|
|
|
{
|
|
|
|
import botan.passhash.bcrypt : checkBcrypt, generateBcrypt;
|
|
|
|
import botan.rng.rng : RandomNumberGenerator;
|
|
|
|
|
|
|
|
string generateHash(in string password) @safe
|
|
|
|
{
|
|
|
|
return (() @trusted => generateBcrypt(password, rng, cost))();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
return (()@trusted => checkBcrypt(password, hash))();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
@Autowire RandomNumberGenerator rng;
|
|
|
|
enum cost = 10;
|
|
|
|
}
|
2017-10-27 19:52:49 +02:00
|
|
|
|
|
|
|
class StubPasswordHasher : PasswordHasher
|
|
|
|
{
|
|
|
|
string generateHash(in string password) const @safe pure nothrow
|
|
|
|
{
|
|
|
|
return password;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool checkHash(in string password, in string hash) const @safe pure nothrow
|
|
|
|
{
|
|
|
|
return password == hash;
|
|
|
|
}
|
|
|
|
}
|