⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pgpnetapphash.c

📁 PGP8.0源码 请认真阅读您的文件包然后写出其具体功能
💻 C
字号:
/*____________________________________________________________________________
	Copyright (C) 2002 PGP Corporation
	All rights reserved.

	$Id: pgpNetAppHash.c,v 1.3 2002/08/06 20:10:28 dallen Exp $
____________________________________________________________________________*/

#include <windows.h>
#include "pgpWin32IPC.h"
#include "pgpNetAppHash.h"
#include "pgpErrors.h"
#include "pgpHash.h"
#include "pgpMem.h"

#define kBufSize		10240

PGPError 
pgpNetHashApplication (
		PGPContextRef	context,
		char* 			pszAppPath, 
		PGPByte*		bHashOut,
		PGPSize			sizeHashOut)
{
	PGPError            err 		= kPGPError_NoErr;
	HANDLE      		hFile;
	PGPByte*			pbuf;
	DWORD       		dwRead;
	PGPBoolean  		bRC;
	PGPHashContextRef   hashRef;
	PGPSize				size;
	HWND				hTray		= NULL;
	HANDLE				hProcess	= NULL;
	HANDLE				hToken		= NULL;
	PGPUInt32			dwPID		= 0;

	if (IsNull (pszAppPath) || IsNull (bHashOut))
		return kPGPError_BadParams;
		
	// allocate a buffer for reading file
	pbuf = PGPNewData (PGPPeekContextMemoryMgr (context), kBufSize, 0);
	if (IsNull (pbuf))
		return kPGPError_OutOfMemory;

	hFile = CreateFile (pszAppPath, GENERIC_READ, 
				FILE_SHARE_READ, NULL, OPEN_EXISTING,
				FILE_ATTRIBUTE_NORMAL, NULL);

	if (hFile == INVALID_HANDLE_VALUE)
	{
		// Couldn't open the file.  Try to impersonate tray and see
		// if we can open it in the users context.
		hTray = FindWindow(PGPTRAY_WINDOWNAME, PGPTRAY_WINDOWNAME);
		if (IsntNull(hTray))
		{
			GetWindowThreadProcessId(hTray, &dwPID);
  
			hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID);
			if (hProcess)
			{
				if (OpenProcessToken (
						hProcess, 
						TOKEN_IMPERSONATE | TOKEN_DUPLICATE | TOKEN_QUERY,
						&hToken) == FALSE)
				{
					CloseHandle(hProcess);
					return kPGPError_CantOpenFile;
				}

				  (hToken);

				// Open the file read-only
				hFile = CreateFile (pszAppPath, GENERIC_READ, 
						FILE_SHARE_READ, NULL, OPEN_EXISTING,
						FILE_ATTRIBUTE_NORMAL, NULL);


				CloseHandle(hProcess);
				CloseHandle(hToken);
				RevertToSelf();
			
				if (hFile == INVALID_HANDLE_VALUE)
					return kPGPError_CantOpenFile;
			}
			else
				return kPGPError_CantOpenFile;
		}
		else
			return kPGPError_CantOpenFile;
	}
	
    // Initialize a Hash Context
	err = PGPNewHashContext (context, kPGPHashAlgorithm_MD5, &hashRef);

	if (IsPGPError (err))
	{
		CloseHandle (hFile);
		PGPFreeData (pbuf);
		return err;
	}

	// check that buffer to receive hash is big enough
	err = PGPGetHashSize (hashRef, &size);
	if (IsntPGPError (err))
	{
		if (size > sizeHashOut)
			err = kPGPError_BufferTooSmall;
	}
	if (IsPGPError (err))
	{
		CloseHandle (hFile);
		PGPFreeData (pbuf);
		return err;
	}

	// Stream the file through the hash calculation a buffer at a time
	while ((bRC = ReadFile (hFile, pbuf, kBufSize, &dwRead, NULL)))
	{
		if (dwRead == 0)
			break;

		err = PGPContinueHash (hashRef, pbuf, dwRead);
		if (IsPGPError(err))
			break;
	}

	// If the read failed, or the hash failed, return
	if (IsntPGPError (err))
	{
		if (bRC == FALSE) 
			err = kPGPError_FileOpFailed;
		else
			err = PGPFinalizeHash (hashRef, bHashOut);
	}

	CloseHandle (hFile);

	PGPFreeHashContext (hashRef);
	PGPFreeData (pbuf);

	return err;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -