Remove sha256 hash from storage internal code (#2585)

This commit is contained in:
JinmingHu 2021-07-12 10:07:26 +08:00 committed by GitHub
parent 89222f1950
commit 46e333d32d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 4 additions and 71 deletions

View File

@ -6,6 +6,7 @@
#include <chrono>
#include <thread>
#include <azure/core/internal/cryptography/sha_hash.hpp>
#include <azure/storage/blobs/blob_lease_client.hpp>
#include <azure/storage/blobs/blob_sas_builder.hpp>
#include <azure/storage/common/crypt.hpp>
@ -566,7 +567,8 @@ namespace Azure { namespace Storage { namespace Test {
aes256Key.resize(32);
RandomBuffer(&aes256Key[0], aes256Key.size());
key.Key = Azure::Core::Convert::Base64Encode(aes256Key);
key.KeyHash = _internal::Sha256(aes256Key);
key.KeyHash = Azure::Core::Cryptography::_internal::Sha256Hash().Final(
aes256Key.data(), aes256Key.size());
key.Algorithm = Blobs::Models::EncryptionAlgorithmType::Aes256;
return key;
};

View File

@ -37,7 +37,6 @@ namespace Azure { namespace Storage {
};
namespace _internal {
std::vector<uint8_t> Sha256(const std::vector<uint8_t>& data);
std::vector<uint8_t> HmacSha256(
const std::vector<uint8_t>& data,
const std::vector<uint8_t>& key);

View File

@ -85,7 +85,6 @@ namespace Azure { namespace Storage {
enum class AlgorithmType
{
HmacSha256,
Sha256,
};
struct AlgorithmProviderInstance final
@ -97,7 +96,7 @@ namespace Azure { namespace Storage {
AlgorithmProviderInstance(AlgorithmType type)
{
const wchar_t* algorithmId = nullptr;
if (type == AlgorithmType::HmacSha256 || type == AlgorithmType::Sha256)
if (type == AlgorithmType::HmacSha256)
{
algorithmId = BCRYPT_SHA256_ALGORITHM;
}
@ -149,53 +148,6 @@ namespace Azure { namespace Storage {
~AlgorithmProviderInstance() { BCryptCloseAlgorithmProvider(Handle, 0); }
};
std::vector<uint8_t> Sha256(const std::vector<uint8_t>& data)
{
AZURE_ASSERT_MSG(data.size() <= std::numeric_limits<ULONG>::max(), "Data size is too big.");
static AlgorithmProviderInstance AlgorithmProvider(AlgorithmType::Sha256);
std::string context;
context.resize(AlgorithmProvider.ContextSize);
BCRYPT_HASH_HANDLE hashHandle;
NTSTATUS status = BCryptCreateHash(
AlgorithmProvider.Handle,
&hashHandle,
reinterpret_cast<PUCHAR>(&context[0]),
static_cast<ULONG>(context.size()),
nullptr,
0,
0);
if (!BCRYPT_SUCCESS(status))
{
throw std::runtime_error("BCryptCreateHash failed.");
}
status = BCryptHashData(
hashHandle,
reinterpret_cast<PBYTE>(const_cast<uint8_t*>(data.data())),
static_cast<ULONG>(data.size()),
0);
if (!BCRYPT_SUCCESS(status))
{
throw std::runtime_error("BCryptHashData failed.");
}
std::vector<uint8_t> hash;
hash.resize(AlgorithmProvider.HashLength);
status = BCryptFinishHash(
hashHandle, reinterpret_cast<PUCHAR>(&hash[0]), static_cast<ULONG>(hash.size()), 0);
if (!BCRYPT_SUCCESS(status))
{
throw std::runtime_error("BCryptFinishHash failed.");
}
BCryptDestroyHash(hashHandle);
return hash;
}
std::vector<uint8_t> HmacSha256(
const std::vector<uint8_t>& data,
const std::vector<uint8_t>& key)
@ -250,16 +202,6 @@ namespace Azure { namespace Storage {
namespace _internal {
std::vector<uint8_t> Sha256(const std::vector<uint8_t>& data)
{
SHA256_CTX context;
SHA256_Init(&context);
SHA256_Update(&context, data.data(), data.size());
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_Final(hash, &context);
return std::vector<uint8_t>(std::begin(hash), std::end(hash));
}
std::vector<uint8_t> HmacSha256(
const std::vector<uint8_t>& data,
const std::vector<uint8_t>& key)

View File

@ -15,16 +15,6 @@ namespace Azure { namespace Storage { namespace Test {
return std::vector<uint8_t>(start, start + strlen(text));
}
TEST(CryptFunctionsTest, Sha256)
{
EXPECT_EQ(
Azure::Core::Convert::Base64Encode(_internal::Sha256(ToBinaryVector(""))),
"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=");
EXPECT_EQ(
Azure::Core::Convert::Base64Encode(_internal::Sha256(ToBinaryVector("Hello Azure!"))),
"Mjzwx2mqGHb9FSgjm33ShNmXYndkgvwA6tQmEiskOHg=");
}
TEST(CryptFunctionsTest, HmacSha256)
{
std::string key = "8CwtGFF1mGR4bPEP9eZ0x1fxKiQ3Ca5N";