-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpadding.hpp
More file actions
58 lines (45 loc) · 1.4 KB
/
padding.hpp
File metadata and controls
58 lines (45 loc) · 1.4 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
48
49
50
51
52
53
54
55
56
57
58
#ifndef __PADDING_HPP__
#define __PADDING_HPP__
#include <string>
#include <iterator>
#include "byte_vector.hpp"
template <typename T, typename OutputIt>
void pkcs7(const T& obj, size_t padded_size, OutputIt it) {
assert(padded_size >= obj.size());
size_t diff = padded_size - obj.size();
for (int i = 0; i < diff; i += 1) {
*it++ = (typename T::value_type) diff;
}
}
template <typename T>
void pad_pkcs7(T& obj, size_t padded_size) {
pkcs7(obj, padded_size, std::back_inserter(obj));
}
template <typename T>
T padded_pkcs7(const T& obj, size_t padded_size) {
T res(obj);
pkcs7(res, padded_size, std::back_inserter(res));
return res;
}
int get_pkcs7_padding(const byte_vector& bytes) {
if (bytes.size() == 0) {
return 0;
}
const int size = bytes.size();
byte last_byte = bytes[size - 1];
bool is_valid = std::all_of(next(begin(bytes), size - last_byte), end(bytes),
[&] (const byte& b) { return b == last_byte; });
return is_valid ? last_byte : 0;
}
bool has_valid_pkcs7_padding(const byte_vector& bytes) {
return get_pkcs7_padding(bytes) > 0;
}
bool strip_pkcs7_padding(const byte_vector& in, byte_vector& out) {
int padding_size = get_pkcs7_padding(in);
if (padding_size < 1) {
return false;
}
out = byte_vector(begin(in), next(begin(in), in.size() - padding_size));
return true;
}
#endif//__PADDING_HPP__