diff --git a/src/libcalamares/utils/Logger.cpp b/src/libcalamares/utils/Logger.cpp index 01c3e1539..2b952cc56 100644 --- a/src/libcalamares/utils/Logger.cpp +++ b/src/libcalamares/utils/Logger.cpp @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include #include @@ -252,4 +254,34 @@ operator<<( QDebug& s, const RedactedCommand& l ) return s; } +/** @brief Returns a stable-but-private hash of @p context and @p s + * + * Identical strings with the same context will be hashed the same, + * so that they can be logged and still recognized as the-same. + */ +static uint insertRedactedName( const QString& context, const QString& s ) +{ + static uint salt = QRandomGenerator::global()->generate(); // Just once + + uint val = qHash(context, salt); + return qHash(s, val); +} + +RedactedName::RedactedName( const QString& context, const QString& s ) + : m_id( insertRedactedName(context, s) ), + m_context(context) +{ +} + +RedactedName::RedactedName(const char *context, const QString& s ) + : RedactedName( QString::fromLatin1(context), s ) +{ +} + +QDebug& +operator<< ( QDebug& s, const RedactedName& n ) +{ + return s << NoQuote << n.m_context << '$' << n.m_id << Quote; +} + } // namespace Logger diff --git a/src/libcalamares/utils/Logger.h b/src/libcalamares/utils/Logger.h index 9f0aeffea..70b29b98e 100644 --- a/src/libcalamares/utils/Logger.h +++ b/src/libcalamares/utils/Logger.h @@ -226,6 +226,23 @@ struct RedactedCommand QDebug& operator<<( QDebug& s, const RedactedCommand& l ); +/** @brief When logging "private" identifiers, keep them consistent but private + * + * Send a string to a logger in such a way that each time it is logged, + * it logs the same way, but without revealing the actual contents. + * This can be applied to user names, UUIDs, etc. + */ +struct RedactedName +{ + RedactedName( const char* context, const QString& s ); + RedactedName( const QString& context, const QString& s ); + + const uint m_id; + const QString m_context; +}; + +QDebug& operator<<( QDebug& s, const RedactedName& n ); + /** * @brief Formatted logging of a pointer *