diff options
Diffstat (limited to 'xbmc/utils/Digest.h')
| -rw-r--r-- | xbmc/utils/Digest.h | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/xbmc/utils/Digest.h b/xbmc/utils/Digest.h new file mode 100644 index 0000000..6452857 --- /dev/null +++ b/xbmc/utils/Digest.h | |||
| @@ -0,0 +1,138 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2018 Team Kodi | ||
| 3 | * This file is part of Kodi - https://kodi.tv | ||
| 4 | * | ||
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later | ||
| 6 | * See LICENSES/README.md for more information. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #pragma once | ||
| 10 | |||
| 11 | #include "StringUtils.h" | ||
| 12 | |||
| 13 | #include <iostream> | ||
| 14 | #include <memory> | ||
| 15 | #include <stdexcept> | ||
| 16 | #include <string> | ||
| 17 | |||
| 18 | #include <openssl/evp.h> | ||
| 19 | |||
| 20 | namespace KODI | ||
| 21 | { | ||
| 22 | namespace UTILITY | ||
| 23 | { | ||
| 24 | |||
| 25 | /** | ||
| 26 | * Utility class for calculating message digests/hashes, currently using OpenSSL | ||
| 27 | */ | ||
| 28 | class CDigest | ||
| 29 | { | ||
| 30 | public: | ||
| 31 | enum class Type | ||
| 32 | { | ||
| 33 | MD5, | ||
| 34 | SHA1, | ||
| 35 | SHA256, | ||
| 36 | SHA512, | ||
| 37 | INVALID | ||
| 38 | }; | ||
| 39 | |||
| 40 | /** | ||
| 41 | * Convert type enumeration value to lower-case string representation | ||
| 42 | */ | ||
| 43 | static std::string TypeToString(Type type); | ||
| 44 | /** | ||
| 45 | * Convert digest type string representation to enumeration value | ||
| 46 | */ | ||
| 47 | static Type TypeFromString(std::string const& type); | ||
| 48 | |||
| 49 | /** | ||
| 50 | * Create a digest calculation object | ||
| 51 | */ | ||
| 52 | CDigest(Type type); | ||
| 53 | /** | ||
| 54 | * Update digest with data | ||
| 55 | * | ||
| 56 | * Cannot be called after \ref Finalize has been called | ||
| 57 | */ | ||
| 58 | void Update(std::string const& data); | ||
| 59 | /** | ||
| 60 | * Update digest with data | ||
| 61 | * | ||
| 62 | * Cannot be called after \ref Finalize has been called | ||
| 63 | */ | ||
| 64 | void Update(void const* data, std::size_t size); | ||
| 65 | /** | ||
| 66 | * Finalize and return the digest | ||
| 67 | * | ||
| 68 | * The digest object cannot be used any more after this function | ||
| 69 | * has been called. | ||
| 70 | * | ||
| 71 | * \return digest value as string in lower-case hexadecimal notation | ||
| 72 | */ | ||
| 73 | std::string Finalize(); | ||
| 74 | /** | ||
| 75 | * Finalize and return the digest | ||
| 76 | * | ||
| 77 | * The digest object cannot be used any more after this | ||
| 78 | * function has been called. | ||
| 79 | * | ||
| 80 | * \return digest value as binary std::string | ||
| 81 | */ | ||
| 82 | std::string FinalizeRaw(); | ||
| 83 | |||
| 84 | /** | ||
| 85 | * Calculate message digest | ||
| 86 | */ | ||
| 87 | static std::string Calculate(Type type, std::string const& data); | ||
| 88 | /** | ||
| 89 | * Calculate message digest | ||
| 90 | */ | ||
| 91 | static std::string Calculate(Type type, void const* data, std::size_t size); | ||
| 92 | |||
| 93 | private: | ||
| 94 | struct MdCtxDeleter | ||
| 95 | { | ||
| 96 | void operator()(EVP_MD_CTX* context); | ||
| 97 | }; | ||
| 98 | |||
| 99 | bool m_finalized{false}; | ||
| 100 | std::unique_ptr<EVP_MD_CTX, MdCtxDeleter> m_context; | ||
| 101 | EVP_MD const* m_md; | ||
| 102 | }; | ||
| 103 | |||
| 104 | struct TypedDigest | ||
| 105 | { | ||
| 106 | CDigest::Type type{CDigest::Type::INVALID}; | ||
| 107 | std::string value; | ||
| 108 | |||
| 109 | TypedDigest() = default; | ||
| 110 | |||
| 111 | TypedDigest(CDigest::Type type, std::string const& value) | ||
| 112 | : type(type), value(value) | ||
| 113 | {} | ||
| 114 | |||
| 115 | bool Empty() const | ||
| 116 | { | ||
| 117 | return (type == CDigest::Type::INVALID || value.empty()); | ||
| 118 | } | ||
| 119 | }; | ||
| 120 | |||
| 121 | inline bool operator==(TypedDigest const& left, TypedDigest const& right) | ||
| 122 | { | ||
| 123 | if (left.type != right.type) | ||
| 124 | { | ||
| 125 | throw std::logic_error("Cannot compare digests of different type"); | ||
| 126 | } | ||
| 127 | return StringUtils::EqualsNoCase(left.value, right.value); | ||
| 128 | } | ||
| 129 | |||
| 130 | inline bool operator!=(TypedDigest const& left, TypedDigest const& right) | ||
| 131 | { | ||
| 132 | return !(left == right); | ||
| 133 | } | ||
| 134 | |||
| 135 | std::ostream& operator<<(std::ostream& os, TypedDigest const& digest); | ||
| 136 | |||
| 137 | } | ||
| 138 | } | ||
