summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/Digest.h
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2020-10-19 00:52:24 +0200
committermanuel <manuel@mausz.at>2020-10-19 00:52:24 +0200
commitbe933ef2241d79558f91796cc5b3a161f72ebf9c (patch)
treefe3ab2f130e20c99001f2d7a81d610c78c96a3f4 /xbmc/utils/Digest.h
parent5f8335c1e49ce108ef3481863833c98efa00411b (diff)
downloadkodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.gz
kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.bz2
kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.zip
sync with upstream
Diffstat (limited to 'xbmc/utils/Digest.h')
-rw-r--r--xbmc/utils/Digest.h138
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
20namespace KODI
21{
22namespace UTILITY
23{
24
25/**
26 * Utility class for calculating message digests/hashes, currently using OpenSSL
27 */
28class CDigest
29{
30public:
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
93private:
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
104struct 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
121inline 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
130inline bool operator!=(TypedDigest const& left, TypedDigest const& right)
131{
132 return !(left == right);
133}
134
135std::ostream& operator<<(std::ostream& os, TypedDigest const& digest);
136
137}
138}