chore: further code cleanups suggested by clazy in the oath module

These cleanups block the invalid password detection/retry UX feature from landing.

See-Also: https://invent.kde.org/utilities/keysmith/-/merge_requests/71
master
Johan Ouwerkerk 2020-11-23 20:15:55 +01:00
parent 0a74440fe4
commit eb39c18fe2
2 changed files with 11 additions and 11 deletions

View File

@ -97,7 +97,7 @@ namespace oath
return encoder && encoder->tokenLength() >= 6; return encoder && encoder->tokenLength() >= 6;
} }
bool Algorithm::validate(QCryptographicHash::Algorithm algorithm, const std::optional<uint> &offset) bool Algorithm::validate(QCryptographicHash::Algorithm algorithm, const std::optional<uint> offset)
{ {
/* /*
* An nullopt offset indicates dynamic truncation. * An nullopt offset indicates dynamic truncation.
@ -114,7 +114,7 @@ namespace oath
return digestSize && *digestSize >= 4U && (*digestSize - 4U) >= truncateAt; return digestSize && *digestSize >= 4U && (*digestSize - 4U) >= truncateAt;
} }
std::optional<Algorithm> Algorithm::create(QCryptographicHash::Algorithm algorithm, const std::optional<uint> &offset, const QSharedPointer<const Encoder> &encoder, bool requireSaneKeyLength) std::optional<Algorithm> Algorithm::create(QCryptographicHash::Algorithm algorithm, const std::optional<uint> offset, const QSharedPointer<const Encoder> &encoder, bool requireSaneKeyLength)
{ {
if(!validate(algorithm, offset)) { if(!validate(algorithm, offset)) {
qCDebug(logger) << "Invalid algorithm:" << algorithm << "or incompatible with truncation offset:" << (offset ? *offset : 16U); qCDebug(logger) << "Invalid algorithm:" << algorithm << "or incompatible with truncation offset:" << (offset ? *offset : 16U);
@ -126,10 +126,10 @@ namespace oath
return std::nullopt; return std::nullopt;
} }
std::function<quint32(QByteArray)> truncation(truncateDynamically); std::function<quint32(const QByteArray &)> truncation(truncateDynamically);
if (offset) { if (offset) {
uint at = *offset; uint at = *offset;
truncation = [at](QByteArray bytes) -> quint32 truncation = [at](const QByteArray &bytes) -> quint32
{ {
return truncate(bytes, at); return truncate(bytes, at);
}; };
@ -144,13 +144,13 @@ namespace oath
return create(algorithm, std::nullopt, encoder, requireSaneKeyLength); return create(algorithm, std::nullopt, encoder, requireSaneKeyLength);
} }
std::optional<Algorithm> Algorithm::hotp(const std::optional<uint> &offset, uint tokenLength, bool checksum, bool requireSaneKeyLength) std::optional<Algorithm> Algorithm::hotp(const std::optional<uint> offset, uint tokenLength, bool checksum, bool requireSaneKeyLength)
{ {
const QSharedPointer<const Encoder> encoder(new Encoder(tokenLength, checksum)); const QSharedPointer<const Encoder> encoder(new Encoder(tokenLength, checksum));
return create(QCryptographicHash::Sha1, offset, encoder, requireSaneKeyLength); return create(QCryptographicHash::Sha1, offset, encoder, requireSaneKeyLength);
} }
Algorithm::Algorithm(const QSharedPointer<const Encoder> &encoder, const std::function<quint32(QByteArray)> &truncation, QCryptographicHash::Algorithm algorithm, bool requireSaneKeyLength) : Algorithm::Algorithm(const QSharedPointer<const Encoder> &encoder, const std::function<quint32(const QByteArray &)> &truncation, QCryptographicHash::Algorithm algorithm, bool requireSaneKeyLength) :
m_encoder(encoder), m_truncation(truncation), m_enforceKeyLength(requireSaneKeyLength), m_algorithm(algorithm) m_encoder(encoder), m_truncation(truncation), m_enforceKeyLength(requireSaneKeyLength), m_algorithm(algorithm)
{ {
} }

View File

@ -38,16 +38,16 @@ namespace oath
{ {
public: public:
static bool validate(const Encoder *encoder); static bool validate(const Encoder *encoder);
static bool validate(QCryptographicHash::Algorithm algorithm, const std::optional<uint> &offset); static bool validate(QCryptographicHash::Algorithm algorithm, const std::optional<uint> offset);
static std::optional<Algorithm> create(QCryptographicHash::Algorithm algorithm, const std::optional<uint> &offset, const QSharedPointer<const Encoder> &encoder, bool requireSaneKeyLength = false); static std::optional<Algorithm> create(QCryptographicHash::Algorithm algorithm, const std::optional<uint> offset, const QSharedPointer<const Encoder> &encoder, bool requireSaneKeyLength = false);
static std::optional<Algorithm> totp(QCryptographicHash::Algorithm algorithm, uint tokenLength, bool requireSaneKeyLength = false); static std::optional<Algorithm> totp(QCryptographicHash::Algorithm algorithm, uint tokenLength, bool requireSaneKeyLength = false);
static std::optional<Algorithm> hotp(const std::optional<uint> &offset, uint tokenLength, bool checksum, bool requireSaneKeyLength = false); static std::optional<Algorithm> hotp(const std::optional<uint> offset, uint tokenLength, bool checksum, bool requireSaneKeyLength = false);
std::optional<QString> compute(quint64 counter, char * secretBuffer, int length) const; std::optional<QString> compute(quint64 counter, char * secretBuffer, int length) const;
private: private:
Algorithm(const QSharedPointer<const Encoder> &encoder, const std::function<quint32(QByteArray)> &truncation, QCryptographicHash::Algorithm algorithm, bool requireSaneKeyLength); Algorithm(const QSharedPointer<const Encoder> &encoder, const std::function<quint32(const QByteArray &)> &truncation, QCryptographicHash::Algorithm algorithm, bool requireSaneKeyLength);
private: private:
const QSharedPointer<const Encoder> m_encoder; const QSharedPointer<const Encoder> m_encoder;
const std::function<quint32(QByteArray)> m_truncation; const std::function<quint32(const QByteArray &)> m_truncation;
bool m_enforceKeyLength; bool m_enforceKeyLength;
const QCryptographicHash::Algorithm m_algorithm; const QCryptographicHash::Algorithm m_algorithm;
}; };