From 6b12fdeaab472f0397e8c74067ae89230b040731 Mon Sep 17 00:00:00 2001 From: bengin Date: Wed, 13 Jun 2012 16:52:41 +0000 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BF=AE=E5=A4=8DNetService=E7=9A=84?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E9=94=99=E8=AF=AF=202.=20=E9=80=82=E5=BA=94?= =?UTF-8?q?=E7=99=BB=E9=99=86=E5=8D=8F=E8=AE=AE=E9=94=99=E8=AF=AF=E7=9A=84?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=EF=BC=8C=E5=81=9A=E4=BA=86=E4=BA=9B=E6=94=B9?= =?UTF-8?q?=E5=8A=A8=203.=20=E5=AF=86=E7=A0=81=E5=81=9A=E4=BA=86SHA1?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=EF=BC=8C=E6=89=8D=E4=BC=A0=E7=BB=99=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://subversion.assembla.com/svn/vnoc/trunk@255 36a5fca6-8feb-4701-ac75-490187a78786 --- NetService/ServerPostBuild.bat | 6 +- VNOC/VNOC.sln | 28 +- VNOC/VNOC/VNOC.vcxproj | 6 + VNOC/VNOC/VNOC.vcxproj.filters | 9 + VNOC/VNOC/VNOCLoginDlg.cpp | 25 +- sha1/Makefile | 41 +++ sha1/Makefile.nt | 48 +++ sha1/license.txt | 14 + sha1/sha.cpp | 176 ++++++++++ sha1/sha1.cpp | 589 +++++++++++++++++++++++++++++++++ sha1/sha1.h | 89 +++++ sha1/shacmp.cpp | 169 ++++++++++ sha1/shatest.cpp | 149 +++++++++ 13 files changed, 1326 insertions(+), 23 deletions(-) create mode 100644 sha1/Makefile create mode 100644 sha1/Makefile.nt create mode 100644 sha1/license.txt create mode 100644 sha1/sha.cpp create mode 100644 sha1/sha1.cpp create mode 100644 sha1/sha1.h create mode 100644 sha1/shacmp.cpp create mode 100644 sha1/shatest.cpp diff --git a/NetService/ServerPostBuild.bat b/NetService/ServerPostBuild.bat index a4607040..ba150f91 100644 --- a/NetService/ServerPostBuild.bat +++ b/NetService/ServerPostBuild.bat @@ -1,14 +1,14 @@ @echo off -@cd .. setlocal EnableDelayedExpansion @if "%1"=="RELEASE" goto gRelease -; +cd +xcopy /e/c/y "../VisualLeakDetector/bin/win32" "Debug" > nul goto gEnd :gRelease -; +@xcopy /e/c/y "../VisualLeakDetector/bin/win32" "Release" > nul :gEnd @echo "Post Dispatch OK!" \ No newline at end of file diff --git a/VNOC/VNOC.sln b/VNOC/VNOC.sln index 7dca99be..0e41a9ae 100644 --- a/VNOC/VNOC.sln +++ b/VNOC/VNOC.sln @@ -5,11 +5,11 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VNOC", "VNOC\VNOC.vcxproj", EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MyNetClient", "..\MyNetClient\MyNetClient.vcxproj", "{BF0C816A-FB56-47B1-84BB-881F6E320887}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cppunit_vs10", "..\cppunit-1.12.1\src\cppunit\cppunit_vs10.vcxproj", "{F80B2DA6-C279-B8DA-5E33-D58D3486560F}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cppunit_vs10", "..\cppunit-1.12.1\src\cppunit\cppunit_vs10.vcxproj", "{85AFC928-BE19-F7CA-113B-8D128D323A90}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "testRunner", "VNOC\testRunner\testRunner.vcxproj", "{5857152D-B7F5-4175-AD8C-DEA94DEDA32B}" ProjectSection(ProjectDependencies) = postProject - {F80B2DA6-C279-B8DA-5E33-D58D3486560F} = {F80B2DA6-C279-B8DA-5E33-D58D3486560F} + {85AFC928-BE19-F7CA-113B-8D128D323A90} = {85AFC928-BE19-F7CA-113B-8D128D323A90} EndProjectSection EndProject Global @@ -39,18 +39,18 @@ Global {BF0C816A-FB56-47B1-84BB-881F6E320887}.Release|x64.ActiveCfg = Release|Win32 {BF0C816A-FB56-47B1-84BB-881F6E320887}.VldMemLeakCheck|Win32.ActiveCfg = Release|Win32 {BF0C816A-FB56-47B1-84BB-881F6E320887}.VldMemLeakCheck|x64.ActiveCfg = Release|Win32 - {F80B2DA6-C279-B8DA-5E33-D58D3486560F}.Debug|Win32.ActiveCfg = Debug|Win32 - {F80B2DA6-C279-B8DA-5E33-D58D3486560F}.Debug|Win32.Build.0 = Debug|Win32 - {F80B2DA6-C279-B8DA-5E33-D58D3486560F}.Debug|x64.ActiveCfg = Debug|x64 - {F80B2DA6-C279-B8DA-5E33-D58D3486560F}.Debug|x64.Build.0 = Debug|x64 - {F80B2DA6-C279-B8DA-5E33-D58D3486560F}.Release|Win32.ActiveCfg = Release|Win32 - {F80B2DA6-C279-B8DA-5E33-D58D3486560F}.Release|Win32.Build.0 = Release|Win32 - {F80B2DA6-C279-B8DA-5E33-D58D3486560F}.Release|x64.ActiveCfg = Release|x64 - {F80B2DA6-C279-B8DA-5E33-D58D3486560F}.Release|x64.Build.0 = Release|x64 - {F80B2DA6-C279-B8DA-5E33-D58D3486560F}.VldMemLeakCheck|Win32.ActiveCfg = Debug|Win32 - {F80B2DA6-C279-B8DA-5E33-D58D3486560F}.VldMemLeakCheck|Win32.Build.0 = Debug|Win32 - {F80B2DA6-C279-B8DA-5E33-D58D3486560F}.VldMemLeakCheck|x64.ActiveCfg = Template|x64 - {F80B2DA6-C279-B8DA-5E33-D58D3486560F}.VldMemLeakCheck|x64.Build.0 = Template|x64 + {85AFC928-BE19-F7CA-113B-8D128D323A90}.Debug|Win32.ActiveCfg = Debug|Win32 + {85AFC928-BE19-F7CA-113B-8D128D323A90}.Debug|Win32.Build.0 = Debug|Win32 + {85AFC928-BE19-F7CA-113B-8D128D323A90}.Debug|x64.ActiveCfg = Debug|x64 + {85AFC928-BE19-F7CA-113B-8D128D323A90}.Debug|x64.Build.0 = Debug|x64 + {85AFC928-BE19-F7CA-113B-8D128D323A90}.Release|Win32.ActiveCfg = Release|Win32 + {85AFC928-BE19-F7CA-113B-8D128D323A90}.Release|Win32.Build.0 = Release|Win32 + {85AFC928-BE19-F7CA-113B-8D128D323A90}.Release|x64.ActiveCfg = Release|x64 + {85AFC928-BE19-F7CA-113B-8D128D323A90}.Release|x64.Build.0 = Release|x64 + {85AFC928-BE19-F7CA-113B-8D128D323A90}.VldMemLeakCheck|Win32.ActiveCfg = Debug|Win32 + {85AFC928-BE19-F7CA-113B-8D128D323A90}.VldMemLeakCheck|Win32.Build.0 = Debug|Win32 + {85AFC928-BE19-F7CA-113B-8D128D323A90}.VldMemLeakCheck|x64.ActiveCfg = Template|x64 + {85AFC928-BE19-F7CA-113B-8D128D323A90}.VldMemLeakCheck|x64.Build.0 = Template|x64 {5857152D-B7F5-4175-AD8C-DEA94DEDA32B}.Debug|Win32.ActiveCfg = Debug|Win32 {5857152D-B7F5-4175-AD8C-DEA94DEDA32B}.Debug|Win32.Build.0 = Debug|Win32 {5857152D-B7F5-4175-AD8C-DEA94DEDA32B}.Debug|x64.ActiveCfg = Debug|Win32 diff --git a/VNOC/VNOC/VNOC.vcxproj b/VNOC/VNOC/VNOC.vcxproj index c91fb2a3..da83c548 100644 --- a/VNOC/VNOC/VNOC.vcxproj +++ b/VNOC/VNOC/VNOC.vcxproj @@ -196,6 +196,11 @@ NotUsing NotUsing + + NotUsing + NotUsing + NotUsing + NotUsing NotUsing @@ -267,6 +272,7 @@ + diff --git a/VNOC/VNOC/VNOC.vcxproj.filters b/VNOC/VNOC/VNOC.vcxproj.filters index ad2e4314..31b52e10 100644 --- a/VNOC/VNOC/VNOC.vcxproj.filters +++ b/VNOC/VNOC/VNOC.vcxproj.filters @@ -58,6 +58,9 @@ {b2fd2961-0b64-4e90-adc9-8ff245d8b7a6} + + {c02f1cda-9213-4df7-932a-10b0be8ed19e} + @@ -159,6 +162,9 @@ ErrorMessage + + util\sha1 + @@ -314,6 +320,9 @@ ErrorMessage + + util\sha1 + diff --git a/VNOC/VNOC/VNOCLoginDlg.cpp b/VNOC/VNOC/VNOCLoginDlg.cpp index 047e7fa3..c81ddeb9 100644 --- a/VNOC/VNOC/VNOCLoginDlg.cpp +++ b/VNOC/VNOC/VNOCLoginDlg.cpp @@ -8,6 +8,8 @@ #include "Config.h" #include "INet.h" +#include "../../sha1/sha1.h" + #ifdef _DEBUG #define new DEBUG_NEW #endif @@ -25,7 +27,7 @@ CVNOCLoginDlg::CVNOCLoginDlg(CWnd* pParent /*=NULL*/) { m_bVerifying = FALSE; m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); - m_nlHelper.AddFilter(MSG_AVC_TYPE,this); + m_nlHelper.AddFilter(MSG_ALI_TYPE,this); m_nlHelper.StartListen(); } @@ -105,7 +107,7 @@ HCURSOR CVNOCLoginDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } - +#include void CVNOCLoginDlg::OnBnClickedOk() { @@ -116,16 +118,27 @@ void CVNOCLoginDlg::OnBnClickedOk() OnOK(); } + SHA1 shaer; + shaer.Reset(); + + CStringA pwdBuffer = CT2A(m_strPassword); + shaer.Input(pwdBuffer,pwdBuffer.GetLength()); + UINT pResult[5]; + shaer.Result(pResult); + pwdBuffer.Format("%08x%08x%08x%08x%08x" + ,pResult[0],pResult[1],pResult[2],pResult[3],pResult[4]); + Global->Logf(LogFile_General,_T("SHA1后的密码为:%s\n"),CA2T(pwdBuffer)); + INetCenter *pInet=NULL; Global->GetINetCenter(&pInet); ATLASSERT(pInet); if (pInet) { MSG_RLI mRli; - mRli.SetAccountNumber((const byte*)(LPCTSTR)m_strUsername,m_strUsername.GetLength()*sizeof(TCHAR)); - mRli.SetPassword((const byte*)(LPCTSTR)m_strPassword,m_strUsername.GetLength()*sizeof(TCHAR)); + mRli.SetAccountNumber((byte*)(LPCTSTR)m_strUsername,m_strUsername.GetLength()*sizeof(TCHAR)); + mRli.SetPassword((byte*)(LPCSTR)pwdBuffer,pwdBuffer.GetLength()*sizeof(TCHAR)); pInet->SendServer(mRli); - //mRli.SetVerificationCode() + _SetVerifyState(TRUE); SetTimer(0,5000,NULL); } @@ -164,13 +177,13 @@ HRESULT CVNOCLoginDlg::OnMessage( const CMessage& msg ) const MSG_ALI* ma = dynamic_cast(&msg); if (ma->GetLoginResult() == 1) { - _SetVerifyState(FALSE); OnOK(); } else { MessageBox(_T("登陆失败,服务器拒绝登陆 .")); } + _SetVerifyState(FALSE); } return S_OK; } diff --git a/sha1/Makefile b/sha1/Makefile new file mode 100644 index 00000000..66aaf2b5 --- /dev/null +++ b/sha1/Makefile @@ -0,0 +1,41 @@ +# +# Makefile +# +# Copyright (C) 1998, 2009 +# Paul E. Jones +# All Rights Reserved. +# +############################################################################# +# $Id: Makefile 12 2009-06-22 19:34:25Z paulej $ +############################################################################# +# +# Description: +# This is a makefile for UNIX to build the programs sha, shacmp, and +# shatest +# +# + +CC = g++ + +CFLAGS = -c -O2 -Wall -D_FILE_OFFSET_BITS=64 + +LIBS = + +OBJS = sha1.o + +all: sha shacmp shatest + +sha: sha.o $(OBJS) + $(CC) -o $@ sha.o $(OBJS) $(LIBS) + +shacmp: shacmp.o $(OBJS) + $(CC) -o $@ shacmp.o $(OBJS) $(LIBS) + +shatest: shatest.o $(OBJS) + $(CC) -o $@ shatest.o $(OBJS) $(LIBS) + +%.o: %.cpp + $(CC) $(CFLAGS) -o $@ $< + +clean: + $(RM) *.o sha shacmp shatest diff --git a/sha1/Makefile.nt b/sha1/Makefile.nt new file mode 100644 index 00000000..9bacf64a --- /dev/null +++ b/sha1/Makefile.nt @@ -0,0 +1,48 @@ +# +# Makefile.nt +# +# Copyright (C) 1998, 2009 +# Paul E. Jones +# All Rights Reserved. +# +############################################################################# +# $Id: Makefile.nt 13 2009-06-22 20:20:32Z paulej $ +############################################################################# +# +# Description: +# This is a makefile for Win32 to build the programs sha, shacmp, and +# shatest +# +# Portability Issues: +# Designed to work with Visual C++ +# +# + +.silent: + +!include + +RM = del /q + +LIBS = $(conlibs) setargv.obj + +CFLAGS = -D _CRT_SECURE_NO_WARNINGS /EHsc /O2 /W3 + +OBJS = sha1.obj + +all: sha.exe shacmp.exe shatest.exe + +sha.exe: sha.obj $(OBJS) + $(link) $(conflags) -out:$@ sha.obj $(OBJS) $(LIBS) + +shacmp.exe: shacmp.obj $(OBJS) + $(link) $(conflags) -out:$@ shacmp.obj $(OBJS) $(LIBS) + +shatest.exe: shatest.obj $(OBJS) + $(link) $(conflags) -out:$@ shatest.obj $(OBJS) $(LIBS) + +.cpp.obj: + $(cc) $(CFLAGS) $(cflags) $(cvars) $< + +clean: + $(RM) *.obj sha.exe shacmp.exe shatest.exe diff --git a/sha1/license.txt b/sha1/license.txt new file mode 100644 index 00000000..8d7f3941 --- /dev/null +++ b/sha1/license.txt @@ -0,0 +1,14 @@ +Copyright (C) 1998, 2009 +Paul E. Jones + +Freeware Public License (FPL) + +This software is licensed as "freeware." Permission to distribute +this software in source and binary forms, including incorporation +into other products, is hereby granted without a fee. THIS SOFTWARE +IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESSED OR IMPLIED WARRANTIES, +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHOR SHALL NOT BE HELD +LIABLE FOR ANY DAMAGES RESULTING FROM THE USE OF THIS SOFTWARE, EITHER +DIRECTLY OR INDIRECTLY, INCLUDING, BUT NOT LIMITED TO, LOSS OF DATA +OR DATA BEING RENDERED INACCURATE. diff --git a/sha1/sha.cpp b/sha1/sha.cpp new file mode 100644 index 00000000..ad860863 --- /dev/null +++ b/sha1/sha.cpp @@ -0,0 +1,176 @@ +/* + * sha.cpp + * + * Copyright (C) 1998, 2009 + * Paul E. Jones + * All Rights Reserved + * + ***************************************************************************** + * $Id: sha.cpp 13 2009-06-22 20:20:32Z paulej $ + ***************************************************************************** + * + * Description: + * This utility will display the message digest (fingerprint) for + * the specified file(s). + * + * Portability Issues: + * None. + */ + +#include +#include +#ifdef WIN32 +#include +#endif +#include +#include "sha1.h" + +/* + * Function prototype + */ +void usage(); + + +/* + * main + * + * Description: + * This is the entry point for the program + * + * Parameters: + * argc: [in] + * This is the count of arguments in the argv array + * argv: [in] + * This is an array of filenames for which to compute message digests + * + * Returns: + * Nothing. + * + * Comments: + * + */ +int main(int argc, char *argv[]) +{ + SHA1 sha; // SHA-1 class + FILE *fp; // File pointer for reading files + char c; // Character read from file + unsigned message_digest[5]; // Message digest from "sha" + int i; // Counter + bool reading_stdin; // Are we reading standard in? + bool read_stdin = false; // Have we read stdin? + + /* + * Check the program arguments and print usage information if -? + * or --help is passed as the first argument. + */ + if (argc > 1 && (!strcmp(argv[1],"-?") || !strcmp(argv[1],"--help"))) + { + usage(); + return 1; + } + + /* + * For each filename passed in on the command line, calculate the + * SHA-1 value and display it. + */ + for(i = 0; i < argc; i++) + { + /* + * We start the counter at 0 to guarantee entry into the for loop. + * So if 'i' is zero, we will increment it now. If there is no + * argv[1], we will use STDIN below. + */ + if (i == 0) + { + i++; + } + + if (argc == 1 || !strcmp(argv[i],"-")) + { +#ifdef WIN32 + _setmode(_fileno(stdin), _O_BINARY); +#endif + fp = stdin; + reading_stdin = true; + } + else + { + if (!(fp = fopen(argv[i],"rb"))) + { + fprintf(stderr, "sha: unable to open file %s\n", argv[i]); + return 2; + } + reading_stdin = false; + } + + /* + * We do not want to read STDIN multiple times + */ + if (reading_stdin) + { + if (read_stdin) + { + continue; + } + + read_stdin = true; + } + + /* + * Reset the SHA1 object and process input + */ + sha.Reset(); + + c = fgetc(fp); + while(!feof(fp)) + { + sha.Input(c); + c = fgetc(fp); + } + + if (!reading_stdin) + { + fclose(fp); + } + + if (!sha.Result(message_digest)) + { + fprintf(stderr,"sha: could not compute message digest for %s\n", + reading_stdin?"STDIN":argv[i]); + } + else + { + printf( "%08X %08X %08X %08X %08X - %s\n", + message_digest[0], + message_digest[1], + message_digest[2], + message_digest[3], + message_digest[4], + reading_stdin?"STDIN":argv[i]); + } + } + + return 0; +} + +/* + * usage + * + * Description: + * This function will display program usage information to the user. + * + * Parameters: + * None. + * + * Returns: + * Nothing. + * + * Comments: + * + */ +void usage() +{ + printf("usage: sha [ ...]\n"); + printf("\tThis program will display the message digest (fingerprint)\n"); + printf("\tfor files using the Secure Hashing Algorithm (SHA-1).\n"); +} diff --git a/sha1/sha1.cpp b/sha1/sha1.cpp new file mode 100644 index 00000000..fdcbdcc0 --- /dev/null +++ b/sha1/sha1.cpp @@ -0,0 +1,589 @@ +/* + * sha1.cpp + * + * Copyright (C) 1998, 2009 + * Paul E. Jones + * All Rights Reserved. + * + ***************************************************************************** + * $Id: sha1.cpp 12 2009-06-22 19:34:25Z paulej $ + ***************************************************************************** + * + * Description: + * This class implements the Secure Hashing Standard as defined + * in FIPS PUB 180-1 published April 17, 1995. + * + * The Secure Hashing Standard, which uses the Secure Hashing + * Algorithm (SHA), produces a 160-bit message digest for a + * given data stream. In theory, it is highly improbable that + * two messages will produce the same message digest. Therefore, + * this algorithm can serve as a means of providing a "fingerprint" + * for a message. + * + * Portability Issues: + * SHA-1 is defined in terms of 32-bit "words". This code was + * written with the expectation that the processor has at least + * a 32-bit machine word size. If the machine word size is larger, + * the code should still function properly. One caveat to that + * is that the input functions taking characters and character arrays + * assume that only 8 bits of information are stored in each character. + * + * Caveats: + * SHA-1 is designed to work with messages less than 2^64 bits long. + * Although SHA-1 allows a message digest to be generated for + * messages of any number of bits less than 2^64, this implementation + * only works with messages with a length that is a multiple of 8 + * bits. + * + */ + + +#include "sha1.h" + +/* + * SHA1 + * + * Description: + * This is the constructor for the sha1 class. + * + * Parameters: + * None. + * + * Returns: + * Nothing. + * + * Comments: + * + */ +SHA1::SHA1() +{ + Reset(); +} + +/* + * ~SHA1 + * + * Description: + * This is the destructor for the sha1 class + * + * Parameters: + * None. + * + * Returns: + * Nothing. + * + * Comments: + * + */ +SHA1::~SHA1() +{ + // The destructor does nothing +} + +/* + * Reset + * + * Description: + * This function will initialize the sha1 class member variables + * in preparation for computing a new message digest. + * + * Parameters: + * None. + * + * Returns: + * Nothing. + * + * Comments: + * + */ +void SHA1::Reset() +{ + Length_Low = 0; + Length_High = 0; + Message_Block_Index = 0; + + H[0] = 0x67452301; + H[1] = 0xEFCDAB89; + H[2] = 0x98BADCFE; + H[3] = 0x10325476; + H[4] = 0xC3D2E1F0; + + Computed = false; + Corrupted = false; +} + +/* + * Result + * + * Description: + * This function will return the 160-bit message digest into the + * array provided. + * + * Parameters: + * message_digest_array: [out] + * This is an array of five unsigned integers which will be filled + * with the message digest that has been computed. + * + * Returns: + * True if successful, false if it failed. + * + * Comments: + * + */ +bool SHA1::Result(unsigned *message_digest_array) +{ + int i; // Counter + + if (Corrupted) + { + return false; + } + + if (!Computed) + { + PadMessage(); + Computed = true; + } + + for(i = 0; i < 5; i++) + { + message_digest_array[i] = H[i]; + } + + return true; +} + +/* + * Input + * + * Description: + * This function accepts an array of octets as the next portion of + * the message. + * + * Parameters: + * message_array: [in] + * An array of characters representing the next portion of the + * message. + * + * Returns: + * Nothing. + * + * Comments: + * + */ +void SHA1::Input( const unsigned char *message_array, + unsigned length) +{ + if (!length) + { + return; + } + + if (Computed || Corrupted) + { + Corrupted = true; + return; + } + + while(length-- && !Corrupted) + { + Message_Block[Message_Block_Index++] = (*message_array & 0xFF); + + Length_Low += 8; + Length_Low &= 0xFFFFFFFF; // Force it to 32 bits + if (Length_Low == 0) + { + Length_High++; + Length_High &= 0xFFFFFFFF; // Force it to 32 bits + if (Length_High == 0) + { + Corrupted = true; // Message is too long + } + } + + if (Message_Block_Index == 64) + { + ProcessMessageBlock(); + } + + message_array++; + } +} + +/* + * Input + * + * Description: + * This function accepts an array of octets as the next portion of + * the message. + * + * Parameters: + * message_array: [in] + * An array of characters representing the next portion of the + * message. + * length: [in] + * The length of the message_array + * + * Returns: + * Nothing. + * + * Comments: + * + */ +void SHA1::Input( const char *message_array, + unsigned length) +{ + Input((unsigned char *) message_array, length); +} + +/* + * Input + * + * Description: + * This function accepts a single octets as the next message element. + * + * Parameters: + * message_element: [in] + * The next octet in the message. + * + * Returns: + * Nothing. + * + * Comments: + * + */ +void SHA1::Input(unsigned char message_element) +{ + Input(&message_element, 1); +} + +/* + * Input + * + * Description: + * This function accepts a single octet as the next message element. + * + * Parameters: + * message_element: [in] + * The next octet in the message. + * + * Returns: + * Nothing. + * + * Comments: + * + */ +void SHA1::Input(char message_element) +{ + Input((unsigned char *) &message_element, 1); +} + +/* + * operator<< + * + * Description: + * This operator makes it convenient to provide character strings to + * the SHA1 object for processing. + * + * Parameters: + * message_array: [in] + * The character array to take as input. + * + * Returns: + * A reference to the SHA1 object. + * + * Comments: + * Each character is assumed to hold 8 bits of information. + * + */ +SHA1& SHA1::operator<<(const char *message_array) +{ + const char *p = message_array; + + while(*p) + { + Input(*p); + p++; + } + + return *this; +} + +/* + * operator<< + * + * Description: + * This operator makes it convenient to provide character strings to + * the SHA1 object for processing. + * + * Parameters: + * message_array: [in] + * The character array to take as input. + * + * Returns: + * A reference to the SHA1 object. + * + * Comments: + * Each character is assumed to hold 8 bits of information. + * + */ +SHA1& SHA1::operator<<(const unsigned char *message_array) +{ + const unsigned char *p = message_array; + + while(*p) + { + Input(*p); + p++; + } + + return *this; +} + +/* + * operator<< + * + * Description: + * This function provides the next octet in the message. + * + * Parameters: + * message_element: [in] + * The next octet in the message + * + * Returns: + * A reference to the SHA1 object. + * + * Comments: + * The character is assumed to hold 8 bits of information. + * + */ +SHA1& SHA1::operator<<(const char message_element) +{ + Input((unsigned char *) &message_element, 1); + + return *this; +} + +/* + * operator<< + * + * Description: + * This function provides the next octet in the message. + * + * Parameters: + * message_element: [in] + * The next octet in the message + * + * Returns: + * A reference to the SHA1 object. + * + * Comments: + * The character is assumed to hold 8 bits of information. + * + */ +SHA1& SHA1::operator<<(const unsigned char message_element) +{ + Input(&message_element, 1); + + return *this; +} + +/* + * ProcessMessageBlock + * + * Description: + * This function will process the next 512 bits of the message + * stored in the Message_Block array. + * + * Parameters: + * None. + * + * Returns: + * Nothing. + * + * Comments: + * Many of the variable names in this function, especially the single + * character names, were used because those were the names used + * in the publication. + * + */ +void SHA1::ProcessMessageBlock() +{ + const unsigned K[] = { // Constants defined for SHA-1 + 0x5A827999, + 0x6ED9EBA1, + 0x8F1BBCDC, + 0xCA62C1D6 + }; + int t; // Loop counter + unsigned temp; // Temporary word value + unsigned W[80]; // Word sequence + unsigned A, B, C, D, E; // Word buffers + + /* + * Initialize the first 16 words in the array W + */ + for(t = 0; t < 16; t++) + { + W[t] = ((unsigned) Message_Block[t * 4]) << 24; + W[t] |= ((unsigned) Message_Block[t * 4 + 1]) << 16; + W[t] |= ((unsigned) Message_Block[t * 4 + 2]) << 8; + W[t] |= ((unsigned) Message_Block[t * 4 + 3]); + } + + for(t = 16; t < 80; t++) + { + W[t] = CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]); + } + + A = H[0]; + B = H[1]; + C = H[2]; + D = H[3]; + E = H[4]; + + for(t = 0; t < 20; t++) + { + temp = CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0]; + temp &= 0xFFFFFFFF; + E = D; + D = C; + C = CircularShift(30,B); + B = A; + A = temp; + } + + for(t = 20; t < 40; t++) + { + temp = CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1]; + temp &= 0xFFFFFFFF; + E = D; + D = C; + C = CircularShift(30,B); + B = A; + A = temp; + } + + for(t = 40; t < 60; t++) + { + temp = CircularShift(5,A) + + ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2]; + temp &= 0xFFFFFFFF; + E = D; + D = C; + C = CircularShift(30,B); + B = A; + A = temp; + } + + for(t = 60; t < 80; t++) + { + temp = CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3]; + temp &= 0xFFFFFFFF; + E = D; + D = C; + C = CircularShift(30,B); + B = A; + A = temp; + } + + H[0] = (H[0] + A) & 0xFFFFFFFF; + H[1] = (H[1] + B) & 0xFFFFFFFF; + H[2] = (H[2] + C) & 0xFFFFFFFF; + H[3] = (H[3] + D) & 0xFFFFFFFF; + H[4] = (H[4] + E) & 0xFFFFFFFF; + + Message_Block_Index = 0; +} + +/* + * PadMessage + * + * Description: + * According to the standard, the message must be padded to an even + * 512 bits. The first padding bit must be a '1'. The last 64 bits + * represent the length of the original message. All bits in between + * should be 0. This function will pad the message according to those + * rules by filling the message_block array accordingly. It will also + * call ProcessMessageBlock() appropriately. When it returns, it + * can be assumed that the message digest has been computed. + * + * Parameters: + * None. + * + * Returns: + * Nothing. + * + * Comments: + * + */ +void SHA1::PadMessage() +{ + /* + * Check to see if the current message block is too small to hold + * the initial padding bits and length. If so, we will pad the + * block, process it, and then continue padding into a second block. + */ + if (Message_Block_Index > 55) + { + Message_Block[Message_Block_Index++] = 0x80; + while(Message_Block_Index < 64) + { + Message_Block[Message_Block_Index++] = 0; + } + + ProcessMessageBlock(); + + while(Message_Block_Index < 56) + { + Message_Block[Message_Block_Index++] = 0; + } + } + else + { + Message_Block[Message_Block_Index++] = 0x80; + while(Message_Block_Index < 56) + { + Message_Block[Message_Block_Index++] = 0; + } + + } + + /* + * Store the message length as the last 8 octets + */ + Message_Block[56] = (Length_High >> 24) & 0xFF; + Message_Block[57] = (Length_High >> 16) & 0xFF; + Message_Block[58] = (Length_High >> 8) & 0xFF; + Message_Block[59] = (Length_High) & 0xFF; + Message_Block[60] = (Length_Low >> 24) & 0xFF; + Message_Block[61] = (Length_Low >> 16) & 0xFF; + Message_Block[62] = (Length_Low >> 8) & 0xFF; + Message_Block[63] = (Length_Low) & 0xFF; + + ProcessMessageBlock(); +} + + +/* + * CircularShift + * + * Description: + * This member function will perform a circular shifting operation. + * + * Parameters: + * bits: [in] + * The number of bits to shift (1-31) + * word: [in] + * The value to shift (assumes a 32-bit integer) + * + * Returns: + * The shifted value. + * + * Comments: + * + */ +unsigned SHA1::CircularShift(int bits, unsigned word) +{ + return ((word << bits) & 0xFFFFFFFF) | ((word & 0xFFFFFFFF) >> (32-bits)); +} diff --git a/sha1/sha1.h b/sha1/sha1.h new file mode 100644 index 00000000..c0efa1c9 --- /dev/null +++ b/sha1/sha1.h @@ -0,0 +1,89 @@ +/* + * sha1.h + * + * Copyright (C) 1998, 2009 + * Paul E. Jones + * All Rights Reserved. + * + ***************************************************************************** + * $Id: sha1.h 12 2009-06-22 19:34:25Z paulej $ + ***************************************************************************** + * + * Description: + * This class implements the Secure Hashing Standard as defined + * in FIPS PUB 180-1 published April 17, 1995. + * + * Many of the variable names in this class, especially the single + * character names, were used because those were the names used + * in the publication. + * + * Please read the file sha1.cpp for more information. + * + */ + +#ifndef _SHA1_H_ +#define _SHA1_H_ + +class SHA1 +{ + + public: + + SHA1(); + virtual ~SHA1(); + + /* + * Re-initialize the class + */ + void Reset(); + + /* + * Returns the message digest + */ + bool Result(unsigned *message_digest_array); + + /* + * Provide input to SHA1 + */ + void Input( const unsigned char *message_array, + unsigned length); + void Input( const char *message_array, + unsigned length); + void Input(unsigned char message_element); + void Input(char message_element); + SHA1& operator<<(const char *message_array); + SHA1& operator<<(const unsigned char *message_array); + SHA1& operator<<(const char message_element); + SHA1& operator<<(const unsigned char message_element); + + private: + + /* + * Process the next 512 bits of the message + */ + void ProcessMessageBlock(); + + /* + * Pads the current message block to 512 bits + */ + void PadMessage(); + + /* + * Performs a circular left shift operation + */ + inline unsigned CircularShift(int bits, unsigned word); + + unsigned H[5]; // Message digest buffers + + unsigned Length_Low; // Message length in bits + unsigned Length_High; // Message length in bits + + unsigned char Message_Block[64]; // 512-bit message blocks + int Message_Block_Index; // Index into message block array + + bool Computed; // Is the digest computed? + bool Corrupted; // Is the message digest corruped? + +}; + +#endif diff --git a/sha1/shacmp.cpp b/sha1/shacmp.cpp new file mode 100644 index 00000000..476ad3f0 --- /dev/null +++ b/sha1/shacmp.cpp @@ -0,0 +1,169 @@ +/* + * shacmp.cpp + * + * Copyright (C) 1998, 2009 + * Paul E. Jones + * All Rights Reserved + * + ***************************************************************************** + * $Id: shacmp.cpp 12 2009-06-22 19:34:25Z paulej $ + ***************************************************************************** + * + * Description: + * This utility will compare two files by producing a message digest + * for each file using the Secure Hashing Algorithm and comparing + * the message digests. This function will return 0 if they + * compare or 1 if they do not or if there is an error. + * Errors result in a return code higher than 1. + * + * Portability Issues: + * none. + * + */ + +#include +#include +#include "sha1.h" + +/* + * Return codes + */ +#define SHA1_COMPARE 0 +#define SHA1_NO_COMPARE 1 +#define SHA1_USAGE_ERROR 2 +#define SHA1_FILE_ERROR 3 + +/* + * Function prototype + */ +void usage(); + +/* + * main + * + * Description: + * This is the entry point for the program + * + * Parameters: + * argc: [in] + * This is the count of arguments in the argv array + * argv: [in] + * This is an array of filenames for which to compute message digests + * + * Returns: + * Nothing. + * + * Comments: + * + */ +int main(int argc, char *argv[]) +{ + SHA1 sha; // SHA-1 class + FILE *fp; // File pointer for reading files + char c; // Character read from file + unsigned message_digest[2][5]; // Message digest for files + int i; // Counter + bool message_match; // Message digest match flag + int returncode; + + /* + * If we have two arguments, we will assume they are filenames. If + * we do not have to arguments, call usage() and exit. + */ + if (argc != 3) + { + usage(); + return SHA1_USAGE_ERROR; + } + + /* + * Get the message digests for each file + */ + for(i = 1; i <= 2; i++) + { + sha.Reset(); + + if (!(fp = fopen(argv[i],"rb"))) + { + fprintf(stderr, "sha: unable to open file %s\n", argv[i]); + return SHA1_FILE_ERROR; + } + + c = fgetc(fp); + while(!feof(fp)) + { + sha.Input(c); + c = fgetc(fp); + } + + fclose(fp); + + if (!sha.Result(message_digest[i-1])) + { + fprintf(stderr,"shacmp: could not compute message digest for %s\n", + argv[i]); + return SHA1_FILE_ERROR; + } + } + + /* + * Compare the message digest values + */ + message_match = true; + for(i = 0; i < 5; i++) + { + if (message_digest[0][i] != message_digest[1][i]) + { + message_match = false; + break; + } + } + + if (message_match) + { + printf("Fingerprints match:\n"); + returncode = SHA1_COMPARE; + } + else + { + printf("Fingerprints do not match:\n"); + returncode = SHA1_NO_COMPARE; + } + + printf( "\t%08X %08X %08X %08X %08X\n", + message_digest[0][0], + message_digest[0][1], + message_digest[0][2], + message_digest[0][3], + message_digest[0][4]); + printf( "\t%08X %08X %08X %08X %08X\n", + message_digest[1][0], + message_digest[1][1], + message_digest[1][2], + message_digest[1][3], + message_digest[1][4]); + + return returncode; +} + +/* + * usage + * + * Description: + * This function will display program usage information to the user. + * + * Parameters: + * None. + * + * Returns: + * Nothing. + * + * Comments: + * + */ +void usage() +{ + printf("usage: shacmp \n"); + printf("\tThis program will compare the message digests (fingerprints)\n"); + printf("\tfor two files using the Secure Hashing Algorithm (SHA-1).\n"); +} diff --git a/sha1/shatest.cpp b/sha1/shatest.cpp new file mode 100644 index 00000000..4d29ef9e --- /dev/null +++ b/sha1/shatest.cpp @@ -0,0 +1,149 @@ +/* + * shatest.cpp + * + * Copyright (C) 1998, 2009 + * Paul E. Jones + * All Rights Reserved + * + ***************************************************************************** + * $Id: shatest.cpp 12 2009-06-22 19:34:25Z paulej $ + ***************************************************************************** + * + * Description: + * This file will exercise the SHA1 class and perform the three + * tests documented in FIPS PUB 180-1. + * + * Portability Issues: + * None. + * + */ + +#include +#include "sha1.h" + +using namespace std; + +/* + * Define patterns for testing + */ +#define TESTA "abc" +#define TESTB "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + +/* + * Function prototype + */ +void DisplayMessageDigest(unsigned *message_digest); + +/* + * main + * + * Description: + * This is the entry point for the program + * + * Parameters: + * None. + * + * Returns: + * Nothing. + * + * Comments: + * + */ +int main() +{ + SHA1 sha; + unsigned message_digest[5]; + + /* + * Perform test A + */ + cout << endl << "Test A: 'abc'" << endl; + + sha.Reset(); + sha << TESTA; + + if (!sha.Result(message_digest)) + { + cerr << "ERROR-- could not compute message digest" << endl; + } + else + { + DisplayMessageDigest(message_digest); + cout << "Should match:" << endl; + cout << '\t' << "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D" << endl; + } + + /* + * Perform test B + */ + cout << endl << "Test B: " << TESTB << endl; + + sha.Reset(); + sha << TESTB; + + if (!sha.Result(message_digest)) + { + cerr << "ERROR-- could not compute message digest" << endl; + } + else + { + DisplayMessageDigest(message_digest); + cout << "Should match:" << endl; + cout << '\t' << "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1" << endl; + } + + /* + * Perform test C + */ + cout << endl << "Test C: One million 'a' characters" << endl; + + sha.Reset(); + for(int i = 1; i <= 1000000; i++) sha.Input('a'); + + if (!sha.Result(message_digest)) + { + cerr << "ERROR-- could not compute message digest" << endl; + } + else + { + DisplayMessageDigest(message_digest); + cout << "Should match:" << endl; + cout << '\t' << "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F" << endl; + } + + return 0; +} + +/* + * DisplayMessageDigest + * + * Description: + * Display Message Digest array + * + * Parameters: + * None. + * + * Returns: + * Nothing. + * + * Comments: + * + */ +void DisplayMessageDigest(unsigned *message_digest) +{ + ios::fmtflags flags; + + cout << '\t'; + + flags = cout.setf(ios::hex|ios::uppercase,ios::basefield); + cout.setf(ios::uppercase); + + for(int i = 0; i < 5 ; i++) + { + cout << message_digest[i] << ' '; + } + + cout << endl; + + cout.setf(flags); +}