DigestEngine
DigestEngine defines the interface for all message digest implementations. This is taken from the Poco project and adapted for use on Arduino.
DigestEngine.h
The definition of the interface for generating message digests.
/* * DigestEngine.h * * Definition of class DigestEngine. * * Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. * and Contributors. * * Permission is hereby granted, free of charge, to any person or organization * obtaining a copy of the software and accompanying documentation covered by * this license (the "Software") to use, reproduce, display, distribute, * execute, and transmit the Software, and to prepare derivative works of the * Software, and to permit third-parties to whom the Software is furnished to * do so, all subject to the following: * * The copyright notices in the Software and this entire statement, including * the above license grant, this restriction and the following disclaimer, * must be included in all copies of the Software, in whole or in part, and * all derivative works of the Software, unless such copies or derivative * works are solely in the form of machine-executable object code generated by * a source language processor. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ #ifndef SPT_DIGEST_DIGESTENGINE #define SPT_DIGEST_DIGESTENGINE #if defined( ARDUINO ) #include "../StandardCplusplus/vector" #include "SPT.h" #else #include <vector> #include <SPT.h> #endif namespace spt { /// Provides classes that compute message digests. namespace digest { /** * @brief Abstract base class for all classes implementing a message digest algorithm. * Digest engines examples are MD5Engine and SHA1Engine. Call * {@link update()} repeatedly with data to compute the digest from. * When done, call {@link digest()} to obtain the message digest. */ class DigestEngine { public: typedef std::vector<Byte> Digest; DigestEngine(); virtual ~DigestEngine(); /// Updates the digest with the given data of specified length. void update( const void* data, uint32_t length ); /// Updates the digest with the given character. void update( char data ); /// Updates the digest with the given string data. void update( const std::string& data ); /// Returns the length of the digest in bytes. virtual uint32_t digestLength() const = 0; /// Resets the engine so that a new digest can be computed. virtual void reset() = 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. virtual const Digest& digest() = 0; /// Converts a message digest into a string of hexadecimal numbers. static std::string digestToHex( const Digest& bytes ); protected: /// Updates the digest with the given data. Must be implemented /// by subclasses. virtual void updateImpl( const void* data, uint32_t length ) = 0; private: DigestEngine( const DigestEngine& ); DigestEngine& operator = ( const DigestEngine& ); }; inline void DigestEngine::update( const void* data, uint32_t length ) { updateImpl( data, length ); } inline void DigestEngine::update( char data ) { updateImpl( &data, 1 ); } inline void DigestEngine::update( const std::string& data ) { updateImpl( data.data(), static_cast<uint32_t>( data.size() ) ); } } // namespace digest } // namespace spt #endif // SPT_DIGEST_DIGESTENGINE
DigestEngine.cpp
The implementation of the abstract base class.
/* * DigestEngine.cpp * * $Id: DigestEngine.cpp 5792 2015-06-13 19:12:19Z rakesh $ * * Library: Foundation * Package: Crypt * Module: DigestEngine * * Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. * and Contributors. * * Permission is hereby granted, free of charge, to any person or organization * obtaining a copy of the software and accompanying documentation covered by * this license (the "Software") to use, reproduce, display, distribute, * execute, and transmit the Software, and to prepare derivative works of the * Software, and to permit third-parties to whom the Software is furnished to * do so, all subject to the following: * * The copyright notices in the Software and this entire statement, including * the above license grant, this restriction and the following disclaimer, * must be included in all copies of the Software, in whole or in part, and * all derivative works of the Software, unless such copies or derivative * works are solely in the form of machine-executable object code generated by * a source language processor. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ #include "DigestEngine.h" using std::string; using spt::digest::DigestEngine; DigestEngine::DigestEngine() {} DigestEngine::~DigestEngine() {} string DigestEngine::digestToHex( const Digest& bytes ) { static const char digits[] = "0123456789abcdef"; string result; result.reserve( bytes.size() * 2 ); for ( Digest::const_iterator it = bytes.begin(); it != bytes.end(); ++it ) { spt::Byte c = *it; result += digits[(c >> 4) & 0xF]; result += digits[c & 0xF]; } return result; }
Digest engine instances may be used independently or with the HMACEngine to generate signed digest messages.