pgpdialogs.pas

来自「用DELPHI实现的 PGP 加密算法」· PAS 代码 · 共 989 行 · 第 1/3 页

PAS
989
字号
			     KeyFilterFlag_AllKeys, UserID_Ordering);
      if Result>0 then Result:=0;
    finally
      PGPFreeKeySet(KeySetSelToAdd);
    end;
  end
  else Result:=kPGPError_PublicKeyNotFound;
end;

function RecipientsDialog(Context: pPGPContext; AllKeys: pPGPKeySet;
			  AlwaysDisplayDialog: PGPBoolean; var Armor, EyesOnly, MIME, Textmode: PGPUInt32;
			  const DefaultKey: String; var RecipientKeys: Pointer;
			  const Prompt: String; WinHandle: Integer): PGPError;
var
  GroupFileSpec	 : pPGPFileSpec;
  GroupSet	 : pPGPGroupSet;
  GroupOption	 : pPGPOptionList;
  FormOptions	 : pPGPOptionList;
  OptionList	 : pPGPOptionList;
  RecipientSpec	 : TPGPRecipientSpec;
begin
  RecipientKeys:=nil;
  GroupFileSpec:=nil;
  GroupSet:=nil;
  OptionList:=nil;
  FormOptions:=nil;
  Result:=PGPNewFileSpecFromFullPath(Context, PChar(KeyRings.GroupsFile), GroupFileSpec);
  try
    if Result=0 then Result:=PGPNewGroupSetFromFile(Context, GroupFileSpec, GroupSet);
    try
      if Result=0 then
	GroupOption:=PGPOUIRecipientGroups(Context, GroupSet)
      else GroupOption:=PGPONullOption(Context);
      try
	Result:=PGPBuildOptionList(Context, FormOptions,
	  [
	    PGPOUICheckbox(Context, 801, '&Armor', nil, Armor, Armor, PGPOLastOption(Context)),
	    PGPOUICheckbox(Context, 802, '&MIME', nil, MIME, MIME, PGPOLastOption(Context)),
	    PGPOUICheckbox(Context, 803, '&Textmode', nil, Textmode, Textmode, PGPOLastOption(Context)),
	    PGPOUICheckbox(Context, 804, 'For your &eyes only', nil, EyesOnly, EyesOnly, PGPOLastOption(Context))
	  ]);
	if Result<>0 then Exit;
	try
	  Result:=PGPBuildOptionList(Context, OptionList,
	    [
	      PGPOUIDialogOptions(Context, FormOptions, PGPOLastOption(Context)),
	      PGPOUIDisplayMarginalValidity(Context, PGPTrue),
	      GroupOption,
	      PGPOUIWindowTitle(Context, 'PGP'),
	      PGPOUIDialogPrompt(Context, PChar(Prompt)),
	      PGPOUIParentWindowHandle(Context, WinHandle)
	    ]);
	  if Result<>0 then Exit;
	  if DefaultKey<>'' then begin
	    FillChar(RecipientSpec, SizeOf(TPGPRecipientSpec), 0);
	    RecipientSpec.AType:=kPGPRecipientSpecType_Key;
	    Result:=GetKeyByHexID(AllKeys, DefaultKey, RecipientSpec.ID.Key);
	    if Result=0 then PGPAppendOptionList(OptionList, [PGPOUIDefaultRecipients(Context, 1, @RecipientSpec)]);
	  end;
	  try
	    if PGP7X then begin
	      Result:=PGPRecipientDialog(Context, PGPPeekKeySetKeyDB(AllKeys), AlwaysDisplayDialog,
					 pPGPKeyDB(RecipientKeys) ,OptionList, PGPOLastOption(Context));
	    end
	    else begin
	      Result:=PGPRecipientDialog(Context, AllKeys, AlwaysDisplayDialog,
					 pPGPKeySet(RecipientKeys), OptionList, PGPOLastOption(Context));
	    end;
	    ProcessMessages;
	  finally
	    PGPFreeOptionList(OptionList);
	  end;
	finally
	  PGPFreeOptionList(FormOptions);
	end;
      finally
	PGPFreeOptionList(GroupOption);
      end;
    finally
      if GroupSet<>nil then PGPFreeGroupSet(GroupSet);
    end;
  finally
    PGPFreeFileSpec(GroupFileSpec);
  end;
end;

function KeyServerDialog(Context: pPGPContext; tlsContext: pPGPtlsContext;
			 KeySetMain: pPGPKeySet; const KeyData: String;
			 var KeysFound: Pointer; WinHandle: Integer): PGPError;
var
  KeyFilter	 : pPGPFilter;
  ThreadStorage	 : pPGPKeyServerThreadStorage;
  KeyServerSpecs : pPGPKeyServerSpec;
  KeyServerCount : PGPUInt32;
  OptionList	 : pPGPOptionList;
begin
  KeysFound:=nil;
  KeyFilter:=nil;
  OptionList:=nil;
  Result:=GetKeyFilterByAnyID(Context, KeyData, true, KeyFilter);
  if Result<>0 then Exit;
  try
    Result:=InitKeyServer(Context, ThreadStorage, KeyServerSpecs, KeyServerCount);
    if Result<>0 then Exit;
    Result:=PGPBuildOptionList(Context, OptionList,
      [
	PGPOKeyServerSearchFilter(Context, KeyFilter),
	PGPOUIParentWindowHandle(Context, WinHandle)
      ]);
    try
      Result:=PGPSearchKeyServerDialog(Context, KeyServerCount, KeyServerSpecs, tlsContext, PGPFalse,
				       KeysFound, OptionList, PGPOLastOption(Context));
      Sleep(100); // trying to prevent a strange cancel crash
      ProcessMessages;
    finally
      PGPFreeOptionList(OptionList);
    end;
  finally
    FreeKeyServer(ThreadStorage, KeyServerSpecs, KeyServerCount);
    PGPFreeFilter(KeyFilter);
  end;
end;

function CollectRandomDataDialog(Context: pPGPContext; NeededEntropyBits: PGPUInt32; WinHandle: Integer): PGPError;
begin
  Result:=PGPCollectRandomDataDialog(Context,
				     NeededEntropyBits,
				     PGPOUIParentWindowHandle(Context, WinHandle),
				     PGPOLastOption(Context));
  ProcessMessages;
end;

function ConvEncPassphraseDialog(Context: pPGPContext; var Passphrase: PChar;
				 const Prompt: String; WinHandle: Integer): PGPError;
var
  OptionList	 : pPGPOptionList;
begin
  Passphrase:=nil;
  OptionList:=nil;
  Result:=PGPBuildOptionList(Context, OptionList,
    [
      PGPOUIWindowTitle(Context, 'PGP'),
      PGPOUIDialogPrompt(Context, PChar(Prompt)),
      PGPOUIOutputPassphrase(Context, Passphrase),
      PGPOUIParentWindowHandle(Context, WinHandle)
    ]);
  if Result<>0 then Exit;
  try
    Result:=PGPConventionalEncryptionPassphraseDialog(Context, OptionList, PGPOLastOption(Context));
    ProcessMessages;
  finally
    PGPFreeOptionList(OptionList);
  end;
end;

function ConvDecPassphraseDialog(Context: pPGPContext; var Passphrase: PChar;
				 const Prompt: String; WinHandle: Integer): PGPError;
var
  OptionList	 : pPGPOptionList;
begin
  Passphrase:=nil;
  OptionList:=nil;
  Result:=PGPBuildOptionList(Context, OptionList,
    [
      PGPOUIWindowTitle(Context, 'PGP'),
      PGPOUIDialogPrompt(Context, PChar(Prompt)),
      PGPOUIOutputPassphrase(Context, Passphrase),
      PGPOUIParentWindowHandle(Context, WinHandle)
    ]);
  if Result<>0 then Exit;
  try
    Result:=PGPConventionalDecryptionPassphraseDialog(Context, OptionList, PGPOLastOption(Context));
    ProcessMessages;
  finally
    PGPFreeOptionList(OptionList);
  end;
end;

function KeyPassphraseDialog(Context: pPGPContext; KeySet: pPGPKeySet; var Passphrase: PChar;
			     const Prompt: String; WinHandle: Integer): PGPError;
var
  Key		 : pPGPKey;
  OptionList	 : pPGPOptionList;
begin
  Passphrase:=nil;
  OptionList:=nil;
  Result:=GetKeyFromKeySet(KeySet, Key);
  if Result<>0 then Exit;
  Result:=PGPBuildOptionList(Context, OptionList,
    [
      PGPOUIWindowTitle(Context, 'PGP'),
      PGPOUIDialogPrompt(Context, PChar(Prompt)),
      PGPOUIOutputPassphrase(Context, Passphrase),
      PGPOUIParentWindowHandle(Context, WinHandle)
    ]);
  if Result<>0 then Exit;
  try
    Result:=PGPKeyPassphraseDialog(Context, Key, OptionList, PGPOLastOption(Context));
    ProcessMessages;
  finally
    PGPFreeOptionList(OptionList);
  end;
end;

function SigningPassphraseDialog(Context: pPGPContext; KeySetMain: pPGPKeySet;
				 var SigningKey: pPGPKey; var SignKeyPass: PChar;
				 const DefaultKeyID: String; FindMatchingKey: Longbool; SignOption: TSignOption;
				 var Armor, MIME, RipeMD: PGPUInt32; const Prompt: String; WinHandle: Integer): PGPError;
var
  DefaultKey	 : pPGPKey;
  OptionList	 : pPGPOptionList;
  DialogOptions	 : pPGPOptionList;
begin
  SigningKey:=nil;
  SignKeyPass:=nil;
  OptionList:=nil;
  if (DefaultKeyID='') or (GetKeyByHexID(KeySetMain, PChar(DefaultKeyID), DefaultKey)<>0) then DefaultKey:=nil;
  Result:=PGPBuildOptionList(Context, OptionList,
    [
      PGPOUIWindowTitle(Context, 'PGP'),
      PGPOUIDialogPrompt(Context, PChar(Prompt)),
      PGPOUIDefaultKey(Context, DefaultKey),
      PGPOUIFindMatchingKey(Context, PGPBoolean(FindMatchingKey) and 1),
      PGPOUIVerifyPassphrase(Context, PGPTrue),
      PGPOUIOutputPassphrase(Context, SignKeyPass),
      PGPOUIParentWindowHandle(Context, WinHandle)
    ]);
  if Result<>0 then Exit;
  DialogOptions:=nil;
  try
    case SignOption of
      soClear: begin
	Result:=PGPBuildOptionList(Context, DialogOptions,
	  [
	    PGPOUICheckbox(Context, 802, '&MIME', nil, MIME, MIME, PGPOLastOption(Context)),
	    PGPOUICheckbox(Context, 803, '&RipeMD (RSA)', nil, RipeMD, RipeMD, PGPOLastOption(Context))
	  ]);
      end;
      soDetached: begin
	Result:=PGPBuildOptionList(Context, DialogOptions,
	  [
	    PGPOUICheckbox(Context, 801, '&Armor', nil, Armor, Armor, PGPOLastOption(Context)),
	    PGPOUICheckbox(Context, 803, '&RipeMD', nil, RipeMD, RipeMD, PGPOLastOption(Context))
	  ]);
      end;
    else
      Result:=PGPBuildOptionList(Context, DialogOptions,
	[
	  PGPOUICheckbox(Context, 803, '&RipeMD', nil, RipeMD, RipeMD, PGPOLastOption(Context))
	]);
    end;
    if Result<>0 then Exit;
    Result:=PGPAppendOptionList(OptionList, [PGPOUIDialogOptions(Context, DialogOptions, PGPOLastOption(Context))]);
  finally
    PGPFreeOptionList(DialogOptions);
  end;
  if Result<>0 then Exit;
  try
    if PGP7X then begin
      Result:=PGPSigningPassphraseDialog(Context, PGPPeekKeySetKeyDB(KeySetMain), SigningKey,
					 OptionList, PGPOLastOption(Context));
    end
    else Result:=PGPSigningPassphraseDialog(Context, KeySetMain, SigningKey, OptionList, PGPOLastOption(Context));
    ProcessMessages;
  finally
    PGPFreeOptionList(OptionList);
  end;
end;

function DecryptionPassphraseDialog(Context: pPGPContext; RecipientKeys: pPGPKeySet;
				    KeyIDCount: PGPUInt32; const KeyIDList: pPGPKeyID7;
				    var DecryptionKey: pPGPKey; var DecryptKeyPass: PChar;
				    FindMatchingKey: Longbool; const Prompt: String; WinHandle: Integer): PGPError;
var
  OptionList	 : pPGPOptionList;
begin
  DecryptionKey:=nil;
  DecryptKeyPass:=nil;
  OptionList:=nil;
  Result:=PGPBuildOptionList(Context, OptionList,
    [
      PGPOUIWindowTitle(Context, 'PGP'),
      PGPOUIDialogPrompt(Context, PChar(Prompt)),
      PGPOUIFindMatchingKey(Context, PGPBoolean(FindMatchingKey) and 1),
      PGPOUIVerifyPassphrase(Context, PGPTrue),
      PGPOUIOutputPassphrase(Context, DecryptKeyPass),
      PGPOUIParentWindowHandle(Context, WinHandle)
    ]);
  if Result<>0 then Exit;
  try
    Result:=PGPDecryptionPassphraseDialog(Context, RecipientKeys, KeyIDCount, KeyIDList,
					  DecryptionKey, OptionList, PGPOLastOption(Context));
    ProcessMessages;
  finally
    PGPFreeOptionList(OptionList);
  end;
end;

function ConfirmationPassphraseDialog(Context: pPGPContext; var Passphrase: PChar;
				      MinPassLength, MinPassQuality: Integer;
				      ShowPassphraseQuality: Longbool;
				      const Prompt: String; WinHandle: Integer): PGPError;
var
  OptionList	 : pPGPOptionList;
begin
  Passphrase:=nil;
  OptionList:=nil;
  Result:=PGPBuildOptionList(Context, OptionList,
    [
      PGPOUIWindowTitle(Context, 'PGP'),
      PGPOUIDialogPrompt(Context, PChar(Prompt)),
      PGPOUIMinimumPassphraseLength(Context, MinPassLength),
      PGPOUIMinimumPassphraseQuality(Context, MinPassQuality),
      PGPOUIShowPassphraseQuality(Context, PGPBoolean(ShowPassphraseQuality) and 1),
      PGPOUIOutputPassphrase(Context, Passphrase),
      PGPOUIParentWindowHandle(Context, WinHandle)
    ]);
  if Result<>0 then Exit;
  try
    Result:=PGPConfirmationPassphraseDialog(Context, OptionList, PGPOLastOption(Context));
    ProcessMessages;
  finally
    PGPFreeOptionList(OptionList);
  end;
end;

end.

⌨️ 快捷键说明

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