万博存款为什么送东西:使用Crypto++ 5.5.2完成SHA加密
来源:百度文库 编辑:偶看新闻 时间:2024/03/29 07:32:05
使用Crypto++5.5.2完成SHA加密
由于我仅仅使用SHA512,所以示例代码仅包含了SHA512的代码,如果你需要其他的SHA加密,可以参考SHA512的实现!
使用Crypto+5.5.2需要注意的地方,已在 《使用Crypto++5.5.2完成RSA加解密》一文做了说明。如果你遇到任何问题,最好先查看该文。如果还有其他问题,欢迎给我留言!贴代码吧!
/************************ MySHA.h******************************/
#ifndef __MYSHA_H__
#define __MYSHA_H__
#include
using namespace std;
class CMySHA
{
public:
CMySHA(void);
virtual ~CMySHA(void);
virtual void CalculateDigest(string &Digest, conststring &Message) = 0;
virtual bool VerifyDigest(const string &Digest, conststring &Message) = 0;
protected:
void uCharToHex(char *Dst, constunsigned char *Src, const int SrcLen);
void HexTouChar(unsigned char *Dst,const char *Src, const int SrcLen);
};
#endif /* End of __MYSHA_H__ */
/************************ MySHA.cpp******************************/
#include
#include "MySHA.h"
using namespace std;
CMySHA::CMySHA(void)
{
}
CMySHA::~CMySHA(void)
{
}
void CMySHA::uCharToHex(char *Dst, const unsigned char *Src, const int SrcLen)
{
char *Temp = Dst;
int i;
if (Dst == NULL || Src == NULL || SrcLen <= 0)
{
return;
}
for (i = 0; i < SrcLen; ++i)
{
sprintf(Temp, "%02X",Src[i]);
Temp += 2;
}
return;
}
void CMySHA::HexTouChar(unsigned char *Dst, const char *Src, const int SrcLen)
{
unsigned char *Temp = Dst;
int i;
if (Dst == NULL || Src == NULL || (SrcLen & 1 != 0) ||SrcLen <= 0)
{
return;
}
for (i = 0; i < SrcLen; i +=2)
{
sscanf(Src + i, "%02X",Temp);
++Temp;
}
return;
}
/************************ MySHA512.h******************************/
#ifndef __MYSHA512_H__
#define __MYSHA512_H__
#include "MySHA.h"
class CMySHA512 : CMySHA
{
public:
void CalculateDigest(string &Digest, const string&Message);
bool VerifyDigest(const string &Digest, const string&Message);
};
#endif /* End of __MYSHA512_H__ */
/************************ MySHA512.cpp******************************/
#include
#include
#include
#include
#include
#include "MySHA512.h"
using namespace std;
using namespace CryptoPP;
void CMySHA512::CalculateDigest(string &Digest, const string &Message)
{
SHA512 sha512;
int DigestSize = sha512.DigestSize();
byte byDigest[DigestSize];
char strDigest[DigestSize*2 + 1];
sha512.CalculateDigest(byDigest, (const byte *)Message.c_str(),Message.size());
memset(strDigest, 0, sizeof(strDigest));
uCharToHex(strDigest, byDigest,DigestSize);
Digest = strDigest;
return;
}
bool CMySHA512::VerifyDigest(const string &Digest, const string&Message)
{
bool Result;
SHA512 sha512;
byte byDigest[sha512.DigestSize()];
HexTouChar(byDigest, Digest.c_str(),Digest.size());
Result = sha512.VerifyDigest(byDigest, (const byte*)Message.c_str(), Message.size());
return Result;
}
/************************ Main.cpp******************************/
#include
#include
#include
#include
#include
#include "MySHA512.h"
using namespace std;
using namespace CryptoPP;
int main(void)
{
bool Result = false;
CMySHA512 MySHA512;
string Message = "Hello, garry!";
string Digest;
MySHA512.CalculateDigest(Digest, Message);
cout << "The size of Digest = " <
Result = MySHA512.VerifyDigest(Digest, Message);
if (Result)
{
cout << "Verifysucceful." << endl;
}
else
{
cout << "Verify failed."<< endl;
}
return 0;
}
/************************ Makefile ******************************/
#==================================================================================
# Usage:
# $make compile andlink the program.
# $ make objs compile only (nolinking. Rarely used).
# $ make clean clean the objectivesand dependencies.
# $ make cleanall clean the objectives, dependencies andexecutable.
# $ make rebuild rebuild the program. The same asmake clean && make all.
#==================================================================================
# The executable file name.
PROGRAM := mysha2
# The directories in which source files reside.
SRCDIRS := . # current directory
# The source file types (headers excluded).
SRCEXTS := .cpp
# The flags used by the cpp (man cpp for more).
CPPFLAGS :=
# The compiling flags used only for C.
# If it is a C++ program, no need to set these flags.
# If it is a C and C++ merging program, set these flags for the C parts.
CFLAGS :=
CFLAGS +=
# The compiling flags used only for C++.
# If it is a C program, no need to set these flags.
# If it is a C and C++ merging program, set these flags for the C++ parts.
CXXFLAGS := -g -O2 -I./include
CXXFLAGS +=
# The library and the link options ( C and C++ common).
LDFLAGS := -L./lib -lcryptopp
LDFLAGS +=
## Implict Section: change the following only when necessary.
##=============================================================================
# The C program compiler. Uncomment it to specify yours explicitly.
#CC = gcc
# The C++ program compiler. Uncomment it to specify yours explicitly.
CXX = g++
# Uncomment the 2 lines to compile C programs as C++ ones.
CC = $(CXX)
CFLAGS = $(CXXFLAGS)
# The command used to delete file.
RM = rm -f
## Stable Section: usually no need to be changed. But you can add more.
##=============================================================================
SHELL = /bin/sh
SOURCES = $(foreach d,$(SRCDIRS),$(wildcard $(addprefix $(d)/*,$(SRCEXTS))))
OBJS = $(foreach x,$(SRCEXTS), \
$(patsubst %$(x),%.o,$(filter %$(x),$(SOURCES))))
DEPS = $(patsubst %.o,%.d,$(OBJS))
.PHONY : all objs clean cleanall rebuild
all : $(PROGRAM)
# Rules for creating the dependency files (.d).
#---------------------------------------------------
%.d : %.c
@$(CC) -MM -MD $(CFLAGS) $<
%.d : %.C
@$(CC) -MM -MD $(CXXFLAGS) $<
%.d : %.cc
@$(CC) -MM -MD $(CXXFLAGS) $<
%.d : %.cpp
@$(CC) -MM -MD $(CXXFLAGS) $<
%.d : %.CPP
@$(CC) -MM -MD $(CXXFLAGS) $<
%.d : %.c++
@$(CC) -MM -MD $(CXXFLAGS) $<
%.d : %.cp
@$(CC) -MM -MD $(CXXFLAGS) $<
%.d : %.cxx
@$(CC) -MM -MD $(CXXFLAGS) $<
# Rules for producing the objects.
#---------------------------------------------------
objs : $(OBJS)
%.o : %.c
$(CC) -c $(CPPFLAGS) $(CFLAGS) $<
%.o : %.C
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $<
%.o : %.cc
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $<
%.o : %.cpp
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $<
%.o : %.CPP
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $<
%.o : %.c++
$(CXX -c $(CPPFLAGS) $(CXXFLAGS) $<
%.o : %.cp
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $<
%.o : %.cxx
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $<
# Rules for producing the executable.
#----------------------------------------------
$(PROGRAM) : $(OBJS)
ifeq ($(strip $(SRCEXTS)), .c) # C file
$(CC) -o $(PROGRAM) $(OBJS) $(LDFLAGS)
else # C++ file
$(CXX) -o $(PROGRAM) $(OBJS) $(LDFLAGS)
endif
-include $(DEPS)
rebuild: clean all
clean :
@$(RM) *.o *.d
cleanall: clean
@$(RM) $(PROGRAM) $(PROGRAM).exe