⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hash.h

📁 Last change: 2008-02-03 This is the source code of KCeasy。
💻 H
字号:
/*
This file is part of KCeasy (http://www.kceasy.com)
Copyright (C) 2002-2004 Markus Kern <mkern@kceasy.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

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.  See the
GNU General Public License for more details.
*/
//---------------------------------------------------------------------------
#ifndef HashH
#define HashH

#include "istring.h"
//---------------------------------------------------------------------------

namespace KCeasyEngine {

// enum for various hash encodings
enum THashEncoding
{
    HASHENC_UNKNOWN = 0,
    HASHENC_BASE16, // RFC 3548
    HASHENC_BASE32, // RFC 3548
    HASHENC_BASE64  // RFC 3548
};

// class for all hash algos

class THashAlgo
{
public:
    THashAlgo(const THashAlgo* Algo);
    THashAlgo(const string& Name, THashEncoding Encoding = HASHENC_UNKNOWN);

    inline const string& GetName() const { return Name; }
    inline THashEncoding GetEncoding() const { return Encoding; }

    inline bool Equals(const THashAlgo* Cmp) const { return (Name == Cmp->Name && Encoding == Cmp->Encoding); }
    inline bool operator ==(const THashAlgo& Cmp) const { return Equals(&Cmp); };
    inline bool operator !=(const THashAlgo& Cmp) const { return !Equals(&Cmp); };

protected:
    string Name;
    THashEncoding Encoding;
};

// class representing a set of hash algos

class THashAlgoSet
{
public:
    typedef list<THashAlgo*>::const_iterator Iterator;

    THashAlgoSet();
    THashAlgoSet(const THashAlgoSet* org);
    ~THashAlgoSet();

    THashAlgo* GetAlgo(const string& Name) const;
    THashAlgo* GetFirstAlgo() const;
    void AddAlgo(THashAlgo* Algo);
    bool AddAlgo(const string Name, THashEncoding Encoding = HASHENC_UNKNOWN);
    bool RemoveAlgo(const string& Name);
    void Clear();

    int Size() const { return Algos.size(); }
    Iterator Begin() const { return Algos.begin(); }
    Iterator End() const { return Algos.end(); }

    // returns true of at least one algo matches
    bool Equals(const THashAlgoSet* Cmp) const;
    bool operator==(const THashAlgoSet& Cmp) const { return Equals(&Cmp); }
    bool operator!=(const THashAlgoSet& Cmp) const { return !Equals(&Cmp); }

    THashAlgoSet& operator=(const THashAlgoSet& Org);

private:
    list<THashAlgo*> Algos;
};

// class representing a specific hash

class THash
{
public:
    THash();
    THash(const THash* org);
    THash(const string& Name, THashEncoding Encoding, const string& EncStr);
    ~THash();

    const string& GetName() const { return Algo->GetName(); };
    THashEncoding GetEncoding() const { return Algo->GetEncoding(); };
    const THashAlgo* GetAlgo() const { return Algo; }

    // set/get for hash encoded with default encoding
    string GetEncoded() const;
    bool SetEncoded(const string& Name, THashEncoding Encoding, const string& EncStr);

    // set/get for urns (e.g. urn:md5:babe...dead)
    string GetUrn() const;
    bool SetUrn(const string& Urn);

    // set/get gift style MD5:babe...dead
    string GetGiftHash() const;
    bool SetGiftHash(const string& GiftHash);

    // compare hashes
    bool Equals(const THash* Cmp) const;
    bool operator==(const THash& Cmp) const { return Equals(&Cmp); }
    bool operator!=(const THash& Cmp) const { return !Equals(&Cmp); }

    THash& operator=(const THash& Org);

private:
    THashAlgo* Algo;
    // we keep the hash in encoded form for now
    string EncodedHash;
};


// class representing a set of hashes

class THashSet
{
public:
    typedef list<THash*>::const_iterator Iterator;

    THashSet();
    THashSet(const THashSet* org);
    ~THashSet();

    THash* GetHash(const string& Name) const;
    THash* GetFirstHash() const;
    void AddHash(THash* Hash);
    bool AddGiftHash(const string& GiftHash);
    bool RemoveHash(const string& Name);
    void JoinHashSet(const THashSet* HashSet);
    void Clear();

    int Size() const { return Hashes.size(); }
    Iterator Begin() const { return Hashes.begin(); }
    Iterator End() const { return Hashes.end(); }

    // returns true of at least one hash matches and there are no others which contradict
    bool Contains(const THash* Hash) const;
    bool Equals(const THashSet* Cmp) const;
    bool operator==(const THashSet& Cmp) const { return Equals(&Cmp); }
    bool operator!=(const THashSet& Cmp) const { return !Equals(&Cmp); }

    THashSet& operator=(const THashSet& org);

    string GetBitziUrl();

private:
    list<THash*> Hashes;
};

} // namespace KCeasyEngine

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -