Review fixes

This commit is contained in:
Weslly 2017-05-03 20:54:19 -03:00
parent bf57a28654
commit 4c9b8c7794
9 changed files with 117 additions and 62 deletions

View File

@ -131,6 +131,8 @@ set(keepassx_SOURCES
streams/qtiocompressor.cpp
streams/StoreDataStream.cpp
streams/SymmetricCipherStream.cpp
totp/base32.h
totp/base32.cpp
totp/totp.h
totp/totp.cpp
)

View File

@ -329,7 +329,7 @@ QString Entry::totpSeed() const
if (m_attributes->hasKey("otp")) {
secret = m_attributes->value("otp");
} else {
} else if (m_attributes->hasKey("TOTP Seed")) {
secret = m_attributes->value("TOTP Seed");
}

View File

@ -41,7 +41,7 @@ TotpDialog::TotpDialog(DatabaseWidget* parent, Entry* entry)
uCounter = resetCounter();
updateProgressBar();
QTimer *timer = new QTimer(this);
QTimer* timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateProgressBar()));
connect(timer, SIGNAL(timeout()), this, SLOT(updateSeconds()));
timer->start(m_step * 10);

68
src/totp/base32.cpp Normal file
View File

@ -0,0 +1,68 @@
// Base32 implementation
//
// Copyright 2010 Google Inc.
// Author: Markus Gutschke
// Source: https://github.com/google/google-authenticator-libpam/blob/master/src/base32.c
// Modifications copyright (C) 2017 KeePassXC team <https://keepassxc.org/>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "base32.h"
Base32::Base32()
{
}
QByteArray Base32::base32_decode(const QByteArray encoded)
{
QByteArray result;
int buffer = 0;
int bitsLeft = 0;
for (char ch : encoded) {
if (ch == 0 || ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '-' || ch == '=') {
continue;
}
buffer <<= 5;
// Deal with commonly mistyped characters
if (ch == '0') {
ch = 'O';
} else if (ch == '1') {
ch = 'L';
} else if (ch == '8') {
ch = 'B';
}
// Look up one base32 digit
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
ch = (ch & 0x1F) - 1;
} else if (ch >= '2' && ch <= '7') {
ch -= '2' - 26;
} else {
return QByteArray();
}
buffer |= ch;
bitsLeft += 5;
if (bitsLeft >= 8) {
result.append(static_cast<char> (buffer >> (bitsLeft - 8)));
bitsLeft -= 8;
}
}
return result;
}

34
src/totp/base32.h Normal file
View File

@ -0,0 +1,34 @@
// Base32 implementation
//
// Copyright 2010 Google Inc.
// Author: Markus Gutschke
// Source: https://github.com/google/google-authenticator-libpam/blob/master/src/base32.h
// Modifications copyright (C) 2017 KeePassXC team <https://keepassxc.org/>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef BASE32_H
#define BASE32_H
#include <QtCore/qglobal.h>
#include <QByteArray>
class Base32
{
public:
Base32();
static QByteArray base32_decode(const QByteArray encoded);
};
#endif //BASE32_H

View File

@ -16,6 +16,7 @@
*/
#include "totp.h"
#include "base32.h"
#include <cmath>
#include <QtEndian>
#include <QRegExp>
@ -91,62 +92,12 @@ QString QTotp::parseOtpString(QString key, quint8 &digits, quint8 &step)
return seed;
}
QByteArray QTotp::base32_decode(const QByteArray encoded)
{
// Base32 implementation
// Copyright 2010 Google Inc.
// Author: Markus Gutschke
// Licensed under the Apache License, Version 2.0
QByteArray result;
int buffer = 0;
int bitsLeft = 0;
for (char ch : encoded) {
if (ch == 0 || ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '-' || ch == '=') {
continue;
}
buffer <<= 5;
// Deal with commonly mistyped characters
if (ch == '0') {
ch = 'O';
} else if (ch == '1') {
ch = 'L';
} else if (ch == '8') {
ch = 'B';
}
// Look up one base32 digit
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
ch = (ch & 0x1F) - 1;
} else if (ch >= '2' && ch <= '7') {
ch -= '2' - 26;
} else {
return QByteArray();
}
buffer |= ch;
bitsLeft += 5;
if (bitsLeft >= 8) {
result.append(static_cast<char> (buffer >> (bitsLeft - 8)));
bitsLeft -= 8;
}
}
return result;
}
QString QTotp::generateTotp(const QByteArray key, quint64 time, const quint8 numDigits = defaultDigits, const quint8 step = defaultStep)
QString QTotp::generateTotp(const QByteArray key, quint64 time,
const quint8 numDigits = defaultDigits, const quint8 step = defaultStep)
{
quint64 current = qToBigEndian(time / step);
QByteArray secret = QTotp::base32_decode(key);
QByteArray secret = Base32::base32_decode(key);
if (secret.isEmpty()) {
return "Invalid TOTP secret key";
}

View File

@ -25,7 +25,6 @@ class QTotp
public:
QTotp();
static QString parseOtpString(QString rawSecret, quint8 &digits, quint8 &step);
static QByteArray base32_decode(const QByteArray encoded);
static QString generateTotp(const QByteArray key, quint64 time, const quint8 numDigits, const quint8 step);
static const quint8 defaultStep;
static const quint8 defaultDigits;

View File

@ -25,6 +25,7 @@
#include "crypto/Crypto.h"
#include "totp/totp.h"
#include "totp/base32.h"
QTEST_GUILESS_MAIN(TestTotp)
@ -34,7 +35,7 @@ void TestTotp::initTestCase()
}
void TestTotp::testSecret()
void TestTotp::testParseSecret()
{
quint8 digits = 0;
quint8 step = 0;
@ -61,19 +62,19 @@ void TestTotp::testSecret()
void TestTotp::testBase32()
{
QByteArray key = QString("JBSW Y3DP EB3W 64TM MQXC 4LQA").toLatin1();
QByteArray secret = QTotp::base32_decode(key);
QByteArray secret = Base32::base32_decode(key);
QCOMPARE(QString::fromLatin1(secret), QString("Hello world..."));
key = QString("gezdgnbvgy3tqojqgezdgnbvgy3tqojq").toLatin1();
secret = QTotp::base32_decode(key);
secret = Base32::base32_decode(key);
QCOMPARE(QString::fromLatin1(secret), QString("12345678901234567890"));
key = QString("ORSXG5A=").toLatin1();
secret = QTotp::base32_decode(key);
secret = Base32::base32_decode(key);
QCOMPARE(QString::fromLatin1(secret), QString("test"));
key = QString("MZXW6YTBOI======").toLatin1();
secret = QTotp::base32_decode(key);
secret = Base32::base32_decode(key);
QCOMPARE(QString::fromLatin1(secret), QString("foobar"));
}

View File

@ -28,7 +28,7 @@ class TestTotp : public QObject
private slots:
void initTestCase();
void testSecret();
void testParseSecret();
void testBase32();
void testTotpCode();
};