From eb39c18fe2c66c0407d08db71ebc2b2b51ac234d Mon Sep 17 00:00:00 2001 From: Johan Ouwerkerk Date: Mon, 23 Nov 2020 20:15:55 +0100 Subject: [PATCH] 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 --- src/oath/oath.cpp | 12 ++++++------ src/oath/oath.h | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/oath/oath.cpp b/src/oath/oath.cpp index 5efb00f..617031e 100644 --- a/src/oath/oath.cpp +++ b/src/oath/oath.cpp @@ -97,7 +97,7 @@ namespace oath return encoder && encoder->tokenLength() >= 6; } - bool Algorithm::validate(QCryptographicHash::Algorithm algorithm, const std::optional &offset) + bool Algorithm::validate(QCryptographicHash::Algorithm algorithm, const std::optional offset) { /* * An nullopt offset indicates dynamic truncation. @@ -114,7 +114,7 @@ namespace oath return digestSize && *digestSize >= 4U && (*digestSize - 4U) >= truncateAt; } - std::optional Algorithm::create(QCryptographicHash::Algorithm algorithm, const std::optional &offset, const QSharedPointer &encoder, bool requireSaneKeyLength) + std::optional Algorithm::create(QCryptographicHash::Algorithm algorithm, const std::optional offset, const QSharedPointer &encoder, bool requireSaneKeyLength) { if(!validate(algorithm, offset)) { qCDebug(logger) << "Invalid algorithm:" << algorithm << "or incompatible with truncation offset:" << (offset ? *offset : 16U); @@ -126,10 +126,10 @@ namespace oath return std::nullopt; } - std::function truncation(truncateDynamically); + std::function truncation(truncateDynamically); if (offset) { uint at = *offset; - truncation = [at](QByteArray bytes) -> quint32 + truncation = [at](const QByteArray &bytes) -> quint32 { return truncate(bytes, at); }; @@ -144,13 +144,13 @@ namespace oath return create(algorithm, std::nullopt, encoder, requireSaneKeyLength); } - std::optional Algorithm::hotp(const std::optional &offset, uint tokenLength, bool checksum, bool requireSaneKeyLength) + std::optional Algorithm::hotp(const std::optional offset, uint tokenLength, bool checksum, bool requireSaneKeyLength) { const QSharedPointer encoder(new Encoder(tokenLength, checksum)); return create(QCryptographicHash::Sha1, offset, encoder, requireSaneKeyLength); } - Algorithm::Algorithm(const QSharedPointer &encoder, const std::function &truncation, QCryptographicHash::Algorithm algorithm, bool requireSaneKeyLength) : + Algorithm::Algorithm(const QSharedPointer &encoder, const std::function &truncation, QCryptographicHash::Algorithm algorithm, bool requireSaneKeyLength) : m_encoder(encoder), m_truncation(truncation), m_enforceKeyLength(requireSaneKeyLength), m_algorithm(algorithm) { } diff --git a/src/oath/oath.h b/src/oath/oath.h index 55309d6..8c8fb19 100644 --- a/src/oath/oath.h +++ b/src/oath/oath.h @@ -38,16 +38,16 @@ namespace oath { public: static bool validate(const Encoder *encoder); - static bool validate(QCryptographicHash::Algorithm algorithm, const std::optional &offset); - static std::optional create(QCryptographicHash::Algorithm algorithm, const std::optional &offset, const QSharedPointer &encoder, bool requireSaneKeyLength = false); + static bool validate(QCryptographicHash::Algorithm algorithm, const std::optional offset); + static std::optional create(QCryptographicHash::Algorithm algorithm, const std::optional offset, const QSharedPointer &encoder, bool requireSaneKeyLength = false); static std::optional totp(QCryptographicHash::Algorithm algorithm, uint tokenLength, bool requireSaneKeyLength = false); - static std::optional hotp(const std::optional &offset, uint tokenLength, bool checksum, bool requireSaneKeyLength = false); + static std::optional hotp(const std::optional offset, uint tokenLength, bool checksum, bool requireSaneKeyLength = false); std::optional compute(quint64 counter, char * secretBuffer, int length) const; private: - Algorithm(const QSharedPointer &encoder, const std::function &truncation, QCryptographicHash::Algorithm algorithm, bool requireSaneKeyLength); + Algorithm(const QSharedPointer &encoder, const std::function &truncation, QCryptographicHash::Algorithm algorithm, bool requireSaneKeyLength); private: const QSharedPointer m_encoder; - const std::function m_truncation; + const std::function m_truncation; bool m_enforceKeyLength; const QCryptographicHash::Algorithm m_algorithm; };