sha.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (c) 2017-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <algorithm>
  24. #include <array>
  25. #include <cstring>
  26. #include <iomanip>
  27. #include <sstream>
  28. #include <string>
  29. #include <detail/cryptography/sha.hpp>
  30. extern "C" {
  31. extern void sha1_hash(const uint8_t *message, size_t len, uint32_t hash[5]);
  32. extern void sha512_hash(const uint8_t *message, size_t len, uint64_t hash[8]);
  33. }
  34. namespace {
  35. #ifdef _MSC_VER
  36. inline std::uint32_t byteswap32(std::uint32_t i)
  37. {
  38. return _byteswap_ulong(i);
  39. }
  40. inline std::uint64_t byteswap64(std::uint64_t i)
  41. {
  42. return _byteswap_uint64(i);
  43. }
  44. #else
  45. inline std::uint32_t byteswap32(std::uint32_t i)
  46. {
  47. return __builtin_bswap32(i);
  48. }
  49. inline std::uint64_t byteswap64(std::uint64_t i)
  50. {
  51. return __builtin_bswap64(i);
  52. }
  53. #endif
  54. void byteswap(std::uint32_t *arr, std::size_t len)
  55. {
  56. for (auto i = std::size_t(0); i < len; ++i)
  57. {
  58. arr[i] = byteswap32(arr[i]);
  59. }
  60. }
  61. void byteswap(std::uint64_t *arr, std::size_t len)
  62. {
  63. for (auto i = std::size_t(0); i < len; ++i)
  64. {
  65. arr[i] = byteswap64(arr[i]);
  66. }
  67. }
  68. } // namespace
  69. namespace xlnt {
  70. namespace detail {
  71. void sha1(const std::vector<std::uint8_t> &input, std::vector<std::uint8_t> &output)
  72. {
  73. static const auto sha1_bytes = 20;
  74. output.resize(sha1_bytes);
  75. auto output_pointer_u32 = reinterpret_cast<std::uint32_t *>(output.data());
  76. sha1_hash(input.data(), input.size(), output_pointer_u32);
  77. byteswap(output_pointer_u32, sha1_bytes / sizeof(std::uint32_t));
  78. }
  79. void sha512(const std::vector<std::uint8_t> &input, std::vector<std::uint8_t> &output)
  80. {
  81. static const auto sha512_bytes = 64;
  82. output.resize(sha512_bytes);
  83. auto output_pointer_u64 = reinterpret_cast<std::uint64_t *>(output.data());
  84. sha512_hash(input.data(), input.size(), output_pointer_u64);
  85. byteswap(output_pointer_u64, sha512_bytes / sizeof(std::uint64_t));
  86. }
  87. } // namespace detail
  88. } // namespace xlnt