/* */ #ifndef D_MESSAGE_DIGEST_IMPL_H #define D_MESSAGE_DIGEST_IMPL_H #include "common.h" #include #include #include #include #include #include "a2functional.h" namespace aria2 { class MessageDigestImpl { public: typedef std::function()> factory_t; typedef std::tuple hash_info_t; typedef std::map hashes_t; template inline static hash_info_t make_hi() { return std::make_tuple([]() { return make_unique(); }, T::length()); } private: static hashes_t hashes; MessageDigestImpl(const MessageDigestImpl&) = delete; MessageDigestImpl& operator=(const MessageDigestImpl&) = delete; public: virtual ~MessageDigestImpl() {} static std::unique_ptr sha1(); inline static std::unique_ptr create( const std::string& hashType) { auto i = hashes.find(hashType); if (i == hashes.end()) { return nullptr; } factory_t factory; std::tie(factory, std::ignore) = i->second; return factory(); } inline static bool supports(const std::string& hashType) { auto i = hashes.find(hashType); return i != hashes.end(); } inline static size_t getDigestLength(const std::string& hashType) { auto i = hashes.find(hashType); if (i == hashes.end()) { return 0; } size_t len; std::tie(std::ignore, len) = i->second; return len; } public: virtual size_t getDigestLength() const = 0; virtual void reset() = 0; virtual void update(const void* data, size_t length) = 0; virtual void digest(unsigned char* md) = 0; protected: MessageDigestImpl() {} }; } // namespace aria2 #endif // D_MESSAGE_DIGEST_IMPL_H