Updated crypto.hpp: deprecated functions replaced
This commit is contained in:
parent
bdb105712b
commit
8598036b73
1 changed files with 66 additions and 93 deletions
159
crypto.hpp
159
crypto.hpp
|
|
@ -32,7 +32,7 @@ namespace SimpleWeb {
|
||||||
std::string base64;
|
std::string base64;
|
||||||
|
|
||||||
BIO *bio, *b64;
|
BIO *bio, *b64;
|
||||||
BUF_MEM *bptr = BUF_MEM_new();
|
auto bptr = BUF_MEM_new();
|
||||||
|
|
||||||
b64 = BIO_new(BIO_f_base64());
|
b64 = BIO_new(BIO_f_base64());
|
||||||
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
|
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
|
||||||
|
|
@ -62,10 +62,8 @@ namespace SimpleWeb {
|
||||||
|
|
||||||
/// Returns Base64 decoded string from base64 input.
|
/// Returns Base64 decoded string from base64 input.
|
||||||
static std::string decode(const std::string &base64) noexcept {
|
static std::string decode(const std::string &base64) noexcept {
|
||||||
std::string ascii;
|
std::string ascii((6 * base64.size()) / 8, '\0'); // The size is a up to two bytes too large.
|
||||||
|
|
||||||
// Resize ascii, however, the size is a up to two bytes too large.
|
|
||||||
ascii.resize((6 * base64.size()) / 8);
|
|
||||||
BIO *b64, *bio;
|
BIO *b64, *bio;
|
||||||
|
|
||||||
b64 = BIO_new(BIO_f_base64());
|
b64 = BIO_new(BIO_f_base64());
|
||||||
|
|
@ -99,133 +97,109 @@ namespace SimpleWeb {
|
||||||
return hex_stream.str();
|
return hex_stream.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return hash value using specific EVP_MD from input string.
|
||||||
|
static std::string message_digest(const std::string &str, const EVP_MD *evp_md, std::size_t digest_length) noexcept {
|
||||||
|
std::string md(digest_length, '\0');
|
||||||
|
|
||||||
|
auto ctx = EVP_MD_CTX_create();
|
||||||
|
EVP_MD_CTX_init(ctx);
|
||||||
|
EVP_DigestInit_ex(ctx, evp_md, nullptr);
|
||||||
|
EVP_DigestUpdate(ctx, str.data(), str.size());
|
||||||
|
EVP_DigestFinal_ex(ctx, reinterpret_cast<unsigned char *>(&md[0]), nullptr);
|
||||||
|
EVP_MD_CTX_destroy(ctx);
|
||||||
|
|
||||||
|
return md;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return hash value using specific EVP_MD from input stream.
|
||||||
|
static std::string stream_digest(std::istream &stream, const EVP_MD *evp_md, std::size_t digest_length) noexcept {
|
||||||
|
std::string md(digest_length, '\0');
|
||||||
|
std::unique_ptr<char[]> buffer(new char[buffer_size]);
|
||||||
|
std::streamsize read_length;
|
||||||
|
|
||||||
|
auto ctx = EVP_MD_CTX_create();
|
||||||
|
EVP_MD_CTX_init(ctx);
|
||||||
|
EVP_DigestInit_ex(ctx, evp_md, nullptr);
|
||||||
|
while((read_length = stream.read(buffer.get(), buffer_size).gcount()) > 0)
|
||||||
|
EVP_DigestUpdate(ctx, buffer.get(), static_cast<std::size_t>(read_length));
|
||||||
|
EVP_DigestFinal_ex(ctx, reinterpret_cast<unsigned char *>(&md[0]), nullptr);
|
||||||
|
EVP_MD_CTX_destroy(ctx);
|
||||||
|
|
||||||
|
return md;
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns md5 hash value from input string.
|
/// Returns md5 hash value from input string.
|
||||||
static std::string md5(const std::string &input, std::size_t iterations = 1) noexcept {
|
static std::string md5(const std::string &input, std::size_t iterations = 1) noexcept {
|
||||||
std::string hash;
|
auto evp_md = EVP_md5();
|
||||||
|
auto hash = message_digest(input, evp_md, MD5_DIGEST_LENGTH);
|
||||||
hash.resize(128 / 8);
|
for(std::size_t i = 1; i < iterations; ++i)
|
||||||
MD5(reinterpret_cast<const unsigned char *>(&input[0]), input.size(), reinterpret_cast<unsigned char *>(&hash[0]));
|
hash = message_digest(hash, evp_md, MD5_DIGEST_LENGTH);
|
||||||
|
|
||||||
for(std::size_t c = 1; c < iterations; ++c)
|
|
||||||
MD5(reinterpret_cast<const unsigned char *>(&hash[0]), hash.size(), reinterpret_cast<unsigned char *>(&hash[0]));
|
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns md5 hash value from input stream.
|
/// Returns md5 hash value from input stream.
|
||||||
static std::string md5(std::istream &stream, std::size_t iterations = 1) noexcept {
|
static std::string md5(std::istream &stream, std::size_t iterations = 1) noexcept {
|
||||||
MD5_CTX context;
|
auto evp_md = EVP_md5();
|
||||||
MD5_Init(&context);
|
auto hash = stream_digest(stream, evp_md, MD5_DIGEST_LENGTH);
|
||||||
std::streamsize read_length;
|
for(std::size_t i = 1; i < iterations; ++i)
|
||||||
std::vector<char> buffer(buffer_size);
|
hash = message_digest(hash, evp_md, MD5_DIGEST_LENGTH);
|
||||||
while((read_length = stream.read(&buffer[0], buffer_size).gcount()) > 0)
|
|
||||||
MD5_Update(&context, buffer.data(), static_cast<std::size_t>(read_length));
|
|
||||||
std::string hash;
|
|
||||||
hash.resize(128 / 8);
|
|
||||||
MD5_Final(reinterpret_cast<unsigned char *>(&hash[0]), &context);
|
|
||||||
|
|
||||||
for(std::size_t c = 1; c < iterations; ++c)
|
|
||||||
MD5(reinterpret_cast<const unsigned char *>(&hash[0]), hash.size(), reinterpret_cast<unsigned char *>(&hash[0]));
|
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns sha1 hash value from input string.
|
/// Returns sha1 hash value from input string.
|
||||||
static std::string sha1(const std::string &input, std::size_t iterations = 1) noexcept {
|
static std::string sha1(const std::string &input, std::size_t iterations = 1) noexcept {
|
||||||
std::string hash;
|
auto evp_md = EVP_sha1();
|
||||||
|
auto hash = message_digest(input, evp_md, SHA_DIGEST_LENGTH);
|
||||||
hash.resize(160 / 8);
|
for(std::size_t i = 1; i < iterations; ++i)
|
||||||
SHA1(reinterpret_cast<const unsigned char *>(&input[0]), input.size(), reinterpret_cast<unsigned char *>(&hash[0]));
|
hash = message_digest(hash, evp_md, SHA_DIGEST_LENGTH);
|
||||||
|
|
||||||
for(std::size_t c = 1; c < iterations; ++c)
|
|
||||||
SHA1(reinterpret_cast<const unsigned char *>(&hash[0]), hash.size(), reinterpret_cast<unsigned char *>(&hash[0]));
|
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns sha1 hash value from input stream.
|
/// Returns sha1 hash value from input stream.
|
||||||
static std::string sha1(std::istream &stream, std::size_t iterations = 1) noexcept {
|
static std::string sha1(std::istream &stream, std::size_t iterations = 1) noexcept {
|
||||||
SHA_CTX context;
|
auto evp_md = EVP_sha1();
|
||||||
SHA1_Init(&context);
|
auto hash = stream_digest(stream, evp_md, SHA_DIGEST_LENGTH);
|
||||||
std::streamsize read_length;
|
for(std::size_t i = 1; i < iterations; ++i)
|
||||||
std::vector<char> buffer(buffer_size);
|
hash = message_digest(hash, evp_md, SHA_DIGEST_LENGTH);
|
||||||
while((read_length = stream.read(&buffer[0], buffer_size).gcount()) > 0)
|
|
||||||
SHA1_Update(&context, buffer.data(), static_cast<std::size_t>(read_length));
|
|
||||||
std::string hash;
|
|
||||||
hash.resize(160 / 8);
|
|
||||||
SHA1_Final(reinterpret_cast<unsigned char *>(&hash[0]), &context);
|
|
||||||
|
|
||||||
for(std::size_t c = 1; c < iterations; ++c)
|
|
||||||
SHA1(reinterpret_cast<const unsigned char *>(&hash[0]), hash.size(), reinterpret_cast<unsigned char *>(&hash[0]));
|
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns sha256 hash value from input string.
|
/// Returns sha256 hash value from input string.
|
||||||
static std::string sha256(const std::string &input, std::size_t iterations = 1) noexcept {
|
static std::string sha256(const std::string &input, std::size_t iterations = 1) noexcept {
|
||||||
std::string hash;
|
auto evp_md = EVP_sha256();
|
||||||
|
auto hash = message_digest(input, evp_md, SHA256_DIGEST_LENGTH);
|
||||||
hash.resize(256 / 8);
|
for(std::size_t i = 1; i < iterations; ++i)
|
||||||
SHA256(reinterpret_cast<const unsigned char *>(&input[0]), input.size(), reinterpret_cast<unsigned char *>(&hash[0]));
|
hash = message_digest(hash, evp_md, SHA256_DIGEST_LENGTH);
|
||||||
|
|
||||||
for(std::size_t c = 1; c < iterations; ++c)
|
|
||||||
SHA256(reinterpret_cast<const unsigned char *>(&hash[0]), hash.size(), reinterpret_cast<unsigned char *>(&hash[0]));
|
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns sha256 hash value from input stream.
|
/// Returns sha256 hash value from input stream.
|
||||||
static std::string sha256(std::istream &stream, std::size_t iterations = 1) noexcept {
|
static std::string sha256(std::istream &stream, std::size_t iterations = 1) noexcept {
|
||||||
SHA256_CTX context;
|
auto evp_md = EVP_sha256();
|
||||||
SHA256_Init(&context);
|
auto hash = stream_digest(stream, evp_md, SHA256_DIGEST_LENGTH);
|
||||||
std::streamsize read_length;
|
for(std::size_t i = 1; i < iterations; ++i)
|
||||||
std::vector<char> buffer(buffer_size);
|
hash = message_digest(hash, evp_md, SHA256_DIGEST_LENGTH);
|
||||||
while((read_length = stream.read(&buffer[0], buffer_size).gcount()) > 0)
|
|
||||||
SHA256_Update(&context, buffer.data(), static_cast<std::size_t>(read_length));
|
|
||||||
std::string hash;
|
|
||||||
hash.resize(256 / 8);
|
|
||||||
SHA256_Final(reinterpret_cast<unsigned char *>(&hash[0]), &context);
|
|
||||||
|
|
||||||
for(std::size_t c = 1; c < iterations; ++c)
|
|
||||||
SHA256(reinterpret_cast<const unsigned char *>(&hash[0]), hash.size(), reinterpret_cast<unsigned char *>(&hash[0]));
|
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns sha512 hash value from input string.
|
/// Returns sha512 hash value from input string.
|
||||||
static std::string sha512(const std::string &input, std::size_t iterations = 1) noexcept {
|
static std::string sha512(const std::string &input, std::size_t iterations = 1) noexcept {
|
||||||
std::string hash;
|
auto evp_md = EVP_sha512();
|
||||||
|
auto hash = message_digest(input, evp_md, SHA512_DIGEST_LENGTH);
|
||||||
hash.resize(512 / 8);
|
for(std::size_t i = 1; i < iterations; ++i)
|
||||||
SHA512(reinterpret_cast<const unsigned char *>(&input[0]), input.size(), reinterpret_cast<unsigned char *>(&hash[0]));
|
hash = message_digest(hash, evp_md, SHA512_DIGEST_LENGTH);
|
||||||
|
|
||||||
for(std::size_t c = 1; c < iterations; ++c)
|
|
||||||
SHA512(reinterpret_cast<const unsigned char *>(&hash[0]), hash.size(), reinterpret_cast<unsigned char *>(&hash[0]));
|
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns sha512 hash value from input stream.
|
/// Returns sha512 hash value from input stream.
|
||||||
static std::string sha512(std::istream &stream, std::size_t iterations = 1) noexcept {
|
static std::string sha512(std::istream &stream, std::size_t iterations = 1) noexcept {
|
||||||
SHA512_CTX context;
|
auto evp_md = EVP_sha512();
|
||||||
SHA512_Init(&context);
|
auto hash = stream_digest(stream, evp_md, SHA512_DIGEST_LENGTH);
|
||||||
std::streamsize read_length;
|
for(std::size_t i = 1; i < iterations; ++i)
|
||||||
std::vector<char> buffer(buffer_size);
|
hash = message_digest(hash, evp_md, SHA512_DIGEST_LENGTH);
|
||||||
while((read_length = stream.read(&buffer[0], buffer_size).gcount()) > 0)
|
|
||||||
SHA512_Update(&context, buffer.data(), static_cast<std::size_t>(read_length));
|
|
||||||
std::string hash;
|
|
||||||
hash.resize(512 / 8);
|
|
||||||
SHA512_Final(reinterpret_cast<unsigned char *>(&hash[0]), &context);
|
|
||||||
|
|
||||||
for(std::size_t c = 1; c < iterations; ++c)
|
|
||||||
SHA512(reinterpret_cast<const unsigned char *>(&hash[0]), hash.size(), reinterpret_cast<unsigned char *>(&hash[0]));
|
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns PBKDF2 hash value from the given password
|
|
||||||
/// Input parameter key_size number of bytes of the returned key.
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns PBKDF2 derived key from the given password.
|
* Returns PBKDF2 derived key from the given password.
|
||||||
*
|
*
|
||||||
|
|
@ -237,8 +211,7 @@ namespace SimpleWeb {
|
||||||
* @return The PBKDF2 derived key.
|
* @return The PBKDF2 derived key.
|
||||||
*/
|
*/
|
||||||
static std::string pbkdf2(const std::string &password, const std::string &salt, int iterations, int key_size) noexcept {
|
static std::string pbkdf2(const std::string &password, const std::string &salt, int iterations, int key_size) noexcept {
|
||||||
std::string key;
|
std::string key(static_cast<std::size_t>(key_size), '\0');
|
||||||
key.resize(static_cast<std::size_t>(key_size));
|
|
||||||
PKCS5_PBKDF2_HMAC_SHA1(password.c_str(), password.size(),
|
PKCS5_PBKDF2_HMAC_SHA1(password.c_str(), password.size(),
|
||||||
reinterpret_cast<const unsigned char *>(salt.c_str()), salt.size(), iterations,
|
reinterpret_cast<const unsigned char *>(salt.c_str()), salt.size(), iterations,
|
||||||
key_size, reinterpret_cast<unsigned char *>(&key[0]));
|
key_size, reinterpret_cast<unsigned char *>(&key[0]));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue