Heart
1.4.918.d927d9c
Heart is base back end library for your c++ Qt projects.
Toggle main menu visibility
Loading...
Searching...
No Matches
accesstoken.cpp
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2018-2025 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
12
namespace
QH
{
13
14
QByteArray 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
27
AccessToken::AccessToken
(
int
duration,
const
QByteArray &entropy) {
28
_duration =
static_cast<
int
>
(QDateTime::currentSecsSinceEpoch()) + duration;
29
_data = generate(entropy);
30
}
31
32
AccessToken::AccessToken
(
const
QByteArray &other) {
33
fromBytes
(other);
34
}
35
36
AccessToken::AccessToken
() =
default
;
37
38
AccessToken::AccessToken
(
const
AccessToken
&other) =
default
;
39
40
bool
AccessToken::isValid
()
const
{
41
return
(_duration > QDateTime::currentSecsSinceEpoch()) &&
42
(_data.size() == 32);
43
}
44
45
void
AccessToken::clear
()
46
{
47
_data.clear();
48
_duration = 0;
49
}
50
51
bool
AccessToken::operator ==
(
const
AccessToken
&other)
const
{
52
return
_data == other._data;
53
}
54
55
bool
AccessToken::operator !=
(
const
AccessToken
&other)
const
{
56
return
!
operator==
(other);
57
}
58
59
AccessToken
&
AccessToken::operator =
(
const
AccessToken
&other) =
default
;
60
61
QDataStream &
AccessToken::fromStream
(QDataStream &stream) {
62
63
stream >> _data;
64
stream >> _duration;
65
return
stream;
66
}
67
68
QDataStream &
AccessToken::toStream
(QDataStream &stream)
const
{
69
stream << _data;
70
stream << _duration;
71
return
stream;
72
}
73
74
}
75
accesstoken.h
QH::AccessToken
The AccessToken class contains information duration of the access. Token are byte array for validatio...
Definition
accesstoken.h:25
QH::AccessToken::toStream
QDataStream & toStream(QDataStream &stream) const
fromStream This method should be write all members of the current object to the stream object.
Definition
accesstoken.cpp:68
QH::AccessToken::fromStream
QDataStream & fromStream(QDataStream &stream)
fromStream This method should be read all bytes from the stream object and full the current object.
Definition
accesstoken.cpp:61
QH::AccessToken::operator==
bool operator==(const AccessToken &other) const
operator == compare left and right values with type AccessToken.
Definition
accesstoken.cpp:51
QH::AccessToken::operator!=
bool operator!=(const AccessToken &other) const
operator != compare left and right values with type AccessToken.
Definition
accesstoken.cpp:55
QH::AccessToken::AccessToken
AccessToken(int duration, const QByteArray &entropy="")
AccessToken This constructor create token on duration (in seconds).
Definition
accesstoken.cpp:27
QH::AccessToken::operator=
AccessToken & operator=(const AccessToken &other)
operator = this is operator of copy objects.
QH::AccessToken::isValid
bool isValid() const
isValid This method check this toke to valid.
Definition
accesstoken.cpp:40
QH::AccessToken::clear
void clear()
clearThis Method reset all data of token.
Definition
accesstoken.cpp:45
QH::AccessToken::AccessToken
AccessToken()
AccessToken constructor by default create not valid token.
QH::StreamBase::fromBytes
bool fromBytes(const QByteArray &dataz)
fromBytes This method provide initialization of object from byte array.
Definition
streambase.cpp:21
QH
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition
heart.cpp:13
src
public
accesstoken.cpp
Generated by
1.18.0