Xceed Encryption Library Documentation
Examples / Digitally signing a file (Delphi streaming example)
In This Topic
    Digitally signing a file (Delphi streaming example)
    In This Topic
    Delphi Copy Code

    { This example signs data using the RSA method from a file to memory in 8K (8192 byte) chunks. }  

    uses XceedEncryptionLib_TLB

    const CHUNKSIZE = 8192; 

    var
      xSign : TXceedSigning; 
      xRSA : DXceedRSASigningMethod; 
      vaBytesRead : OleVariant; 
      vaSignature : OleVariant; 
      vaSeed : OleVariant; 
      lOffset : Longint; 
      lFileSize : Longint; 
      SourceFile : file of byte; 
    begin
      xSign := TXceedSigning.Create( self ); 
      xSign.License( 'your license key' ); 

      xRSA := CoXceedRSASigningMethod.Create();  

      try 
        { The following call to SetRandomKeyPair would, in the real world,  
          be replaced by the assignment of a Private Key to the  
          PrivateKey property. }  

        vaSeed := varEmpty; 

        xRSA.SetRandomKeyPair( 512, vaSeed ); 

        xSign.SigningMethod := xRSA;  

        AssignFile( SourceFile, 'c:\temp\source.txt' ); 

        Reset( SourceFile ); 

        lFileSize := FileSize( SourceFile ); 

        CloseFile( SourceFile ); 

        lOffset := 0;  

        while lOffset < lFileSize do 
        begin 
          { bEndOfData will be true if the current offset + CHUNKSIZE exceeds the end of the file. } 
          vaBytesRead := xSign.ReadFile( 'c:\temp\source.txt', lOffset, CHUNKSIZE, efpSign, 
                                         (lOffset + CHUNKSIZE >= lFileSize) ); 

          lOffset := lOffset + vaBytesRead; 
        end;  

        vaSignature := xRSA.Get_Signature;  

        { Do something with the Signature... }  

        ShowMessage( 'Signing successful!' ); 
        lOffset := 0;  

        while lOffset < lFileSize do 
        begin 
          { bEndOfData will be true if the current offset + CHUNKSIZE exceeds the end of the file. } 
          vaBytesRead := xSign.ReadFile( 'c:\temp\source.txt', lOffset, CHUNKSIZE, efpVerify,
                                         (lOffset + CHUNKSIZE >= lFileSize) ); 

          lOffset := lOffset + vaBytesRead; 
        end;  

        ShowMessage( 'Data verified successfully' ); 
      except 
        on xErr : Exception do 
          ShowMessage( xErr.Message ); 
      end;  

      xSign.Free; 
    end;