2012-04-28 11:32:25 +02:00
|
|
|
//
|
|
|
|
// DigestEngine.h
|
|
|
|
//
|
|
|
|
// $Id: //poco/1.4/Foundation/include/Poco/DigestEngine.h#1 $
|
|
|
|
//
|
|
|
|
// Library: Foundation
|
|
|
|
// Package: Crypt
|
|
|
|
// Module: DigestEngine
|
|
|
|
//
|
|
|
|
// Definition of class DigestEngine.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
2014-05-04 21:02:42 +02:00
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
2012-04-28 11:32:25 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef Foundation_DigestEngine_INCLUDED
|
|
|
|
#define Foundation_DigestEngine_INCLUDED
|
|
|
|
|
|
|
|
|
|
|
|
#include "Poco/Foundation.h"
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
namespace Poco {
|
|
|
|
|
|
|
|
|
|
|
|
class Foundation_API DigestEngine
|
|
|
|
/// This class is an abstract base class
|
|
|
|
/// for all classes implementing a message
|
|
|
|
/// digest algorithm, like MD5Engine
|
|
|
|
/// and SHA1Engine.
|
|
|
|
/// Call update() repeatedly with data to
|
|
|
|
/// compute the digest from. When done,
|
|
|
|
/// call digest() to obtain the message
|
|
|
|
/// digest.
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef std::vector<unsigned char> Digest;
|
|
|
|
|
|
|
|
DigestEngine();
|
|
|
|
virtual ~DigestEngine();
|
|
|
|
|
2012-07-20 04:48:41 +02:00
|
|
|
void update(const void* data, std::size_t length);
|
2012-04-28 11:32:25 +02:00
|
|
|
void update(char data);
|
|
|
|
void update(const std::string& data);
|
|
|
|
/// Updates the digest with the given data.
|
|
|
|
|
2012-07-20 04:48:41 +02:00
|
|
|
virtual std::size_t digestLength() const = 0;
|
2012-04-28 11:32:25 +02:00
|
|
|
/// Returns the length of the digest in bytes.
|
|
|
|
|
|
|
|
virtual void reset() = 0;
|
|
|
|
/// Resets the engine so that a new
|
|
|
|
/// digest can be computed.
|
|
|
|
|
|
|
|
virtual const Digest& digest() = 0;
|
|
|
|
/// Finishes the computation of the digest and
|
|
|
|
/// returns the message digest. Resets the engine
|
|
|
|
/// and can thus only be called once for every digest.
|
|
|
|
/// The returned reference is valid until the next
|
|
|
|
/// time digest() is called, or the engine object is destroyed.
|
|
|
|
|
2012-11-13 03:45:16 +01:00
|
|
|
static std::string digestToHex(const Digest& bytes);
|
|
|
|
/// Converts a message digest into a string of hexadecimal numbers.
|
2012-04-28 11:32:25 +02:00
|
|
|
|
2012-11-13 03:45:16 +01:00
|
|
|
static Digest digestFromHex(const std::string& digest);
|
|
|
|
/// Converts a string created by digestToHex back to its Digest presentation
|
2012-04-28 11:32:25 +02:00
|
|
|
|
|
|
|
protected:
|
2012-11-13 03:45:16 +01:00
|
|
|
virtual void updateImpl(const void* data, std::size_t length) = 0;
|
|
|
|
/// Updates the digest with the given data. Must be implemented
|
2012-04-28 11:32:25 +02:00
|
|
|
/// by subclasses.
|
|
|
|
|
|
|
|
private:
|
|
|
|
DigestEngine(const DigestEngine&);
|
|
|
|
DigestEngine& operator = (const DigestEngine&);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// inlines
|
|
|
|
//
|
|
|
|
|
|
|
|
|
2012-07-20 04:48:41 +02:00
|
|
|
inline void DigestEngine::update(const void* data, std::size_t length)
|
2012-04-28 11:32:25 +02:00
|
|
|
{
|
|
|
|
updateImpl(data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void DigestEngine::update(char data)
|
|
|
|
{
|
|
|
|
updateImpl(&data, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void DigestEngine::update(const std::string& data)
|
|
|
|
{
|
2012-07-20 04:48:41 +02:00
|
|
|
updateImpl(data.data(), data.size());
|
2012-04-28 11:32:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Poco
|
|
|
|
|
|
|
|
|
|
|
|
#endif // Foundation_DigestEngine_INCLUDED
|