Logo Search packages:      
Sourcecode: inkscape version File versions

digest.h

#ifndef __DIGEST_H__
#define __DIGEST_H__
/**
 * Secure Hashing Tool
 * *
 * Authors:
 *   Bob Jamison
 *
 * Copyright (C) 2006 Bob Jamison
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 *
 *  This base class and its subclasses  provide an easy API for providing
 *  several different types of secure hashing functions for whatever use
 *  a developer might need.  This is not intended as a high-performance
 *  replacement for the fine implementations already available.  Rather, it
 *  is a small and simple (and maybe a bit slow?) tool for moderate common
 *  hashing requirements, like for communications and authentication.
 *
 *  These hashes are intended to be simple to use.  For example:
 *  Sha256Digest digest;
 *  digest.append("The quick brown dog");
 *  std::string result = digest.finishHex();
 *
 *  There are several forms of append() for convenience.
 *  finish() and finishHex() call reset() for both security and
 *  to prepare for the next use.
 *
 */

#include <vector>
#include <string>


/**
 *  Base class.  Do not use this class directly.  Rather, use of of the
 *  subclasses below.
 *  For all subclasses, overload reset(), update(unsigned char), and finish()
 */
00055 class Digest
{
public:

    /**
     *  Different types of hash algorithms.
     */
00062     typedef enum
        {
        HASH_NONE,
        HASH_SHA1,
        HASH_SHA224,
        HASH_SHA256,
        HASH_SHA384,
        HASH_SHA512,
        HASH_MD5
        } HashType;

    /**
     *  Constructor, with no type
     */
00076     Digest() : hashType(HASH_NONE)
        { reset(); }

    /**
     *  Destructor
     */
00082     virtual ~Digest()
        { reset(); }

    /**
     *  Return one of the enumerated hash types above
     */
00088     virtual int getType()
        { return hashType; }

    /**
     *  Append a single byte to the hash
     */
00094     void append(unsigned char ch)
        { update(ch); }

    /**
     *  Append a string to the hash
     */
00100     virtual void append(const std::string &str)
        {
        for (unsigned int i=0 ; i<str.size() ; i++)
            update((unsigned char)str[i]);
        }

    /**
     *  Append a byte buffer to the hash
     */
00109     virtual void append(unsigned char *buf, int len)
        {
        for (int i=0 ; i<len ; i++)
            update(buf[i]);
        }

    /**
     *  Append a byte vector to the hash
     */
00118     virtual void append(const std::vector<unsigned char> buf)
        {
        for (unsigned int i=0 ; i<buf.size() ; i++)
            update(buf[i]);
        }

    /**
     *  Finish the hash and return a hexidecimal version of the computed
     *  value
     */
    virtual std::string finishHex();

    /**
     *  Initialize the fields of this hash engine to its starting values.
     *  Overload this in every subclass
     */
00134     virtual void reset()
        {}

    /**
     *  Finish the hash and return its computed value
     *  Overload this in every subclass
     */
00141     virtual std::vector<unsigned char> finish()
        {
        std::vector<unsigned char> ret;
        return ret;
        }

protected:

    /**
     *  Update the hash with a given byte
     *  Overload this in every subclass
     */
00153     virtual void update(unsigned char /*ch*/)
        {}

    /**
     * The enumerated type of the hash
     */
00159     int hashType;
};





/**
 *  SHA-1,
 *  Section 6.1, SECURE HASH STANDARD
 *  Federal Information Processing Standards Publication 180-2
 *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
 */
00172 class Sha1Digest : public Digest
{
public:

    /**
     *  Constructor
     */
00179     Sha1Digest()
        { hashType = HASH_SHA1; reset(); }

    /**
     *  Destructor
     */
00185     virtual ~Sha1Digest()
        { reset(); }

    /**
     *  Overloaded from Digest
     */
    virtual void reset();

    /**
     *  Overloaded from Digest
     */
    virtual std::vector<unsigned char> finish();

protected:

    /**
     *  Overloaded from Digest
     */
    virtual void update(unsigned char val);

private:

    void hashblock();

    unsigned long H[5];
    unsigned long W[80];
    unsigned long long size;
    int lenW;

};






/**
 *  SHA-224,
 *  Section 6.1, SECURE HASH STANDARD
 *  Federal Information Processing Standards Publication 180-2
 *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
 */
00227 class Sha224Digest : public Digest
{
public:

    /**
     *  Constructor
     */
00234     Sha224Digest()
        { hashType = HASH_SHA224; reset(); }

    /**
     *  Destructor
     */
00240     virtual ~Sha224Digest()
        { reset(); }

    /**
     *  Overloaded from Digest
     */
    virtual void reset();

    /**
     *  Overloaded from Digest
     */
    virtual std::vector<unsigned char> finish();

protected:

    /**
     *  Overloaded from Digest
     */
    virtual void update(unsigned char val);

private:

    void hashblock();

    unsigned long H[8];
    unsigned long W[64];
    unsigned long long size;
    int lenW;

};



/**
 *  SHA-256,
 *  Section 6.1, SECURE HASH STANDARD
 *  Federal Information Processing Standards Publication 180-2
 *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
 */
00279 class Sha256Digest : public Digest
{
public:

    /**
     *  Constructor
     */
00286     Sha256Digest()
        { hashType = HASH_SHA256; reset(); }

    /**
     *  Destructor
     */
00292     virtual ~Sha256Digest()
        { reset(); }

    /**
     *  Overloaded from Digest
     */
    virtual void reset();

    /**
     *  Overloaded from Digest
     */
    virtual std::vector<unsigned char> finish();

protected:

    /**
     *  Overloaded from Digest
     */
    virtual void update(unsigned char val);

private:

    void hashblock();

    unsigned long H[8];
    unsigned long W[64];
    unsigned long long size;
    int lenW;

};


/**
 *  SHA-384,
 *  Section 6.1, SECURE HASH STANDARD
 *  Federal Information Processing Standards Publication 180-2
 *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
 */
00330 class Sha384Digest : public Digest
{
public:

    /**
     *  Constructor
     */
00337     Sha384Digest()
        { hashType = HASH_SHA384; reset(); }

    /**
     *  Destructor
     */
00343     virtual ~Sha384Digest()
        { reset(); }

    /**
     *  Overloaded from Digest
     */
    virtual void reset();

    /**
     *  Overloaded from Digest
     */
    virtual std::vector<unsigned char> finish();

protected:

    /**
     *  Overloaded from Digest
     */
    virtual void update(unsigned char val);

private:

    void hashblock();

    unsigned long long H[8];
    unsigned long long W[80];
    unsigned long long size;
    int lenW;

};




/**
 *  SHA-512,
 *  Section 6.1, SECURE HASH STANDARD
 *  Federal Information Processing Standards Publication 180-2
 *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
 */
00383 class Sha512Digest : public Digest
{
public:

    /**
     *  Constructor
     */
00390     Sha512Digest()
        { hashType = HASH_SHA512; reset(); }

    /**
     *  Destructor
     */
00396     virtual ~Sha512Digest()
        { reset(); }

    /**
     *  Overloaded from Digest
     */
    virtual void reset();

    /**
     *  Overloaded from Digest
     */
    virtual std::vector<unsigned char> finish();

protected:

    /**
     *  Overloaded from Digest
     */
    virtual void update(unsigned char val);

private:

    void hashblock();

    unsigned long long H[8];
    unsigned long long W[80];
    unsigned long long size;
    int lenW;

};









/**
 * IETF RFC 1321, MD5 Specification
 * http://www.ietf.org/rfc/rfc1321.txt
 */
00439 class Md5Digest :  public Digest
{
public:

    /**
     *  Constructor
     */
00446     Md5Digest()
        { hashType = HASH_MD5; reset(); }

    /**
     *  Destructor
     */
00452     virtual ~Md5Digest()
        { reset(); }

    /**
     *  Overloaded from Digest
     */
    virtual void reset();

    /**
     *  Overloaded from Digest
     */
    virtual std::vector<unsigned char> finish();

protected:

    /**
     *  Overloaded from Digest
     */
    virtual void update(unsigned char val);

private:

    void hashblock();

    unsigned long hash[8];
    unsigned long W[64];
    unsigned long long size;
    int lenW;

};









#endif /*  __DIGEST_H__ */



Generated by  Doxygen 1.6.0   Back to index