keepassxc/src/core/Uuid.cpp

82 lines
1.6 KiB
C++
Raw Normal View History

2010-08-07 15:10:44 +02:00
/*
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Uuid.h"
2010-08-14 12:28:52 +02:00
#include <QtCore/QHash>
2010-09-22 00:16:01 +02:00
#include "crypto/Random.h"
2010-09-05 11:46:36 +02:00
const int Uuid::LENGTH = 16;
2010-08-24 23:12:01 +02:00
Uuid::Uuid()
2010-09-05 11:46:36 +02:00
: m_data(LENGTH, 0)
2010-08-07 15:10:44 +02:00
{
}
Uuid::Uuid(const QByteArray& data)
{
Q_ASSERT(data.size() == 16);
m_data = data;
}
Uuid Uuid::random() {
2010-09-22 00:16:01 +02:00
return Uuid(Random::randomArray(16));
}
2010-08-13 18:08:06 +02:00
QString Uuid::toBase64() const
2010-08-07 15:10:44 +02:00
{
2010-08-13 18:08:06 +02:00
return m_data.toBase64();
2010-08-07 15:10:44 +02:00
}
QByteArray Uuid::toByteArray() const
{
return m_data;
}
bool Uuid::isNull() const
{
for (int i=0; i<m_data.size(); ++i) {
if (m_data[i] != 0)
return false;
}
return true;
}
bool Uuid::operator==(const Uuid& other) const
{
return m_data == other.m_data;
}
bool Uuid::operator!=(const Uuid& other) const
{
return !operator==(other);
}
2010-08-25 21:14:41 +02:00
Uuid Uuid::fromBase64(const QString& str)
{
QByteArray data = QByteArray::fromBase64(str.toAscii());
return Uuid(data);
}
2010-08-07 15:10:44 +02:00
uint qHash(const Uuid& key)
{
return qHash(key.toByteArray());
}