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

    ' Digital signature creation example for VB
    ' Perform processing in 8K (8192 byte) chunks. 

    Const CHUNKSIZE = 8192 

    Dim xSign As New XceedSigning ' Instantiate the object
    Call xSign.License( "your license key" ) 

    Dim lOffSet As Long ' We manage the read offset in the file
    Dim lFileSize As Long
    Dim lBytesRead As Long
    Dim vaSignature As Variant ' Here is where the signature will go 

    ' Set the signing method type 
    Set xSign.SigningMethod = New XceedRSASigningMethod 

    On Error Resume Next 

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

    Call xSign.SigningMethod.SetRandomKeyPair(512, Empty) 

    ' Set up to read from the file 

    lOffSet = 0 ' We must track the current offset
    lFileSize = FileLen("c:\temp\source.txt") 

    While lOffSet < lFileSize
      ' bEndOfData will be true if the current offset + CHUNKSIZE exceeds 
      ' the end of the file.  
      lBytesRead = xSign.ReadFile("c:\temp\source.txt", lOffSet, CHUNKSIZE, efpSign, _
                                  (lOffSet + CHUNKSIZE >= lFileSize)) 

      lOffSet = lOffSet + lBytesRead 
    Wend 

    If Err.Number = 0 Then
      Call MsgBox("Finished Signing file to memory.") 
      vaSignature = xSign.SigningMethod.Signature 

      ' Do something with the signature... 
      lOffSet = 0 
      While lOffSet < lFileSize And Err.Number = 0 
        ' bEndOfData will be true if the current offset + CHUNKSIZE exceeds 
        ' the end of the file. 

        lBytesRead = xSign.ReadFile("c:\temp\source.txt", lOffSet, CHUNKSIZE, efpVerify, _
                                    (lOffSet + CHUNKSIZE >= lFileSize)) 

        lOffSet = lOffSet + lBytesRead 
      Wend  

      If Err.Number = 0 Then 
        Call MsgBox("Data verified successfully") 
      Else 
        If Err.Number = eerVerifyFailed Then 
          Call MsgBox("Verification of signature failed") 
        Else 
          Call MsgBox("Error 0x" & Hex(Err.Number) & " " & Err.Description) 
        End If 
      End If 
    Else
      Call MsgBox("Error 0x" & Hex(Err.Number) & " " & Err.Description) 
    End If 

    On Error GoTo 0 

    Set xSign = Nothing