-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryptor.cpp
More file actions
42 lines (36 loc) · 960 Bytes
/
encryptor.cpp
File metadata and controls
42 lines (36 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "encryptor.h"
#include <QCryptographicHash>
#include <iostream>
void Encryptor::Init(const QString& passoword) {
QByteArray raw = passoword.toUtf8();
hasher.reset();
hasher.addData(raw);
data_ = hasher.result();
Reset();
}
void Encryptor::Reset() {
current_ = 0;
}
QString Encryptor::Read(QDataStream& input) {
QByteArray encoded;
input >> encoded;
for (auto& byte : encoded) {
byte ^= GetNextByte();
}
return QString::fromUtf8(encoded);
}
void Encryptor::Write(QDataStream& output, const QString& string) {
QByteArray encoded = string.toUtf8();
for (auto& byte : encoded) {
byte ^= GetNextByte();
}
output << encoded;
}
uint8_t Encryptor::GetNextByte() {
if (current_ == data_.length()) {
hasher.reset();
hasher.addData(data_);
data_.append(hasher.result());
}
return data_[current_];
}