///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//  TestSha512
// 
//  Test SHA1 algorithm against test vectors
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
bool
    TestSha512
    (
        void
    )
{
    int             i;
    int             k;
    int             len;
    Sha512Context   context;
    SHA512_HASH     hash;
    bool            success = true;

    for( i=0; i<NUM_TEST_VECTORS; i++ )
    {
        Sha512Initialise( &context );
        len = (int) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (int)strlen( gTestVectors[i].PlainText );
        Sha512Update( &context, gTestVectors[i].PlainText, (uint32_t)len );
        Sha512Finalise( &context, &hash );

        if( memcmp( &hash, &gTestVectors[i].Sha512Hash, sizeof(hash) ) == 0 )
        {
            // Test vector passed
        }
        else
        {
            printf( "TestSha512 - Test vector %u failed\n", i );
            success = false;
        }
    }

    // Check the vectors again, this time adding just 1 char at a time to the hash functions
    for( i=0; i<NUM_TEST_VECTORS; i++ )
    {
        Sha512Initialise( &context );
        len = (int) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (int)strlen( gTestVectors[i].PlainText );
        for( k=0; k<len; k++ )
        {
            Sha512Update( &context, &gTestVectors[i].PlainText[k], 1 );
        }
        Sha512Finalise( &context, &hash );

        if( memcmp( &hash, &gTestVectors[i].Sha512Hash, sizeof(hash) ) == 0 )
        {
            // Test vector passed
        }
        else
        {
            printf( "TestSha512 - Test vector %u failed [byte by byte]\n", i );
            success = false;
        }
    }

    return success;
}
예제 #2
0
파일: sha512-nsis.c 프로젝트: if15b006/dxgl
void __declspec(dllexport) CalculateSha512Sum(HWND hwndParent, int string_size,
	TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
{
	int i;
	TCHAR filename[1024];
	Sha512Context context;
	HANDLE file;
	char buffer[512];
	char comp[1024];
	DWORD bytesread;
	SHA512_HASH sha512;
	int compout;
	EXDLL_INIT();
	popstring(filename);
	popstring(comp);
	Sha512Initialise(&context);
	file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
		OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (!file)
	{
		filename[0] = '0';
		filename[1] = 0;
		pushstring(comp);
		pushstring(filename);
		return;
	}
	while (1)
	{
		ReadFile(file, buffer, 512, &bytesread, NULL);
		if (!bytesread) break;
		Sha512Update(&context, buffer, bytesread);
		if (bytesread < 512) break;
	}
	Sha512Finalise(&context, &sha512);
	CloseHandle(file);
	for (i = 0; i < (512 / 8); i++)
	{
		buffer[i * 2] = hexdigit(sha512.bytes[i] >> 4);
		buffer[(i * 2) + 1] = hexdigit(sha512.bytes[i] & 0xF);
	}
	buffer[512 / 4] = 0;
	compout = memcmp(buffer, comp, 128);
	if (compout)
	{
		filename[0] = '0';
	}
	else
	{
		filename[0] = '1';
	}
	filename[1] = 0;
	pushstring(comp);
	pushstring(filename);
}