/*
* This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see .
*/
#ifndef ZONEMINDER_SRC_ZM_CRYPTO_GENERICS_H_
#define ZONEMINDER_SRC_ZM_CRYPTO_GENERICS_H_
#include "zm_define.h"
#include "zm_utils.h"
#include
#include
namespace zm {
namespace crypto {
namespace impl {
enum class HashAlgorithms {
kMD5,
kSHA1
};
template
struct HashAlgorithm;
template<>
struct HashAlgorithm {
static constexpr size_t digest_length = 16;
};
template<>
struct HashAlgorithm {
static constexpr size_t digest_length = 20;
};
template
class GenericHash {
public:
static constexpr size_t DIGEST_LENGTH = HashAlgorithm::digest_length;
using Digest = std::array;
static Digest GetDigestOf(uint8 const *data, size_t len) {
Impl hash;
hash.UpdateData(data, len);
hash.Finalize();
return hash.GetDigest();
}
template
static Digest GetDigestOf(Ts &&... pack) {
Impl hash;
UpdateData(hash, std::forward(pack)...);
hash.Finalize();
return hash.GetDigest();
}
void UpdateData(const uint8 *data, size_t length) {
static_cast(*this).DoUpdateData(data, length);
}
void UpdateData(const std::string &str) {
UpdateData(reinterpret_cast(str.c_str()), str.size());
}
void UpdateData(const char *str) {
UpdateData(reinterpret_cast(str), strlen(str));
}
template
void UpdateData(Container const &c) {
UpdateData(ZM::data(c), ZM::size(c));
}
void Finalize() {
static_cast(*this).DoFinalize();
}
const Digest &GetDigest() const { return digest_; }
protected:
Digest digest_ = {};
private:
template
static void UpdateData(Impl &hash, T const &data) {
hash.UpdateData(data);
}
template
static void UpdateData(Impl &hash, T const &data, TRest &&... rest) {
hash.UpdateData(data);
UpdateData(hash, std::forward(rest)...);
}
};
}
}
}
#endif //ZONEMINDER_SRC_ZM_CRYPTO_GENERICS_H_