-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleStreamCipher.h
More file actions
47 lines (41 loc) · 1.12 KB
/
SimpleStreamCipher.h
File metadata and controls
47 lines (41 loc) · 1.12 KB
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
43
44
45
46
47
#pragma once
#include <QByteArray>
#include <QCryptographicHash>
#include <cstdint>
class SimpleStreamCipher {
public:
SimpleStreamCipher(const QByteArray& key,
const QByteArray& nonce)
: m_key(key),
m_nonce(nonce),
m_counter(0),
m_streamOffset(0)
{}
void process(QByteArray& data) {
for (int i = 0; i < data.size(); ++i) {
if (m_streamOffset == m_stream.size()) {
m_stream = generateKeystreamBlock();
m_streamOffset = 0;
}
data[i] ^= m_stream[m_streamOffset++];
}
}
private:
QByteArray m_key;
QByteArray m_nonce;
uint64_t m_counter;
QByteArray m_stream;
int m_streamOffset;
QByteArray generateKeystreamBlock() {
QByteArray input;
input.append(m_key);
input.append(m_nonce);
input.append(reinterpret_cast<const char*>(&m_counter),
sizeof(m_counter));
m_counter++;
return QCryptographicHash::hash(
input,
QCryptographicHash::Sha256
);
}
};