Heart 1.3.842.34c2ab5
Heart is base back end library for your c++ Qt projects.
accesstoken.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-2024 QuasarApp.
3 * Distributed under the lgplv3 software license, see the accompanying
4 * Everyone is permitted to copy and distribute verbatim copies
5 * of this license document, but changing it is not allowed.
6*/
7
8#include "accesstoken.h"
9#include <QCryptographicHash>
10#include <QDataStream>
11
12namespace QH {
13
14QByteArray AccessToken::generate(const QByteArray &entropy) {
15
16 QByteArray result = QCryptographicHash::hash(entropy, QCryptographicHash::Sha256);
17
18 srand(static_cast<unsigned int>(time(nullptr)));
19 for (int i = 0; i < 256; ++i) {
20 char byte = static_cast<char>(rand() % 256);
21 result.push_back(byte);
22 }
23
24 return QCryptographicHash::hash(result, QCryptographicHash::Sha256);
25}
26
27AccessToken::AccessToken(int duration, const QByteArray &entropy) {
28 _duration = static_cast<int>(QDateTime::currentSecsSinceEpoch()) + duration;
29 _data = generate(entropy);
30}
31
32AccessToken::AccessToken(const QByteArray &other) {
33 fromBytes(other);
34}
35
36AccessToken::AccessToken() = default;
37
38AccessToken::AccessToken(const AccessToken &other) = default;
39
41 return (_duration > QDateTime::currentSecsSinceEpoch()) &&
42 (_data.size() == 32);
43}
44
46{
47 _data.clear();
48 _duration = 0;
49}
50
51bool AccessToken::operator ==(const AccessToken &other) const {
52 return _data == other._data;
53}
54
55bool AccessToken::operator !=(const AccessToken &other) const {
56 return !operator==(other);
57}
58
59AccessToken& AccessToken::operator =(const AccessToken &other) = default;
60
61QDataStream &AccessToken::fromStream(QDataStream &stream) {
62
63 stream >> _data;
64 stream >> _duration;
65 return stream;
66}
67
68QDataStream &AccessToken::toStream(QDataStream &stream) const {
69 stream << _data;
70 stream << _duration;
71 return stream;
72}
73
74}
75
The AccessToken class contains information duration of the access. Token are byte array for validatio...
Definition accesstoken.h:25
QDataStream & toStream(QDataStream &stream) const
fromStream This method should be write all members of the current object to the stream object.
QDataStream & fromStream(QDataStream &stream)
fromStream This method should be read all bytes from the stream object and full the current object.
bool operator==(const AccessToken &other) const
operator == compare left and right values with type AccessToken.
bool operator!=(const AccessToken &other) const
operator != compare left and right values with type AccessToken.
AccessToken & operator=(const AccessToken &other)
operator = this is operator of copy objects.
bool isValid() const
isValid This method check this toke to valid.
void clear()
clearThis Method reset all data of token.
AccessToken()
AccessToken constructor by default create not valid token.
bool fromBytes(const QByteArray &data)
fromBytes This method provide initialization of object from byte array.
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13