Sans Pareil Technologies, Inc.

Key To Your Business

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.

    1: /*
    2: * DigestEngine.h
    3: *
    4: * Definition of class DigestEngine.
    5: *
    6: * Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
    7: * and Contributors.
    8: *
    9: * Permission is hereby granted, free of charge, to any person or organization
   10: * obtaining a copy of the software and accompanying documentation covered by
   11: * this license (the "Software") to use, reproduce, display, distribute,
   12: * execute, and transmit the Software, and to prepare derivative works of the
   13: * Software, and to permit third-parties to whom the Software is furnished to
   14: * do so, all subject to the following:
   15: *
   16: * The copyright notices in the Software and this entire statement, including
   17: * the above license grant, this restriction and the following disclaimer,
   18: * must be included in all copies of the Software, in whole or in part, and
   19: * all derivative works of the Software, unless such copies or derivative
   20: * works are solely in the form of machine-executable object code generated by
   21: * a source language processor.
   22: *
   23: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   24: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   25: * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
   26: * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
   27: * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
   28: * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   29: * DEALINGS IN THE SOFTWARE.
   30: */
   31:
   32: #ifndef SPT_DIGEST_DIGESTENGINE
   33: #define SPT_DIGEST_DIGESTENGINE
   34:
   35: #if defined( ARDUINO )
   36: #include "../StandardCplusplus/vector"
   37: #include "SPT.h"
   38: #else
   39: #include <vector>
   40: #include <SPT.h>
   41: #endif
   42:
   43:
   44: namespace spt
   45: {
   46:   /// Provides classes that compute message digests.
   47:   namespace digest
   48:   {
   49:     /**
   50:      * @brief Abstract base class for all classes implementing a message digest algorithm.
   51:      * Digest engines examples are MD5Engine and SHA1Engine. Call
   52:      * {@link update()} repeatedly with data to compute the digest from.
   53:      * When done, call {@link digest()} to obtain the message digest.
   54:      */
   55:     class DigestEngine
   56:     {
   57:     public:
   58:       typedef std::vector<Byte> Digest;
   59:
   60:       DigestEngine();
   61:       virtual ~DigestEngine();
   62:
   63:       /// Updates the digest with the given data of specified length.
   64:       void update( const void* data, uint32_t length );
   65:
   66:       /// Updates the digest with the given character.
   67:       void update( char data );
   68:
   69:       /// Updates the digest with the given string data.
   70:       void update( const std::string& data );
   71:
   72:       /// Returns the length of the digest in bytes.
   73:       virtual uint32_t digestLength() const = 0;
   74:
   75:       /// Resets the engine so that a new digest can be computed.
   76:       virtual void reset() = 0;
   77:
   78:       /// Finishes the computation of the digest and
   79:       /// returns the message digest. Resets the engine
   80:       /// and can thus only be called once for every digest.
   81:       /// The returned reference is valid until the next
   82:       /// time digest() is called, or the engine object is destroyed.
   83:       virtual const Digest& digest() = 0;
   84:
   85:       /// Converts a message digest into a string of hexadecimal numbers.
   86:       static std::string digestToHex( const Digest& bytes );
   87:
   88:     protected:
   89:       /// Updates the digest with the given data. Must be implemented
   90:       /// by subclasses.
   91:       virtual void updateImpl( const void* data, uint32_t length ) = 0;
   92:
   93:     private:
   94:       DigestEngine( const DigestEngine& );
   95:       DigestEngine& operator = ( const DigestEngine& );
   96:     };
   97:
   98:
   99:     inline void DigestEngine::update( const void* data, uint32_t length )
  100:     {
  101:       updateImpl( data, length );
  102:     }
  103:
  104:
  105:     inline void DigestEngine::update( char data )
  106:     {
  107:       updateImpl( &data, 1 );
  108:     }
  109:
  110:
  111:     inline void DigestEngine::update( const std::string& data )
  112:     {
  113:       updateImpl( data.data(), static_cast<uint32_t>( data.size() ) );
  114:     }
  115:
  116:   } // namespace digest
  117:
  118: } // namespace spt
  119:
  120:
  121: #endif // SPT_DIGEST_DIGESTENGINE



DigestEngine.cpp

The implementation of the abstract base class.

    1: /*
    2: * DigestEngine.cpp
    3: *
    4: * $Id: DigestEngine.cpp 5792 2015-06-13 19:12:19Z rakesh $
    5: *
    6: * Library: Foundation
    7: * Package: Crypt
    8: * Module:  DigestEngine
    9: *
   10: * Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
   11: * and Contributors.
   12: *
   13: * Permission is hereby granted, free of charge, to any person or organization
   14: * obtaining a copy of the software and accompanying documentation covered by
   15: * this license (the "Software") to use, reproduce, display, distribute,
   16: * execute, and transmit the Software, and to prepare derivative works of the
   17: * Software, and to permit third-parties to whom the Software is furnished to
   18: * do so, all subject to the following:
   19: *
   20: * The copyright notices in the Software and this entire statement, including
   21: * the above license grant, this restriction and the following disclaimer,
   22: * must be included in all copies of the Software, in whole or in part, and
   23: * all derivative works of the Software, unless such copies or derivative
   24: * works are solely in the form of machine-executable object code generated by
   25: * a source language processor.
   26: *
   27: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   28: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   29: * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
   30: * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
   31: * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
   32: * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   33: * DEALINGS IN THE SOFTWARE.
   34: */
   35:
   36:
   37: #include "DigestEngine.h"
   38:
   39: using std::string;
   40: using spt::digest::DigestEngine;
   41:
   42:
   43: DigestEngine::DigestEngine() {}
   44:
   45:
   46: DigestEngine::~DigestEngine() {}
   47:
   48:
   49: string DigestEngine::digestToHex( const Digest& bytes )
   50: {
   51:   static const char digits[] = "0123456789abcdef";
   52:   string result;
   53:   result.reserve( bytes.size() * 2 );
   54:
   55:   for ( Digest::const_iterator it = bytes.begin(); it != bytes.end(); ++it )
   56:   {
   57:     spt::Byte c = *it;
   58:     result += digits[(c >> 4) & 0xF];
   59:     result += digits[c & 0xF];
   60:   }
   61:
   62:   return result;
   63: }