acl/lib_acl_cpp/include/acl_cpp/stdlib/md5.hpp

135 lines
4.1 KiB
C++
Raw Normal View History

2014-11-19 00:25:21 +08:00
#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
#include "noncopyable.hpp"
2014-11-19 00:25:21 +08:00
namespace acl {
class istream;
class ACL_CPP_API md5 : public noncopyable
2014-11-19 00:25:21 +08:00
{
public:
md5(void);
~md5(void);
2014-11-19 00:25:21 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD> md5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param dat {const void*} <EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>ַ
* @param len {size_t} dat <EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
* @return {md5&}
*/
md5& update(const void* dat, size_t len);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ md5 <EFBFBD><EFBFBD><EFBFBD>̽<EFBFBD><EFBFBD><EFBFBD>
* @return {md5&}
*/
md5& finish(void);
2014-11-19 00:25:21 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> md5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>ʹ<EFBFBD><EFBFBD>ͬһ<EFBFBD><EFBFBD> md5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {md5&}
*/
md5& reset(void);
2014-11-19 00:25:21 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƹ<EFBFBD>ʽ<EFBFBD><EFBFBD> md5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
* @return {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>Զ<EFBFBD>ǿգ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 16 <EFBFBD>ֽ<EFBFBD>
*/
const char* get_digest() const;
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD> m5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
* @return {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>Զ<EFBFBD>ǿգ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> \0 <EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 32 <EFBFBD>ֽ<EFBFBD>
*/
const char* get_string() const;
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> md5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǩ<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD> 128 λ (<EFBFBD><EFBFBD> 16 <EFBFBD>ֽ<EFBFBD>) <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><EFBFBD><EFBFBD>
* @param dat {const void*} Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param dlen {size_t} dat <EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
* @param key {const char*} <EFBFBD>ǿ<EFBFBD>ʱ<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param klen {size_t} key <EFBFBD>ǿ<EFBFBD>ʱ<EFBFBD><EFBFBD>ʾ key <EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD>
* @param out {void*} <EFBFBD> md5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param size {size_t} out <EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>Ϊ 16 <EFBFBD>ֽ<EFBFBD>
* @return {const char*} <EFBFBD><EFBFBD><EFBFBD>ش<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ(<EFBFBD><EFBFBD> out <EFBFBD><EFBFBD>ַ)
*/
static const char* md5_digest(const void *dat, size_t dlen,
const void *key, size_t klen, void* out, size_t size);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> md5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǩ<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD>
* @param dat {const void*} Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param dlen {size_t} dat <EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
* @param key {const char*} <EFBFBD>ǿ<EFBFBD>ʱ<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param klen {size_t} key <EFBFBD>ǿ<EFBFBD>ʱ<EFBFBD><EFBFBD>ʾ key <EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD>
* @param out {void*} <EFBFBD> md5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param size {size_t} out <EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>Ϊ 33 <EFBFBD>ֽ<EFBFBD>
* @return {const char*} <EFBFBD><EFBFBD><EFBFBD>ش<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ(<EFBFBD><EFBFBD> out <EFBFBD><EFBFBD>ַ)<EFBFBD><EFBFBD>
* <EFBFBD>ҷ<EFBFBD><EFBFBD><EFBFBD>ֵΪ<EFBFBD><EFBFBD> \0 <EFBFBD><EFBFBD>β<EFBFBD><EFBFBD> 32 <EFBFBD>ֽڳ<EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD> \0)<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
*/
static const char* md5_string(const void *dat, size_t dlen,
const void *key, size_t klen, char* out, size_t size);
/**
* <EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> md5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǩ<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param path {const char*} <EFBFBD>ļ<EFBFBD>ȫ·<EFBFBD><EFBFBD>
* @param key {const char*} <EFBFBD>ǿ<EFBFBD>ʱ<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param klen {size_t} key <EFBFBD>ǿ<EFBFBD>ʱ<EFBFBD><EFBFBD>ʾ key <EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD>
* @param out {void*} <EFBFBD> md5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param size {size_t} out <EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>Ϊ 33 <EFBFBD>ֽ<EFBFBD>
* @return {int64) <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵij<EFBFBD><EFBFBD>ȣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD> -1
* 1) <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>ʧ<EFBFBD><EFBFBD>
* 2) δ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* 3) out <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С size С<EFBFBD><EFBFBD> 33 <EFBFBD>ֽڳ<EFBFBD><EFBFBD><EFBFBD>
*/
#if defined(_WIN32) || defined(_WIN64)
2014-11-19 00:25:21 +08:00
static __int64 md5_file(const char* path, const void *key,
size_t klen, char* out, size_t size);
#else
static long long int md5_file(const char* path, const void *key,
size_t klen, char* out, size_t size);
#endif
/**
* <EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> md5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǩ<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param in {istream&} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>
* @param key {const char*} <EFBFBD>ǿ<EFBFBD>ʱ<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param klen {size_t} key <EFBFBD>ǿ<EFBFBD>ʱ<EFBFBD><EFBFBD>ʾ key <EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD>
* @param out {void*} <EFBFBD> md5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param size {size_t} out <EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>Ϊ 33 <EFBFBD>ֽ<EFBFBD>
* @return {int64) <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵij<EFBFBD><EFBFBD>ȣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD> -1:
* 1) δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
* 2) out <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С size С<EFBFBD><EFBFBD> 33 <EFBFBD>ֽڳ<EFBFBD><EFBFBD><EFBFBD>
*/
#if defined(_WIN32) || defined(_WIN64)
2014-11-19 00:25:21 +08:00
static __int64 md5_file(istream& in, const void *key,
size_t klen, char* out, size_t size);
#else
static long long int md5_file(istream& in, const void *key,
size_t klen, char* out, size_t size);
#endif
/**
* <EFBFBD><EFBFBD> 16 <EFBFBD>ֽڳ<EFBFBD><EFBFBD>ȵ<EFBFBD> MD5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>Ϊ 32 <EFBFBD>ֽڳ<EFBFBD><EFBFBD>ȵ<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
* @param in {const void*} 128 λ(<EFBFBD><EFBFBD> 16 <EFBFBD>ֽ<EFBFBD>)<EFBFBD><EFBFBD> md5 ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> in <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD> >= 16<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD>
* @param out {char*} <EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD>
* @param size {size_t} out <EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 33 <EFBFBD>ֽڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {const char*} <EFBFBD><EFBFBD><EFBFBD>ش<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ(<EFBFBD><EFBFBD> out <EFBFBD><EFBFBD>ַ)<EFBFBD><EFBFBD>
* <EFBFBD>ҷ<EFBFBD><EFBFBD><EFBFBD>ֵΪ<EFBFBD><EFBFBD> \0 <EFBFBD><EFBFBD>β<EFBFBD><EFBFBD> 32 <EFBFBD>ֽڳ<EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD> \0)<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
*/
static const char* hex_encode(const void *in, char* out, size_t size);
private:
unsigned int buf_[4];
unsigned int bytes_[2];
unsigned int in_[16];
unsigned char digest_[16];
unsigned char digest_s_[33];
};
} // namespace acl