30 lines
1.3 KiB
C++
30 lines
1.3 KiB
C++
#include <sodium.h>
|
|||
|
|
#include <fstream>
|
||
|
|
#include <string>
|
||
|
|
#include <array>
|
||
|
|
|
||
|
|
static std::string base64(const unsigned char *data, size_t length) {
|
||
|
|
std::array<char, 128> output{};
|
||
|
|
sodium_bin2base64(output.data(), output.size(), data, length, sodium_base64_VARIANT_ORIGINAL_NO_PADDING);
|
||
|
|
return output.data();
|
||
|
|
}
|
||
|
|
|
||
|
|
int main(int argc, char **argv) {
|
||
|
|
if (argc != 2 || sodium_init() < 0) return 1;
|
||
|
|
std::ofstream output(argv[1]);
|
||
|
|
output << "{\"hashes\":[";
|
||
|
|
const std::array<std::string, 3> passwords{"voicecat test", "caf\xc3\xa9", std::string("a\0b", 3)};
|
||
|
|
std::array<unsigned char, 16> salt{};
|
||
|
|
for (size_t i = 0; i < salt.size(); ++i) salt[i] = static_cast<unsigned char>(i);
|
||
|
|
for (size_t i = 0; i < passwords.size(); ++i) {
|
||
|
|
std::array<unsigned char, 32> hash{};
|
||
|
|
if (crypto_pwhash(hash.data(), hash.size(), passwords[i].data(), passwords[i].size(), salt.data(), 2,
|
||
|
|
64 * 1024 * 1024, crypto_pwhash_ALG_ARGON2ID13) != 0) return 1;
|
||
|
|
if (i) output << ',';
|
||
|
|
output << "{\"passwordBase64\":\"" << base64(reinterpret_cast<const unsigned char *>(passwords[i].data()), passwords[i].size())
|
||
|
|
<< "\",\"hash\":\"$argon2id$v=19$m=65536,t=2,p=1$" << base64(salt.data(), salt.size()) << '$' << base64(hash.data(), hash.size()) << "\"}";
|
||
|
|
}
|
||
|
|
output << "]}\n";
|
||
|
|
return output ? 0 : 1;
|
||
|
|
}
|