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

    // This example hashes data using the SHA method
    // from a file to memory in 8K (8192 byte) chunks. 

    uses XceedEncryptionLib_TLB

    const CHUNKSIZE = 8192; 
    var
      xHash : TXceedHashing; 
      xSHA : DXceedSHAHashingMethod; 
      vaBytesRead : OleVariant; 
      vaHashValue : OleVariant; 
      lOffset : Longint; 
      lFileSize : Longint; 
      SourceFile : file of byte; 
    begin
      xHash := TXceedHashing.Create( self ); 
      xHash.License( 'your license key' ); 

      xSHA := CoXceedSHAHashingMethod.Create(); 
      try 
        xSHA.HashSize := 256; 
        xHash.HashingMethod := xSHA;  

        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 := xHash.ReadFile( 'c:\temp\source.txt', lOffset, CHUNKSIZE, efpHash,
                                        (lOffset + CHUNKSIZE >= lFileSize) );  

          lOffset := lOffset + vaBytesRead; 
        end;  

        vaHashValue := xSHA.HashValue; 

        { Do something with the HashValue... }  

        ShowMessage( 'Hashing successful!' ); 
      except 
        on xErr : Exception do 
          ShowMessage( xErr.Message ); 
      end;  

      xHash.Free; 
    end;