1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-06 17:41:04 +00:00

Use openssl's sha1 and sha256, optionally (#15472)

This commit is contained in:
DS 2024-12-10 22:00:43 +01:00 committed by GitHub
parent 4f800dd2b4
commit bcbee873e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 141 additions and 43 deletions

View file

@ -1,10 +1,11 @@
set(util_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/areastore.cpp
${CMAKE_CURRENT_SOURCE_DIR}/auth.cpp
${CMAKE_CURRENT_SOURCE_DIR}/colorize.cpp
${CMAKE_CURRENT_SOURCE_DIR}/base64.cpp
${CMAKE_CURRENT_SOURCE_DIR}/colorize.cpp
${CMAKE_CURRENT_SOURCE_DIR}/directiontables.cpp
${CMAKE_CURRENT_SOURCE_DIR}/enriched_string.cpp
${CMAKE_CURRENT_SOURCE_DIR}/hashing.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ieee_float.cpp
${CMAKE_CURRENT_SOURCE_DIR}/metricsbackend.cpp
${CMAKE_CURRENT_SOURCE_DIR}/numeric.cpp

View file

@ -6,7 +6,7 @@
#include <string>
#include "auth.h"
#include "base64.h"
#include "sha1.h"
#include "util/hashing.h"
#include "srp.h"
#include "util/string.h"
#include "debug.h"
@ -23,9 +23,7 @@ std::string translate_password(const std::string &name,
return "";
std::string slt = name + password;
SHA1 sha1;
sha1.addBytes(slt);
std::string digest = sha1.getDigest();
std::string digest = hashing::sha1(slt);
std::string pwd = base64_encode(digest);
return pwd;
}

50
src/util/hashing.cpp Normal file
View file

@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: 2024 Luanti Contributors
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "hashing.h"
#include "debug.h"
#include "config.h"
#if USE_OPENSSL
#include <openssl/sha.h>
#include <openssl/evp.h>
#else
#include "util/sha1.h"
#include "my_sha256.h"
#endif
namespace hashing
{
std::string sha1(std::string_view data)
{
#if USE_OPENSSL
std::string digest(SHA1_DIGEST_SIZE, '\000');
auto src = reinterpret_cast<const uint8_t *>(data.data());
auto dst = reinterpret_cast<uint8_t *>(digest.data());
SHA1(src, data.size(), dst);
return digest;
#else
SHA1 sha1;
sha1.addBytes(data);
return sha1.getDigest();
#endif
}
std::string sha256(std::string_view data)
{
std::string digest(SHA256_DIGEST_SIZE, '\000');
auto src = reinterpret_cast<const uint8_t *>(data.data());
auto dst = reinterpret_cast<uint8_t *>(digest.data());
#if USE_OPENSSL
// can't call SHA256(), because it's defined by our sha256.c fallback
auto success = EVP_Digest(src, data.size(), dst, nullptr, EVP_sha256(), nullptr) == 1;
FATAL_ERROR_IF(!success, "sha256 failed");
#else
SHA256(src, data.size(), dst);
#endif
return digest;
}
}

21
src/util/hashing.h Normal file
View file

@ -0,0 +1,21 @@
// SPDX-FileCopyrightText: 2024 Luanti Contributors
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#pragma once
#include <string>
#include <string_view>
namespace hashing
{
// Size of raw digest in bytes
constexpr size_t SHA1_DIGEST_SIZE = 20;
constexpr size_t SHA256_DIGEST_SIZE = 32;
// Returns the digest of data
std::string sha1(std::string_view data);
std::string sha256(std::string_view data);
}