Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.
nntpchan/contrib/backends/nntpchan-daemon/tools/authtool.cpp

101 lines
2.4 KiB
C++
Raw Normal View History

2018-05-04 17:38:34 +05:00
#include <nntpchan/base64.hpp>
#include <nntpchan/crypto.hpp>
2016-10-15 21:37:59 +05:00
2016-10-15 22:53:35 +05:00
#include <cassert>
#include <cstring>
2016-10-15 21:37:59 +05:00
#include <iostream>
2016-10-15 22:53:35 +05:00
#include <sodium.h>
2017-10-17 19:29:56 +05:00
#include <string>
2016-10-15 21:37:59 +05:00
2017-10-17 19:29:56 +05:00
static void print_help(const std::string &exename)
2016-10-15 21:37:59 +05:00
{
2017-05-03 20:37:09 +05:00
std::cout << "usage: " << exename << " [help|gen|check]" << std::endl;
2016-10-15 22:53:35 +05:00
}
2017-10-17 19:29:56 +05:00
static void gen_passwd(const std::string &username, const std::string &passwd)
2016-10-15 22:53:35 +05:00
{
std::array<uint8_t, 8> random;
randombytes_buf(random.data(), random.size());
std::string salt = nntpchan::B64Encode(random.data(), random.size());
std::string cred = passwd + salt;
nntpchan::SHA512Digest d;
nntpchan::SHA512((const uint8_t *)cred.c_str(), cred.size(), d);
std::string hash = nntpchan::B64Encode(d.data(), d.size());
std::cout << username << ":" << hash << ":" << salt << std::endl;
}
2017-10-17 19:29:56 +05:00
static bool check_cred(const std::string &cred, const std::string &passwd)
2016-10-15 22:53:35 +05:00
{
auto idx = cred.find(":");
2017-10-17 19:29:56 +05:00
if (idx == std::string::npos || idx == 0)
return false;
std::string part = cred.substr(idx + 1);
2016-10-15 22:53:35 +05:00
idx = part.find(":");
2017-10-17 19:29:56 +05:00
if (idx == std::string::npos || idx == 0)
return false;
std::string salt = part.substr(idx + 1);
2016-10-15 22:53:35 +05:00
std::string hash = part.substr(0, idx);
std::vector<uint8_t> h;
2017-10-17 19:29:56 +05:00
if (!nntpchan::B64Decode(hash, h))
return false;
2016-10-15 22:53:35 +05:00
nntpchan::SHA512Digest d;
std::string l = passwd + salt;
2017-10-17 19:29:56 +05:00
nntpchan::SHA512((const uint8_t *)l.data(), l.size(), d);
2016-10-15 22:53:35 +05:00
return std::memcmp(h.data(), d.data(), d.size()) == 0;
2016-10-15 21:37:59 +05:00
}
2017-10-17 19:29:56 +05:00
int main(int argc, char *argv[])
2016-10-15 21:37:59 +05:00
{
2016-10-15 22:53:35 +05:00
assert(sodium_init() == 0);
2017-10-17 19:29:56 +05:00
if (argc == 1)
{
2016-10-15 21:37:59 +05:00
print_help(argv[0]);
2018-05-04 17:38:34 +05:00
return 0;
2016-10-15 21:37:59 +05:00
}
2016-10-15 22:53:35 +05:00
std::string cmd(argv[1]);
2017-10-17 19:29:56 +05:00
if (cmd == "help")
{
2018-05-04 17:38:34 +05:00
print_help(argv[0]);
2016-10-15 22:53:35 +05:00
return 0;
}
2017-10-17 19:29:56 +05:00
if (cmd == "gen")
{
if (argc == 4)
{
2016-10-15 22:53:35 +05:00
gen_passwd(argv[2], argv[3]);
return 0;
2017-10-17 19:29:56 +05:00
}
else
{
2017-05-03 20:37:09 +05:00
std::cout << "usage: " << argv[0] << " gen username password" << std::endl;
2018-05-04 17:38:34 +05:00
return 0;
2016-10-15 22:53:35 +05:00
}
}
2017-10-17 19:29:56 +05:00
if (cmd == "check")
{
2016-10-15 22:53:35 +05:00
std::string cred;
2017-10-17 19:29:56 +05:00
std::cout << "credential: ";
if (!std::getline(std::cin, cred))
{
2018-05-04 17:38:34 +05:00
std::cout << "read error" << std::endl;
2016-10-15 22:53:35 +05:00
return 1;
}
std::string passwd;
std::cout << "password: ";
2017-10-17 19:29:56 +05:00
if (!std::getline(std::cin, passwd))
{
2018-05-04 17:38:34 +05:00
std::cout << "read error" << std::endl;
2016-10-15 22:53:35 +05:00
return 1;
}
2017-10-17 19:29:56 +05:00
if (check_cred(cred, passwd))
{
2016-10-15 22:53:35 +05:00
std::cout << "okay" << std::endl;
return 0;
}
std::cout << "bad login" << std::endl;
return 1;
}
print_help(argv[0]);
return 1;
2016-10-15 21:37:59 +05:00
}