Cockatrice 2026-10-19-Development-3.1.2-beta.2
A virtual tabletop for multiplayer card games
Loading...
Searching...
No Matches
passwordhasher.h
Go to the documentation of this file.
1#ifndef PASSWORDHASHER_H
2#define PASSWORDHASHER_H
3
4#include <QByteArray>
5#include <QObject>
6#include <QString>
7
8// scrypt cost parameters used for newly created password verifiers. These match
9// the RFC 7914 recommended parameters for interactive use.
10constexpr int SCRYPT_N = 32768;
11constexpr int SCRYPT_R = 8;
12constexpr int SCRYPT_P = 1;
13constexpr int SCRYPT_SALT_LENGTH = 16;
14constexpr int SCRYPT_VERIFIER_LENGTH = 64;
15
17{
18 None = 0,
20};
21
23{
25 int n = 0;
26 int r = 0;
27 int p = 0;
28 QByteArray salt;
29 QByteArray verifier;
30 bool isValid = false;
31};
32
34{
35public:
36 static QString computeHash(const QString &password, const QString &salt);
37 static QString generateRandomSalt(const int len = 16);
38 static QString generateActivationToken();
39
41 static QByteArray deriveKey(const QString &password, const QByteArray &salt, int n, int r, int p);
43 static bool costParamsAreSane(int n, int r, int p);
45 static QString generatePasswordVerifier(const QString &password);
47 static PasswordVerifier parsePasswordVerifier(const QString &stored);
49 static bool isLegacyFormat(const QString &stored);
51 static bool verifyPassword(const QString &password, const QString &storedPasswordData);
53 static QByteArray computeResponse(const QByteArray &key, const QByteArray &nonce);
55 static bool constantTimeEquals(const QByteArray &a, const QByteArray &b);
56};
57
58#endif
Definition passwordhasher.h:34
static bool costParamsAreSane(int n, int r, int p)
True if the scrypt cost parameters are acceptable for server and client use.
Definition passwordhasher.cpp:76
static QByteArray deriveKey(const QString &password, const QByteArray &salt, int n, int r, int p)
Derive the scrypt verifier for the given password, salt and cost parameters. Empty on failure.
Definition passwordhasher.cpp:61
static bool constantTimeEquals(const QByteArray &a, const QByteArray &b)
Constant-time byte comparison.
Definition passwordhasher.cpp:164
static QString generateRandomSalt(const int len=16)
Definition passwordhasher.cpp:24
static QString computeHash(const QString &password, const QString &salt)
Definition passwordhasher.cpp:11
static QString generateActivationToken()
Definition passwordhasher.cpp:56
static QByteArray computeResponse(const QByteArray &key, const QByteArray &nonce)
HMAC-SHA256 of nonce keyed with the password verifier, used for challenge-response logins.
Definition passwordhasher.cpp:152
static QString generatePasswordVerifier(const QString &password)
Build a "$scrypt$<n>$<r>$<p>$<salt>$<verifier>" string with a fresh random salt.
Definition passwordhasher.cpp:83
static bool verifyPassword(const QString &password, const QString &storedPasswordData)
True if the password matches the stored credential, whether legacy salt+hash or scrypt.
Definition passwordhasher.cpp:138
static PasswordVerifier parsePasswordVerifier(const QString &stored)
Parse a stored "$scrypt$..." string into its components.
Definition passwordhasher.cpp:95
static bool isLegacyFormat(const QString &stored)
True if the stored value is not in the scrypt format (legacy salt+hash).
Definition passwordhasher.cpp:133
constexpr int SCRYPT_VERIFIER_LENGTH
Definition passwordhasher.h:14
constexpr int SCRYPT_SALT_LENGTH
Definition passwordhasher.h:13
constexpr int SCRYPT_N
Definition passwordhasher.h:10
constexpr int SCRYPT_R
Definition passwordhasher.h:11
constexpr int SCRYPT_P
Definition passwordhasher.h:12
PasswordFormat
Definition passwordhasher.h:17
@ Scrypt
Definition passwordhasher.h:19
@ None
Definition passwordhasher.h:18
Definition passwordhasher.h:23
int r
Definition passwordhasher.h:26
PasswordFormat format
Definition passwordhasher.h:24
QByteArray verifier
Definition passwordhasher.h:29
QByteArray salt
Definition passwordhasher.h:28
int p
Definition passwordhasher.h:27
bool isValid
Definition passwordhasher.h:30
int n
Definition passwordhasher.h:25
@ None
Definition tally.h:22