redefrag codes of Hash
This commit is contained in:
@@ -23,23 +23,24 @@ namespace hgl
|
||||
|
||||
xxH32,
|
||||
xxH64,
|
||||
XXH3_64,
|
||||
XXH3_128,
|
||||
xxH3_64,
|
||||
xxH3_128,
|
||||
|
||||
FNV1a,
|
||||
// Murmur3,
|
||||
// City64,
|
||||
// City128,
|
||||
Murmur3,
|
||||
City32,
|
||||
City64,
|
||||
City128,
|
||||
|
||||
ENUM_CLASS_RANGE(Adler32,FNV1a)
|
||||
ENUM_CLASS_RANGE(Adler32,City128)
|
||||
};//enum HASH
|
||||
|
||||
/**
|
||||
* Hash编码结构模板
|
||||
*/
|
||||
template<int SIZE> struct HashCode
|
||||
template<size_t SIZE> struct HashCode
|
||||
{
|
||||
unsigned char code[SIZE];
|
||||
uint8 code[SIZE]{};
|
||||
|
||||
public:
|
||||
|
||||
@@ -48,39 +49,15 @@ namespace hgl
|
||||
|
||||
public:
|
||||
|
||||
HashCode()
|
||||
{
|
||||
hgl_zero(code);
|
||||
}
|
||||
static constexpr int size()noexcept{return SIZE;}
|
||||
|
||||
static constexpr int size(){return SIZE;}
|
||||
const uint8 operator[](int index)const{return code[index];}
|
||||
|
||||
const unsigned char operator[](int index)const
|
||||
{
|
||||
return code[index];
|
||||
}
|
||||
void CopyFrom (const void *ptr){memcpy(code,ptr,SIZE);}
|
||||
void FromString (const char *str){ParseHexStr(code,str,SIZE);}
|
||||
|
||||
void CopyFrom(const void *ptr)
|
||||
{
|
||||
memcpy(code,ptr,SIZE);
|
||||
}
|
||||
|
||||
void FromString(const char *str)
|
||||
{
|
||||
ParseHexStr(code,str,SIZE);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void ToUpperString(T *str,const T gap_char=0) const
|
||||
{
|
||||
ToUpperHexStr<T>(str,code,SIZE,gap_char);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void ToLowerString(T *str,const T gap_char=0) const
|
||||
{
|
||||
ToLowerHexStr<T>(str,code,SIZE,gap_char);
|
||||
}
|
||||
template<typename T> void ToUpperString(T *str,const T gap_char=0) const {ToUpperHexStr<T>(str,code,SIZE,gap_char);}
|
||||
template<typename T> void ToLowerString(T *str,const T gap_char=0) const {ToLowerHexStr<T>(str,code,SIZE,gap_char);}
|
||||
|
||||
const int CompFunc(const HashCode<SIZE> &hash)const
|
||||
{
|
||||
@@ -100,36 +77,23 @@ namespace hgl
|
||||
}
|
||||
|
||||
CompOperator(const HashCode<SIZE> &,CompFunc)
|
||||
};//template<int SIZE> struct HashCode
|
||||
|
||||
using HashCodeCRC32 =HashCode<4> ;
|
||||
using HashCodeAdler32 =HashCode<4> ;
|
||||
using HashCodeMD5 =HashCode<16> ;
|
||||
using HashCodeMD4 =HashCode<16> ;
|
||||
using HashCodeSHA1 =HashCode<20> ;
|
||||
using HashCodeSHA1LE =HashCode<20> ;
|
||||
using HashCodeSHA256 =HashCode<32> ;
|
||||
using HashCodeSHA512 =HashCode<64> ;
|
||||
using HashCodexxH32 =HashCode<4> ;
|
||||
using HashCodexxH64 =HashCode<8> ;
|
||||
using HashCodeXXH3_64 =HashCode<8> ;
|
||||
using HashCodeXXH3_128 =HashCode<16> ;
|
||||
using HashCodeFNV1a =HashCode<4>;
|
||||
|
||||
const int hash_code_bytes[]={4,4,16,16,20,20,32,64,4,8,16,4}; //hash码长度
|
||||
};//template<size_t SIZE> struct HashCode
|
||||
|
||||
/**
|
||||
* 散列值计算功能基类
|
||||
*/
|
||||
class Hash ///散列值计算功能基类
|
||||
{
|
||||
AnsiString hash_name;
|
||||
size_t hash_size;
|
||||
|
||||
public:
|
||||
|
||||
Hash(const size_t s,const AnsiString &n):hash_size(s),hash_name(n){}
|
||||
virtual ~Hash()=default;
|
||||
|
||||
virtual void GetName(UTF8String &)const=0; ///<取得HASH算法的名称
|
||||
virtual void GetName(UTF16String &)const=0; ///<取得HASH算法的名称
|
||||
virtual const int GetHashBytes()const=0; ///<取得HASH码字节长度(MD4/MD5为16,SHA1为20)
|
||||
|
||||
void GetName (AnsiString &name)const {name=hash_name;} ///<取得HASH算法的名称
|
||||
const size_t GetHashBytes()const noexcept {return hash_size;} ///<取得HASH码字节长度(MD4/MD5为16,SHA1为20)
|
||||
|
||||
virtual void Init()=0; ///<初始化散列值计算
|
||||
virtual void Update(const void *,uint)=0; ///<提交新的数据
|
||||
@@ -150,50 +114,7 @@ namespace hgl
|
||||
|
||||
template<HASH ha> Hash *CreateHash(); ///<创建一个hash值计算类实例
|
||||
|
||||
#define HGL_CREATE_HASH_FUNC(name) Hash *Create##name##Hash(); \
|
||||
template<> inline Hash *CreateHash<HASH::name>(){return Create##name##Hash();}
|
||||
|
||||
HGL_CREATE_HASH_FUNC(Adler32)
|
||||
HGL_CREATE_HASH_FUNC(CRC32)
|
||||
HGL_CREATE_HASH_FUNC(MD4)
|
||||
HGL_CREATE_HASH_FUNC(MD5)
|
||||
HGL_CREATE_HASH_FUNC(SHA1)
|
||||
HGL_CREATE_HASH_FUNC(SHA1LE)
|
||||
HGL_CREATE_HASH_FUNC(SHA256)
|
||||
HGL_CREATE_HASH_FUNC(SHA512)
|
||||
HGL_CREATE_HASH_FUNC(xxH32)
|
||||
HGL_CREATE_HASH_FUNC(xxH64)
|
||||
HGL_CREATE_HASH_FUNC(XXH3_64)
|
||||
HGL_CREATE_HASH_FUNC(XXH3_128)
|
||||
HGL_CREATE_HASH_FUNC(FNV1a)
|
||||
|
||||
//#undef HGL_CREATE_HASH_FUNC
|
||||
|
||||
inline Hash *CreateHash(HASH ha)
|
||||
{
|
||||
RANGE_CHECK_RETURN_NULLPTR(ha)
|
||||
|
||||
using CreateHashFunc=Hash *(*)();
|
||||
|
||||
const CreateHashFunc func[(size_t)HASH::RANGE_SIZE]=
|
||||
{
|
||||
CreateAdler32Hash,
|
||||
CreateCRC32Hash,
|
||||
CreateMD4Hash,
|
||||
CreateMD5Hash,
|
||||
CreateSHA1Hash,
|
||||
CreateSHA1LEHash,
|
||||
CreateSHA256Hash,
|
||||
CreateSHA512Hash,
|
||||
CreatexxH32Hash,
|
||||
CreatexxH64Hash,
|
||||
CreateXXH3_64Hash,
|
||||
CreateXXH3_128Hash,
|
||||
CreateFNV1aHash
|
||||
};
|
||||
|
||||
return func[(size_t)ha]();
|
||||
}
|
||||
Hash *CreateHash(const HASH ha); ///<创建一个hash值计算类实例
|
||||
|
||||
/**
|
||||
* 计算一段数据的Hash值
|
||||
@@ -220,141 +141,29 @@ namespace hgl
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算一段数据的Hash值
|
||||
* @param data 数据指针
|
||||
* @param size 数据长度
|
||||
* @param ha hash算法
|
||||
* @param hash_code 计算后的hash值存放处
|
||||
* 取得一个文件的hash值
|
||||
* @param filename 文件名
|
||||
* @param ha hash对象
|
||||
* @param hash_code 计算后的hash存放处
|
||||
* @return 是否计算成功
|
||||
*/
|
||||
inline bool CountHash(const void *data,int size,HASH ha,void *hash_code)
|
||||
bool GetFileHash(const OSString &filename,Hash *ha,void *hash_code);
|
||||
|
||||
template<typename ha> bool GetFileHash(const OSString &filename,void *hash_code,size_t &hash_size)
|
||||
{
|
||||
RANGE_CHECK_RETURN_FALSE(ha)
|
||||
if(!data||size<=0||!hash_code)return(false);
|
||||
|
||||
using CountHashFunc=bool (*)(const void *,int size,void *);
|
||||
|
||||
const CountHashFunc func[(size_t)HASH::RANGE_SIZE]=
|
||||
{
|
||||
CountHash<HASH::Adler32 >,
|
||||
CountHash<HASH::CRC32 >,
|
||||
CountHash<HASH::MD4 >,
|
||||
CountHash<HASH::MD5 >,
|
||||
CountHash<HASH::SHA1 >,
|
||||
CountHash<HASH::SHA1LE >,
|
||||
CountHash<HASH::SHA256 >,
|
||||
CountHash<HASH::SHA512 >,
|
||||
CountHash<HASH::xxH32 >,
|
||||
CountHash<HASH::xxH64 >,
|
||||
CountHash<HASH::XXH3_64 >,
|
||||
CountHash<HASH::XXH3_128>,
|
||||
CountHash<HASH::FNV1a >
|
||||
};
|
||||
|
||||
return func[(size_t)ha](data,size,hash_code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算一段数据的Hash值
|
||||
* @param data 数据指针
|
||||
* @param size 数据长度
|
||||
* @param ha hash算法
|
||||
* @param hash_str 计算后的hash值存放处
|
||||
* @param litter 小写字母
|
||||
* @return 是否计算成功
|
||||
*/
|
||||
template<HASH ha> bool CountHashStr(const void *data,int size,UTF8String &hash_str,bool litter=true)
|
||||
{
|
||||
if(!data||size<=0)return(false);
|
||||
|
||||
Hash *h=CreateHash<ha>();
|
||||
|
||||
if(!h)return(false);
|
||||
|
||||
const int hash_bytes=hash_code_bytes[(size_t)ha];
|
||||
|
||||
uint8 *hash_code=new uint8[hash_bytes];
|
||||
u8char *hash_code_str=new u8char[1+(hash_bytes<<1)];
|
||||
|
||||
h->Init();
|
||||
h->Update(data,size);
|
||||
h->Final(hash_code);
|
||||
|
||||
if(!GetFileHash(filename,h,hash_code))
|
||||
{
|
||||
delete h;
|
||||
return(false);
|
||||
}
|
||||
hash_size=h->GetHashBytes();
|
||||
delete h;
|
||||
|
||||
DataToHexStr(hash_code_str,hash_code,hash_bytes,litter?LowerHexChar:UpperHexChar);
|
||||
|
||||
hash_str.SetInstance(hash_code_str,hash_bytes<<1);
|
||||
|
||||
delete[] hash_code;
|
||||
return(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算一段数据的Hash值
|
||||
* @param data 数据指针
|
||||
* @param size 数据长度
|
||||
* @param ha hash算法
|
||||
* @param hash_str 计算后的hash值存放处
|
||||
* @param litter 小写字母
|
||||
* @return 是否计算成功
|
||||
*/
|
||||
inline bool CountHash(const void *data,int size,HASH ha,UTF8String &hash_str,bool litter=true)
|
||||
{
|
||||
RANGE_CHECK_RETURN_FALSE(ha)
|
||||
if(!data||size<=0)return(false);
|
||||
|
||||
using CountHashFunc=bool (*)(const void *,int size,UTF8String &,bool);
|
||||
|
||||
const CountHashFunc func[(size_t)HASH::RANGE_SIZE]=
|
||||
{
|
||||
CountHashStr<HASH::Adler32 >,
|
||||
CountHashStr<HASH::CRC32 >,
|
||||
CountHashStr<HASH::MD4 >,
|
||||
CountHashStr<HASH::MD5 >,
|
||||
CountHashStr<HASH::SHA1 >,
|
||||
CountHashStr<HASH::SHA1LE >,
|
||||
CountHashStr<HASH::SHA256 >,
|
||||
CountHashStr<HASH::SHA512 >,
|
||||
CountHashStr<HASH::xxH32 >,
|
||||
CountHashStr<HASH::xxH64 >,
|
||||
CountHashStr<HASH::XXH3_64 >,
|
||||
CountHashStr<HASH::XXH3_128 >,
|
||||
CountHashStr<HASH::FNV1a >
|
||||
};
|
||||
|
||||
return func[(size_t)ha](data,size,hash_str,litter);
|
||||
}
|
||||
|
||||
#define HGL_COUNT_HASH_FUNC(name) inline bool Count##name(const void *data, int size, HashCode##name &hc) { return CountHash<HASH::name>(data, size, &hc); } \
|
||||
inline bool Count##name(const void *data, int size, UTF8String &hash_str, bool litter = true) { return CountHashStr<HASH::name>(data, size, hash_str, litter); } \
|
||||
inline bool Count##name(const UTF8String &str, UTF8String &hash_str, bool litter = true) { return CountHashStr<HASH::name>(str.c_str(), str.Length(), hash_str, litter); }
|
||||
|
||||
HGL_COUNT_HASH_FUNC(Adler32)
|
||||
HGL_COUNT_HASH_FUNC(CRC32)
|
||||
HGL_COUNT_HASH_FUNC(MD4)
|
||||
HGL_COUNT_HASH_FUNC(MD5)
|
||||
HGL_COUNT_HASH_FUNC(SHA1)
|
||||
HGL_COUNT_HASH_FUNC(SHA1LE)
|
||||
HGL_COUNT_HASH_FUNC(SHA256)
|
||||
HGL_COUNT_HASH_FUNC(SHA512)
|
||||
HGL_COUNT_HASH_FUNC(xxH32)
|
||||
HGL_COUNT_HASH_FUNC(xxH64)
|
||||
HGL_COUNT_HASH_FUNC(XXH3_64)
|
||||
HGL_COUNT_HASH_FUNC(XXH3_128)
|
||||
HGL_COUNT_HASH_FUNC(FNV1a)
|
||||
|
||||
#undef HGL_COUNT_HASH_FUNC
|
||||
|
||||
/**
|
||||
* 取得一个文件的hash值
|
||||
* @param filename 文件名
|
||||
* @param ha hash算法
|
||||
* @param hash_code 计算后的hash存放处
|
||||
* @return 是否计算成功
|
||||
*/
|
||||
bool GetFileHash(const OSString &filename,HASH ha,void *hash_code);
|
||||
|
||||
/**
|
||||
* 取得一个文件的hash值
|
||||
* @param filename 文件名
|
||||
@@ -363,7 +172,7 @@ namespace hgl
|
||||
* @param litter 小写字母
|
||||
* @return 是否计算成功
|
||||
*/
|
||||
bool GetFileHash(const OSString &filename,HASH ha,UTF8String &hash_str,bool litter=true);
|
||||
//bool GetFileHash(const OSString &filename,HASH ha,UTF8String &hash_str,bool litter=true);
|
||||
}//namespace util
|
||||
}//namespace hgl
|
||||
#endif//HGL_UTIL_HASH_INCLUDE
|
||||
|
Reference in New Issue
Block a user