Use common crypt64::encode in server

This commit is contained in:
Alexander van Gessel 2018-03-15 17:39:31 +01:00
parent b753b33239
commit 9932ed0999
No known key found for this signature in database
GPG key ID: ED28AFE007041A1F

View file

@ -15,6 +15,7 @@
#include "server/user_handler.hpp"
#include "config.hpp"
#include "random.hpp"
#include "serialization/base64.hpp"
#include <openssl/rand.h>
#include <array>
@ -52,36 +53,7 @@ std::string user_handler::create_unsecure_nonce(int length) {
return ss.str();
}
// TODO - This really should be a common function.
// This is duplicated in two or three other places.
// Some are virtual member functions.
namespace {
const std::string itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ;
template<size_t len>
std::string encode_hash(const std::array<unsigned char, len>& input) {
std::string encoded_hash;
unsigned int i = 0;
do {
unsigned value = input[i++];
encoded_hash.append(itoa64.substr(value & 0x3f,1));
if(i < len)
value |= static_cast<int>(input[i]) << 8;
encoded_hash.append(itoa64.substr((value >> 6) & 0x3f,1));
if(i++ >= len)
break;
if(i < len)
value |= static_cast<int>(input[i]) << 16;
encoded_hash.append(itoa64.substr((value >> 12) & 0x3f,1));
if(i++ >= len)
break;
encoded_hash.append(itoa64.substr((value >> 18) & 0x3f,1));
} while (i < len);
return encoded_hash;
}
class RAND_bytes_exception: public std::exception
{
};
@ -96,6 +68,6 @@ std::string user_handler::create_secure_nonce()
throw RAND_bytes_exception();
}
return encode_hash(buf);
return crypt64::encode({buf.data(), buf.size()});
}