2021-08-22 20:23:18 +08:00
|
|
|
// Copyright (c) 2017-2021 Thomas Fussell
|
2017-04-12 04:58:09 +08:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
2021-08-22 20:23:18 +08:00
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
2017-04-12 04:58:09 +08:00
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE
|
|
|
|
//
|
|
|
|
// @license: http://www.opensource.org/licenses/mit-license.php
|
|
|
|
// @author: see AUTHORS file
|
2017-04-11 23:08:16 +08:00
|
|
|
|
2017-04-19 08:11:16 +08:00
|
|
|
#include <algorithm>
|
2017-04-11 23:08:16 +08:00
|
|
|
#include <array>
|
2017-04-12 06:04:05 +08:00
|
|
|
#include <cstring>
|
2017-04-11 23:08:16 +08:00
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
2019-12-27 01:03:12 +08:00
|
|
|
#include <string>
|
2017-04-11 23:08:16 +08:00
|
|
|
|
2017-04-21 02:03:03 +08:00
|
|
|
#include <detail/cryptography/sha.hpp>
|
2017-04-11 23:08:16 +08:00
|
|
|
|
2017-04-14 09:45:07 +08:00
|
|
|
extern "C" {
|
2017-04-11 23:08:16 +08:00
|
|
|
|
2017-04-14 09:45:07 +08:00
|
|
|
extern void sha1_hash(const uint8_t *message, size_t len, uint32_t hash[5]);
|
|
|
|
extern void sha512_hash(const uint8_t *message, size_t len, uint64_t hash[8]);
|
2017-04-11 23:08:16 +08:00
|
|
|
}
|
|
|
|
|
2017-04-14 09:59:09 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2017-04-19 06:30:54 +08:00
|
|
|
inline std::uint32_t byteswap32(std::uint32_t i)
|
2017-04-14 09:59:09 +08:00
|
|
|
{
|
|
|
|
return _byteswap_ulong(i);
|
|
|
|
}
|
|
|
|
|
2017-04-19 06:30:54 +08:00
|
|
|
inline std::uint64_t byteswap64(std::uint64_t i)
|
2017-04-14 09:59:09 +08:00
|
|
|
{
|
|
|
|
return _byteswap_uint64(i);
|
|
|
|
}
|
|
|
|
#else
|
2017-04-19 06:30:54 +08:00
|
|
|
inline std::uint32_t byteswap32(std::uint32_t i)
|
2017-04-14 09:59:09 +08:00
|
|
|
{
|
|
|
|
return __builtin_bswap32(i);
|
|
|
|
}
|
|
|
|
|
2017-04-19 06:30:54 +08:00
|
|
|
inline std::uint64_t byteswap64(std::uint64_t i)
|
2017-04-14 09:59:09 +08:00
|
|
|
{
|
|
|
|
return __builtin_bswap64(i);
|
|
|
|
}
|
|
|
|
#endif
|
2017-04-20 04:55:41 +08:00
|
|
|
|
|
|
|
void byteswap(std::uint32_t *arr, std::size_t len)
|
|
|
|
{
|
|
|
|
for (auto i = std::size_t(0); i < len; ++i)
|
|
|
|
{
|
|
|
|
arr[i] = byteswap32(arr[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void byteswap(std::uint64_t *arr, std::size_t len)
|
|
|
|
{
|
|
|
|
for (auto i = std::size_t(0); i < len; ++i)
|
|
|
|
{
|
|
|
|
arr[i] = byteswap64(arr[i]);
|
|
|
|
}
|
2017-04-14 09:59:09 +08:00
|
|
|
}
|
|
|
|
|
2017-04-20 04:55:41 +08:00
|
|
|
} // namespace
|
|
|
|
|
2017-04-14 09:45:07 +08:00
|
|
|
namespace xlnt {
|
|
|
|
namespace detail {
|
2017-04-11 23:08:16 +08:00
|
|
|
|
2017-04-19 06:30:54 +08:00
|
|
|
void sha1(const std::vector<std::uint8_t> &input, std::vector<std::uint8_t> &output)
|
2017-04-11 23:08:16 +08:00
|
|
|
{
|
2017-04-19 06:30:54 +08:00
|
|
|
static const auto sha1_bytes = 20;
|
|
|
|
|
|
|
|
output.resize(sha1_bytes);
|
|
|
|
auto output_pointer_u32 = reinterpret_cast<std::uint32_t *>(output.data());
|
|
|
|
|
|
|
|
sha1_hash(input.data(), input.size(), output_pointer_u32);
|
|
|
|
|
2017-04-20 04:55:41 +08:00
|
|
|
byteswap(output_pointer_u32, sha1_bytes / sizeof(std::uint32_t));
|
2017-04-11 23:08:16 +08:00
|
|
|
}
|
|
|
|
|
2017-04-19 06:30:54 +08:00
|
|
|
void sha512(const std::vector<std::uint8_t> &input, std::vector<std::uint8_t> &output)
|
2017-04-11 23:08:16 +08:00
|
|
|
{
|
2017-04-19 06:30:54 +08:00
|
|
|
static const auto sha512_bytes = 64;
|
|
|
|
|
|
|
|
output.resize(sha512_bytes);
|
|
|
|
auto output_pointer_u64 = reinterpret_cast<std::uint64_t *>(output.data());
|
|
|
|
|
|
|
|
sha512_hash(input.data(), input.size(), output_pointer_u64);
|
|
|
|
|
2017-04-20 04:55:41 +08:00
|
|
|
byteswap(output_pointer_u64, sha512_bytes / sizeof(std::uint64_t));
|
2017-04-11 23:08:16 +08:00
|
|
|
}
|
|
|
|
|
2017-04-12 04:58:09 +08:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace xlnt
|