-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpassword.cpp
42 lines (30 loc) · 1.21 KB
/
password.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
#include "password.h"
#include "random.h"
#include "base64.h"
#include "debug.h"
#include <codecvt>
#include <locale>
#include <array>
#ifdef DEBUG
#include <iostream>
#endif
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter;
std::string Password::generate(const std::u16string& password)
{
std::string password_u8 = converter.to_bytes(password);
return this->generate(password_u8);
}
std::string Password::generate(const std::string& password)
{
const std::array<uint32_t, 4> salt{get_rand(), get_rand(), get_rand(), get_rand()};
std::array<unsigned char, 64> outBuf {};
const int hmacResult = PKCS5_PBKDF2_HMAC(password.c_str(), password.size()
, reinterpret_cast<const unsigned char *>(salt.data()), static_cast<int>(sizeof(salt[0]) * salt.size())
, this->hashIterations, this->hashMethod
, static_cast<int>(outBuf.size()), outBuf.data());
if (hmacResult != 1)
return {};
std::string ret = base64_encode(reinterpret_cast<unsigned char const*>(salt.data()), sizeof(salt[0]) * salt.size())
+ ":" + base64_encode(reinterpret_cast<unsigned char const*>(outBuf.data()), sizeof(outBuf[0]) * outBuf.size());
return ret;
}