hash.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Based on
  3. * 1. OpenSSL lib
  4. * 2. PurpleI2P source code
  5. * 3. cppcodec lib
  6. *
  7. * PUBLIC DOMAIN C++ WRAPPER
  8. * acetone, 2022
  9. */
  10. #include "hash.h"
  11. #include "cppcodec/cppcodec/base64_default_rfc4648.hpp"
  12. #include <openssl/sha.h>
  13. #include <openssl/md5.h>
  14. namespace FriendlyCrypto {
  15. namespace Hash {
  16. const std::vector<uint8_t> Sum::data() const noexcept
  17. {
  18. return m_data;
  19. }
  20. const std::string Sum::base64String() const noexcept
  21. {
  22. return cppcodec::base64_rfc4648::encode (data());
  23. }
  24. bool Sum::operator==(const Sum &another) const noexcept
  25. {
  26. return m_data == another.data();
  27. }
  28. bool Sum::operator==(const std::vector<uint8_t> &rawAnother) const noexcept
  29. {
  30. return m_data == rawAnother;
  31. }
  32. bool Sum::operator==(const std::string &base64String) const noexcept
  33. {
  34. std::vector<uint8_t> rawAnother;
  35. try {
  36. rawAnother = cppcodec::base64_rfc4648::decode(base64String);
  37. } catch (...) {
  38. return false;
  39. }
  40. return operator==(rawAnother);
  41. }
  42. Sum sha256 (const std::vector<uint8_t> &data) noexcept
  43. {
  44. std::vector<uint8_t> result (SHA256_DIGEST_LENGTH);
  45. SHA256 (data.data(), data.size(), result.data());
  46. return result;
  47. }
  48. Sum sha512 (const std::vector<uint8_t> &data) noexcept
  49. {
  50. std::vector<uint8_t> result (SHA512_DIGEST_LENGTH);
  51. SHA512 (data.data(), data.size(), result.data());
  52. return result;
  53. }
  54. Sum md5 (const std::vector<uint8_t> &data) noexcept
  55. {
  56. std::vector<uint8_t> result (MD5_DIGEST_LENGTH);
  57. MD5 (data.data(), data.size(), result.data());
  58. return result;
  59. }
  60. } // namespace Hash
  61. } // namespace FriendlyCrypto