keyproptypes.pas

来自「用DELPHI实现的 PGP 加密算法」· PAS 代码 · 共 465 行

PAS
465
字号
{$J+,Z4}
unit KeyPropTypes;
{$IFDEF CONDITIONALEXPRESSIONS} {$DEFINE FIXED} {$ENDIF} // Delphi 6 and later

{------------------------------------------------------------------------------}
{                                                                              }
{                                 This code is                                 }
{              Copyright (C) 2001-2003 by Michael in der Wiesche               }
{                                                                              }
{------------------------------------------------------------------------------}

interface

uses
  Windows,
  Classes;

const
  ShortHexIDLen				= 10;
  LongHexIDLen				= 18;
  DefHexIDLen				= LongHexIDLen;

{ My tribute to Steve Heller's great work with creating SPGP & DPGP }
const
  spgpKeyPropFlag_None			= 0;
  spgpKeyPropFlag_All			= $FFFFFFFF;

  // "string" properties
  spgpKeyPropFlag_KeyID			= $00000001;
  spgpKeyPropFlag_UserID		= $00000002;
  spgpKeyPropFlag_Fingerprint		= $00000004;
  spgpKeyPropFlag_CreationTimeStr	= $00000008;
  spgpKeyPropFlag_ExpirationTimeStr	= $00000010;

  // "numeric" properties
  spgpKeyPropFlag_KeyBits		= $00000080;
  spgpKeyPropFlag_KeyAlg		= $00000100;
  spgpKeyPropFlag_Trust			= $00000200;
  spgpKeyPropFlag_Validity		= $00000400;
  spgpKeyPropFlag_CreationTime		= $00000800;
  spgpKeyPropFlag_ExpirationTime	= $00001000;

  // "boolean" properties
  spgpKeyPropFlag_IsSecret		= $00008000;
  spgpKeyPropFlag_IsAxiomatic		= $00010000;
  spgpKeyPropFlag_IsRevoked		= $00020000;
  spgpKeyPropFlag_IsDisabled		= $00040000;
  spgpKeyPropFlag_IsExpired		= $00080000;
  spgpKeyPropFlag_IsSecretShared	= $00100000;

  spgpKeyPropFlag_CanEncrypt		= $00200000;
  spgpKeyPropFlag_CanDecrypt		= $00400000;
  spgpKeyPropFlag_CanSign		= $00800000;
  spgpKeyPropFlag_CanVerify		= $01000000;

  spgpKeyPropFlag_HasRevoker		= $02000000;
  spgpKeyPropFlag_HasADK		= $04000000;
  spgpKeyPropFlag_HasSubKey		= $08000000;
  spgpKeyPropFlag_LegacyKey		= $10000000;

  // "list" flags
  spgpKeyPropFlag_IncludeUserIDs	= $20000000;
  spgpKeyPropFlag_IncludeSignerIDs	= $40000000;
  spgpKeyPropFlag_IncludeGroupsList	= $80000000;

  // "convenience" flags
  spgpKeyPropFlag_IDFlags		= spgpKeyPropFlag_KeyID or spgpKeyPropFlag_UserID;
  spgpKeyPropFlag_IDComplete		= spgpKeyPropFlag_IDFlags or spgpKeyPropFlag_KeyBits or spgpKeyPropFlag_KeyAlg;

const
  KeyFilterFlag_AllKeys			= $0000;
  // boolean property filters
  KeyFilterFlag_CanEncrypt		= $0001;
  KeyFilterFlag_CanDecrypt		= $0002;
  KeyFilterFlag_CanSign			= $0004;
  KeyFilterFlag_CanVerify		= $0008;
  // ability filters
  KeyFilterFlag_Enabled			= $0010;
  KeyFilterFlag_Disabled		= $0020;
  // algorithm filters
  KeyFilterFlag_DHDSS			= $0100;
  KeyFilterFlag_RSA			= $0200;
  KeyFilterFlag_V4			= $0400;
  KeyFilterFlag_V3			= $0800;

  // the groups can be combined
  KeyFilterMask_Boolean			= $00FF;
  KeyFilterMask_Algorithm		= $FF00;

const
  // pre-selection flag for key import
  IgnoreFlag_ByHexID			= 1;
  IgnoreFlag_ByUserID			= 2;

type
  TKeyProp = (
    KeyProp_HexID,
    KeyProp_UserID,
    KeyProp_Fingerprint,
    KeyProp_CreaTimeStr,
    KeyProp_ExpTimeStr,
    KeyProp_Size,
    KeyProp_Algorithm,
    KeyProp_Trust,
    KeyProp_Validity,
    KeyProp_CreaTimeNum,
    KeyProp_ExpTimeNum,
    KeyProp_Secret,
    KeyProp_ImplicitTrust,
    KeyProp_Revoked,
    KeyProp_Disabled,
    KeyProp_Expired,
    KeyProp_SecretShared,
    KeyProp_CanEncrypt,
    KeyProp_CanDecrypt,
    KeyProp_CanSign,
    KeyProp_CanVerify,
    KeyProp_HasRevoker,
    KeyProp_HasADK,
    KeyProp_HasSubKey,
    KeyProp_LegacyKey,
    KeyProp_IncludeUserIDs,
    KeyProp_IncludeSignerIDs,
    KeyProp_IncludeGroupsList
  );
  TKeyProps = Set of TKeyProp;
  TPGPKeyOrdering = (
    Invalid_Ordering,
    Any_Ordering,
    UserID_Ordering,
    ReverseUserID_Ordering,
    KeyID_Ordering,
    ReverseKeyID_Ordering,
    Validity_Ordering,
    ReverseValidity_Ordering,
    Trust_Ordering,
    ReverseTrust_Ordering,
    EncryptKeySize_Ordering,
    ReverseEncryptKeySize_Ordering,
    SigKeySize_Ordering,
    ReverseSigKeySize_Ordering,
    Creation_Ordering,
    ReverseCreation_Ordering,
    Expiration_Ordering,
    ReverseExpiration_Ordering
  );
  TTrustLevel = (
    KeyTrust_Undefined,
    KeyTrust_Unknown,
    KeyTrust_Never,
    KeyTrust_Reserved1,
    KeyTrust_Reserved2,
    KeyTrust_Marginal,
    KeyTrust_Complete,
    KeyTrust_Ultimate
  );
  TValidityLevel = (
    Validity_Unknown,
    Validity_Invalid,
    Validity_Marginal,
    Validity_Complete
  );
  TKeyAlgorithm = (
    KeyAlgorithm_Invalid,
    KeyAlgorithm_RSA,
    KeyAlgorithm_RSAEncryptOnly,
    KeyAlgorithm_RSASignOnly,
    KeyAlgorithm_Reserved01,
    KeyAlgorithm_Reserved02,
    KeyAlgorithm_Reserved03,
    KeyAlgorithm_Reserved04,
    KeyAlgorithm_Reserved05,
    KeyAlgorithm_Reserved06,
    KeyAlgorithm_Reserved07,
    KeyAlgorithm_Reserved08,
    KeyAlgorithm_Reserved09,
    KeyAlgorithm_Reserved10,
    KeyAlgorithm_Reserved11,
    KeyAlgorithm_Reserved12,
    KeyAlgorithm_DH,
    KeyAlgorithm_DSS,
    KeyAlgorithm_DHDSS
  );
  TCipherAlgorithm = (
    CipherAlgorithm_None,
    CipherAlgorithm_IDEA,
    CipherAlgorithm_3DES,
    CipherAlgorithm_CAST5,
    CipherAlgorithm_AES128,
    CipherAlgorithm_AES192,
    CipherAlgorithm_AES256,
    CipherAlgorithm_Twofish256
  );
  TADKType = (
    NoADK,
    SimpleADK,
    EnforcedADK
  );

  TUserIDs = class(TStringList)
  private
    function GetValidity(Index: Longint): TValidityLevel;
  public
    property Validities[Index: Longint]: TValidityLevel read GetValidity;
  end;

  TSignerIDs = TStringList;
  TADKeyIDs = TStringList;
  TRevKeyIDs = TStringList;
  TGroupsList = TStringList;

  pKeyPropsRec = ^TKeyPropsRec;
  TKeyPropsRec = Record
    kHexID: String;
    kUserID: String;
    kFingerprint: String;
    kCreaTimeStr: String;
    kExpTimeStr: String;
    kSize: String;
    kAlgorithm: TKeyAlgorithm;
    kTrust: TTrustLevel;
    kValidity: TValidityLevel;
    kCreaTimeNum: Longint;
    kExpTimeNum: Longint;
    kPrivate: Longbool;
    kImplicitTrust: Longbool;
    kRevoked: Longbool;
    kDisabled: Longbool;
    kExpired: Longbool;
    kSecShared: Longbool;
    kCanEncrypt: Longbool;
    kCanDecrypt: Longbool;
    kCanSign: Longbool;
    kCanVerify: Longbool;
    kHasRevoker: Longbool;
    kHasADK: TADKType;
    kHasSubKey: Longbool;
    kLegacyKey: Longbool;
    kUserIDList: TUserIDs;
    kSignerIDList: TSignerIDs;
    kADKeyIDList: TADKeyIDs;
    kRevKeyIDList: TRevKeyIDs;
  end;

  pKeyPropsList = ^TKeyPropsList;
  TKeyPropsList = class(TStringList)
  private
    FDuplicates: Longbool;
    FValidProps: TKeyProps;
    IncludeUserIDs: Longbool;
    IncludeSignerIDs: Longbool;
    IncludeADKeyIDs: Longbool;
    IncludeRevKeyIDs: Longbool;
    function	AllocKeyPropsRec: pKeyPropsRec;
    function	GetKeyProps(Index: Longint): TKeyPropsRec;
    function	FreeKeyPropsRec(Index: Longint): pKeyPropsRec;
    function	GetKeyPropsSet(KeyPropsFlag: DWord): TKeyProps;
  public
    GroupsList:	TGroupsList;
    constructor	Create(ItemCount: Cardinal; PropertyFlags: DWord);
    destructor	Destroy; override;
    procedure	Clear; override;
    procedure	Delete(Index: Integer); override;
    procedure	Move(CurIndex, NewIndex: Integer); override;
    function	Add(const S: String): Integer; override;
    {$IFNDEF FIXED}
    function	AddObject(const S: String; AObject: TObject): Integer; override;
    {$ENDIF}
    procedure	Insert(Index: Integer; const S: String); override;
    // returns first free item index
    function	Append(ItemCount: Cardinal): Integer;
    // returns true if requested record item exists
    function	GetKeyPropsRec(var KeyPropsRec: TKeyPropsRec; Index: Integer): Longbool;
    // access indexed KeyProps record like Strings[] or Objects[]
    property	KeyProps[Index: Longint]: TKeyPropsRec read GetKeyProps;
    // indicates accessable key properties for the respective list
    property	ValidProps: TKeyProps read FValidProps;
    // just a dummy to prevent using the inherited property
    property	Duplicates: Longbool read FDuplicates;
  end;

implementation

function TUserIDs.GetValidity(Index: Longint): TValidityLevel;
begin
  Result:=TValidityLevel(Objects[Index]);
end;

function TKeyPropsList.AllocKeyPropsRec: pKeyPropsRec;
begin
  New(Result);
  if Result<>nil then begin
    FillChar(Result^, SizeOf(TKeyPropsRec), 0);
    if IncludeUserIDs then Result.kUserIDList:=TUserIDs.Create;
    if IncludeSignerIDs then Result.kSignerIDList:=TSignerIDs.Create;
    if IncludeADKeyIDs then Result.kADKeyIDList:=TADKeyIDs.Create;
    if IncludeRevKeyIDs then Result.kRevKeyIDList:=TRevKeyIDs.Create;
  end;
end;

function TKeyPropsList.GetKeyProps(Index: Longint): TKeyPropsRec;
begin
  Result:=pKeyPropsRec(Objects[Index])^;
end;

function TKeyPropsList.FreeKeyPropsRec(Index: Longint): pKeyPropsRec;
begin
  Result:=pKeyPropsRec(Objects[Index]);
  if Result<>nil then begin
    Result^.kUserIDList.Free;
    Result^.kSignerIDList.Free;
    Result^.kADKeyIDList.Free;
    Result^.kRevKeyIDList.Free;
    Dispose(Result);
    Result:=nil;
  end;
end;

function TKeyPropsList.GetKeyPropsSet(KeyPropsFlag: DWord): TKeyProps;
begin
  Result:=[];
  if KeyPropsFlag and spgpKeyPropFlag_KeyID<>0 then Include(Result, KeyProp_HexID);
  if KeyPropsFlag and spgpKeyPropFlag_UserID<>0 then  Include(Result, KeyProp_UserID);
  if KeyPropsFlag and spgpKeyPropFlag_Fingerprint<>0 then Include(Result, KeyProp_Fingerprint);
  if KeyPropsFlag and spgpKeyPropFlag_CreationTimeStr<>0 then Include(Result, KeyProp_CreaTimeStr);
  if KeyPropsFlag and spgpKeyPropFlag_ExpirationTimeStr<>0 then Include(Result, KeyProp_ExpTimeStr);
  if KeyPropsFlag and spgpKeyPropFlag_KeyBits<>0 then Include(Result, KeyProp_Size);
  if KeyPropsFlag and spgpKeyPropFlag_KeyAlg<>0 then Include(Result, KeyProp_Algorithm);
  if KeyPropsFlag and spgpKeyPropFlag_Trust<>0 then Include(Result, KeyProp_Trust);
  if KeyPropsFlag and spgpKeyPropFlag_Validity<>0 then Include(Result, KeyProp_Validity);
  if KeyPropsFlag and spgpKeyPropFlag_CreationTime<>0 then Include(Result, KeyProp_CreaTimeNum);
  if KeyPropsFlag and spgpKeyPropFlag_ExpirationTime<>0 then Include(Result, KeyProp_ExpTimeNum);
  if KeyPropsFlag and spgpKeyPropFlag_IsSecret<>0 then Include(Result, KeyProp_Secret);
  if KeyPropsFlag and spgpKeyPropFlag_IsAxiomatic<>0 then Include(Result, KeyProp_ImplicitTrust);
  if KeyPropsFlag and spgpKeyPropFlag_IsRevoked<>0 then Include(Result, KeyProp_Revoked);
  if KeyPropsFlag and spgpKeyPropFlag_IsDisabled<>0 then Include(Result, KeyProp_Disabled);
  if KeyPropsFlag and spgpKeyPropFlag_IsExpired<>0 then Include(Result, KeyProp_Expired);
  if KeyPropsFlag and spgpKeyPropFlag_IsSecretShared<>0 then Include(Result, KeyProp_SecretShared);
  if KeyPropsFlag and spgpKeyPropFlag_CanEncrypt<>0 then Include(Result, KeyProp_CanEncrypt);
  if KeyPropsFlag and spgpKeyPropFlag_CanDecrypt<>0 then Include(Result, KeyProp_CanDecrypt);
  if KeyPropsFlag and spgpKeyPropFlag_CanSign<>0 then Include(Result, KeyProp_CanSign);
  if KeyPropsFlag and spgpKeyPropFlag_CanVerify<>0 then Include(Result, KeyProp_CanVerify);
  if KeyPropsFlag and spgpKeyPropFlag_HasRevoker<>0 then Include(Result, KeyProp_HasRevoker);
  if KeyPropsFlag and spgpKeyPropFlag_HasADK<>0 then Include(Result, KeyProp_HasADK);
  if KeyPropsFlag and spgpKeyPropFlag_HasSubKey<>0 then Include(Result, KeyProp_HasSubKey);
  if KeyPropsFlag and spgpKeyPropFlag_LegacyKey<>0 then Include(Result, KeyProp_LegacyKey);
  if KeyPropsFlag and spgpKeyPropFlag_IncludeUserIDs<>0 then Include(Result, KeyProp_IncludeUserIDs );
  if KeyPropsFlag and spgpKeyPropFlag_IncludeSignerIDs<>0 then Include(Result, KeyProp_IncludeSignerIDs);
  if KeyPropsFlag and spgpKeyPropFlag_IncludeGroupsList<>0 then Include(Result, KeyProp_IncludeGroupsList);
end;

constructor TKeyPropsList.Create(ItemCount: Cardinal; PropertyFlags: DWord);
var Index: Cardinal;
begin
  inherited Create;
  GroupsList:=TGroupsList.Create;
  IncludeUserIDs:=((PropertyFlags and spgpKeyPropFlag_IncludeUserIDs)<>0);
  IncludeSignerIDs:=((PropertyFlags and spgpKeyPropFlag_IncludeSignerIDs)<>0);
  IncludeADKeyIDs:=((PropertyFlags and spgpKeyPropFlag_HasADK)<>0);
  IncludeRevKeyIDs:=((PropertyFlags and spgpKeyPropFlag_HasRevoker)<>0);
  {$IFDEF FIXED}
  for Index:=1 to ItemCount do AddObject('', TObject(AllocKeyPropsRec));
  {$ELSE}
  for Index:=1 to ItemCount do Objects[inherited Add('')]:=TObject(AllocKeyPropsRec);
  {$ENDIF}
  FValidProps:=GetKeyPropsSet(PropertyFlags);
end;

destructor TKeyPropsList.Destroy;
var Index: Integer;
begin
  try
    for Index:=0 to pred(Count) do FreeKeyPropsRec(Index);
  finally
    try
      GroupsList.Free;
    finally
      inherited Destroy;
    end;
  end;
end;

procedure TKeyPropsList.Clear;
var Index: Integer;
begin
  try
    for Index:=0 to pred(Count) do FreeKeyPropsRec(Index);
  finally
    try
      GroupsList.Clear;
    finally
      inherited Clear;
    end;
  end;
end;

procedure TKeyPropsList.Delete(Index: Integer);
begin
  FreeKeyPropsRec(Index);
  inherited Delete(Index);
end;

procedure TKeyPropsList.Move(CurIndex, NewIndex: Integer);
var TempStr: String; TempObj: TObject;
begin
  if CurIndex<>NewIndex then begin
    TempStr:=Strings[CurIndex];
    TempObj:=Objects[CurIndex];
    inherited Delete(CurIndex);
    InsertObject(NewIndex, TempStr, TempObj);
  end;
end;

function TKeyPropsList.Add(const S: String): Integer;
begin
  {$IFDEF FIXED}
  Result:=AddObject(S, TObject(AllocKeyPropsRec));
  {$ELSE}
  Result:=inherited Add(S);
  Objects[Result]:=TObject(AllocKeyPropsRec);
  {$ENDIF}
end;

{$IFNDEF FIXED}
function TKeyPropsList.AddObject(const S: String; AObject: TObject): Integer;
begin
  Result:=inherited Add(S);
  Objects[Result]:=AObject;
end;
{$ENDIF}

procedure TKeyPropsList.Insert(Index: Integer; const S: String);
begin
  inherited Insert(Index, S);
  Objects[Index]:=TObject(AllocKeyPropsRec);
end;

function TKeyPropsList.Append(ItemCount: Cardinal): Integer;
var Index: Cardinal;
begin
  Result:=Count;
  try
    {$IFDEF FIXED}
    if ItemCount>0 then for Index:=1 to ItemCount do AddObject('', TObject(AllocKeyPropsRec));
    {$ELSE}
    if ItemCount>0 then for Index:=1 to ItemCount do Objects[inherited Add('')]:=TObject(AllocKeyPropsRec);
    {$ENDIF}
  except
    Result:=-1;
  end;
end;

function TKeyPropsList.GetKeyPropsRec(var KeyPropsRec: TKeyPropsRec; Index: Integer): Longbool;
begin
  Result:=false;
  FillChar(KeyPropsRec, SizeOf(TKeyPropsRec), 0);
  if (Index<Count) and (Objects[Index]<>nil) then begin
    KeyPropsRec:=pKeyPropsRec(Objects[Index])^;
    Result:=true;
  end;
end;

end.

⌨️ 快捷键说明

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