// ---------------------------------------------------------------------------
// - Digest.hpp                                                              -
// - afnix cryptography - base message digest class definition               -
// ---------------------------------------------------------------------------
// - This program is free software;  you can redistribute it  and/or  modify -
// - it provided that this copyright notice is kept intact.                  -
// -                                                                         -
// - 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.  In no event shall -
// - the copyright holder be liable for any  direct, indirect, incidental or -
// - special damages arising in any way out of the use of this software.     -
// ---------------------------------------------------------------------------
// - copyright (c) 1999-2005 amaury darsch                                   -
// ---------------------------------------------------------------------------

#ifndef  AFNIX_DIGEST_HPP
#define  AFNIX_DIGEST_HPP

#ifndef  AFNIX_INPUT_HPP
#include "Input.hpp"
#endif

#ifndef  AFNIX_BUFFER_HPP
#include "Buffer.hpp"
#endif

namespace afnix {

  /// The Digest class is a base class that is used to build a message
  /// digest also known as message hash. A message digest is a hash computation
  /// of a particular message. The hash agorithm is determined by the class
  /// derivation
  /// @author amaury darsch

  class Digest {
  protected:
    /// the digest name
    String d_name;
    /// requested buffer size
    long    d_size;
    /// the message length
    t_long  d_length;
    /// the buffer count
    long    d_count;
    /// the buffer data
    t_byte* p_data;

  public:
    /// create a digest object by name and size
    /// @param name the digest name
    /// @param size the block size
    Digest (const String& name, const long size);

    /// destroy the digest
    virtual ~Digest (void);

    /// reset this digest
    virtual void reset (void);

    /// clear this digest counter
    virtual void clear (void);

    /// @return the digest name
    virtual String getname (void) const;

    /// @return the buffer count
    virtual long getcount (void) const;

    /// @return the buffer length
    virtual t_long getlength (void) const;

    /// @return the buffer count
    /// copy some data in the buffer by size and return the copied length
    /// @param data the data to copy in the buffer
    /// @param size the data size
    virtual long copy (const t_byte* data, const long size);

    /// copy some data in the buffer from a stream
    /// @param is the input stream used as input
    virtual long copy (Input& is);

    /// update the message state with the buffer data
    virtual void update (void) =0;

    /// process a message by data
    /// @param data the data to process
    /// @param size the data size
    virtual void process (const t_byte* data, const long size) =0;
 
   /// process a message with an input stream
    /// @param is the input stream to process
    virtual void process (Input& is) =0;

    /// finish the message by padding the data and return the digest
    virtual void finish (void) =0;

    /// @return the formatted message digest
    virtual String format (void) const =0;

    /// compute a message digest from a string
    /// @param msg the string message to compute
    virtual String compute (const String& msg) =0;

    /// compute a message digest from an input stream
    /// @param is the input stream
    virtual String compute (Input& is) =0; 

  private:
    // make the copy constructor private
    Digest (const Digest&);
    // make the assignment operator private
    Digest& operator = (const Digest&);
  };
}

#endif
