This repository has been archived by the owner on Sep 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Util.cpp
82 lines (73 loc) · 2.78 KB
/
Util.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "pch.h"
#include "ShowStart.h"
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Security::Cryptography;
using namespace Windows::Security::Cryptography::Core;
using namespace Windows::Data::Json;
namespace util {
hstring strip(hstring const& data) noexcept {
std::wstring str{ data };
auto pred{ [ ](wchar_t ch) { return ch != L' ' && ch != L'\t' && ch != L'\n'; } };
str.erase(str.begin(), std::find_if(str.begin(), str.end(), pred));
str.erase(std::find_if(str.rbegin(), str.rend(), pred).base(), str.end());
return str.data();
}
hstring replace_br(hstring const& data) noexcept {
std::wstring str{ data };
long long oldPos{ }, start{ };
while ((start = str.find(L"<br/>", oldPos)) != std::wstring::npos) {
str.replace(start, 5, L"\n");
oldPos = start + 1;
}
return str.data();
}
hstring timestamp13() noexcept {
using namespace std::chrono;
return to_hstring(duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count());
}
hstring uuid32() noexcept {
std::srand(static_cast<unsigned int>(std::chrono::system_clock::now().time_since_epoch().count()));
hstring n{ L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + timestamp13() };
wchar_t buf[33]{ };
for (auto& i : buf) {
i = n[std::rand() % 75];
}
buf[32] = 0;
return buf;
}
winrt::Windows::Foundation::Collections::IVector<winrt::hstring> split_string(winrt::hstring const& data) noexcept
{
std::wstringstream ss(data.data());
auto container{ winrt::single_threaded_vector<winrt::hstring>() };
std::wstring str;
while (ss >> str)
{
container.Append(str);
}
return container;
}
JsonObject map_to_json(std::unordered_map<hstring, IJsonValue> const& map) noexcept {
JsonObject json;
for (auto& [key, value] : map) {
json.Insert(key, value);
}
return json;
}
hstring get_md5(hstring const& data) noexcept {
auto provider{ HashAlgorithmProvider::OpenAlgorithm(HashAlgorithmNames::Md5()) };
auto hasher{ provider.CreateHash() };
auto bin{ CryptographicBuffer::ConvertStringToBinary(data, BinaryStringEncoding::Utf8) };
hasher.Append(bin);
auto md5{ hasher.GetValueAndReset() };
return CryptographicBuffer::EncodeToHexString(md5);
}
hstring get_AES_base64(hstring const& data, hstring const& key) noexcept {
auto provider{ SymmetricKeyAlgorithmProvider::OpenAlgorithm(SymmetricAlgorithmNames::AesEcbPkcs7()) };
auto key_bin{ CryptographicBuffer::ConvertStringToBinary(key, BinaryStringEncoding::Utf8) };
auto symmetricKey{ provider.CreateSymmetricKey(key_bin) };
auto data_bin{ CryptographicBuffer::ConvertStringToBinary(data, BinaryStringEncoding::Utf8) };
auto aes{ CryptographicEngine::Encrypt(symmetricKey, data_bin, nullptr) };
return CryptographicBuffer::EncodeToBase64String(aes);
}
}