Overte C++ Documentation
HMACAuth.h
1 //
2 // HMACAuth.h
3 // libraries/networking/src
4 //
5 // Created by Simon Walton on 3/19/2018.
6 // Copyright 2018 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_HMACAuth_h
13 #define hifi_HMACAuth_h
14 
15 #include <vector>
16 #include <memory>
17 #include <QtCore/QMutex>
18 
19 class QUuid;
20 
21 class HMACAuth {
22 public:
23  enum AuthMethod { MD5, SHA1, SHA224, SHA256, RIPEMD160 };
24  using HMACHash = std::vector<unsigned char>;
25 
26  explicit HMACAuth(AuthMethod authMethod = MD5);
27  ~HMACAuth();
28 
29  bool setKey(const char* keyValue, int keyLen);
30  bool setKey(const QUuid& uidKey);
31  // Calculate complete hash in one.
32  bool calculateHash(HMACHash& hashResult, const char* data, int dataLen);
33 
34  // Append to data to be hashed.
35  bool addData(const char* data, int dataLen);
36  // Get the resulting hash from calls to addData().
37  // Note that only one hash may be calculated at a time for each
38  // HMACAuth instance if this interface is used.
39  HMACHash result();
40 
41 private:
42  QRecursiveMutex _lock;
43  struct hmac_ctx_st* _hmacContext;
44  AuthMethod _authMethod;
45 };
46 
47 #endif // hifi_HMACAuth_h