keepassxc/src/core/Uuid.cpp

111 lines
2.1 KiB
C++
Raw Normal View History

2010-08-07 09:10:44 -04: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 06:28:52 -04:00
#include <QtCore/QHash>
2010-09-21 18:16:01 -04:00
#include "crypto/Random.h"
2012-05-14 13:10:42 -04:00
const int Uuid::Length = 16;
2010-08-24 17:12:01 -04:00
Uuid::Uuid()
2012-05-14 13:10:42 -04:00
: m_data(Length, 0)
2010-08-07 09:10:44 -04:00
{
}
Uuid::Uuid(const QByteArray& data)
{
2012-05-14 13:10:42 -04:00
Q_ASSERT(data.size() == Length);
2010-08-07 09:10:44 -04:00
m_data = data;
}
Uuid Uuid::random() {
2012-05-14 13:10:42 -04:00
return Uuid(Random::randomArray(Length));
}
2010-08-13 12:08:06 -04:00
QString Uuid::toBase64() const
2010-08-07 09:10:44 -04:00
{
2010-08-13 12:08:06 -04:00
return m_data.toBase64();
2010-08-07 09:10:44 -04:00
}
2012-05-18 08:40:52 -04:00
QString Uuid::toHex() const
{
return m_data.toHex();
}
2010-08-07 09:10:44 -04:00
QByteArray Uuid::toByteArray() const
{
return m_data;
}
bool Uuid::isNull() const
{
2012-04-18 16:08:22 -04:00
for (int i = 0; i < m_data.size(); ++i) {
2011-11-12 04:42:34 -05:00
if (m_data[i] != 0) {
2010-08-07 09:10:44 -04:00
return false;
2011-11-12 04:42:34 -05:00
}
2010-08-07 09:10:44 -04:00
}
return true;
}
Uuid& Uuid::operator=(const Uuid& other)
{
m_data = other.m_data;
return *this;
}
2010-08-07 09:10:44 -04:00
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 15:14:41 -04:00
Uuid Uuid::fromBase64(const QString& str)
{
QByteArray data = QByteArray::fromBase64(str.toAscii());
return Uuid(data);
}
2010-08-07 09:10:44 -04:00
uint qHash(const Uuid& key)
{
return qHash(key.toByteArray());
}
QDataStream& operator<<(QDataStream& stream, const Uuid& uuid)
{
return stream << uuid.toByteArray();
}
QDataStream& operator>>(QDataStream& stream, Uuid& uuid)
{
QByteArray data;
stream >> data;
2012-05-14 13:10:42 -04:00
if (data.size() == Uuid::Length) {
uuid = Uuid(data);
}
return stream;
}