Examining a Digital Signature Example for VB.NET

Function GetFormatted(ByRef sValue As String, ByRef nWidth As Short) As String
   GetFormatted = sValue
   Do While Len(GetFormatted) < nWidth
      GetFormatted = "0" & GetFormatted
   Loop
End Function

 

Sub ExamineSignature(ByRef objDS As LTDICLib.LEADDicomDS, ByRef hSignatureItem As Integer)
   Dim nRet As Short
   objDS.SetCurrentElement(hSignatureItem)

   ' Verify the Digital Signature; if hSignatureItem is 0, the method will
   ' verify all the Digital Signatures that exist in the Data Set
   nRet = objDS.VerifySignature()
   Select Case nRet
      Case LTDicomKernelLib.DicomErrorCodes.DICOM_SUCCESS
         If hSignatureItem <> 0 Then
            MessageBox.Show("The Digital Signature was verified.", "Sample")
         Else
            MessageBox.Show("All Digital Signatures were verified (if there are any).", "Sample")
            Exit Sub
         End If

      Case LTDicomKernelLib.DicomErrorCodes.DICOM_ERROR_INVALID_SIGNATURE
         If hSignatureItem <> 0 Then
            MessageBox.Show("The Digital Signature is invalid.", "Sample")
         Else
            MessageBox.Show("At least one Digital Signature is invalid.", "Sample")
         End If
         Exit Sub

      Case Else
         MessageBox.Show("An error occurred [Error: " & nRet & "].", "Sample")
         Exit Sub
   End Select

   Dim sMsg As String

   ' The Digital Signature UID
   If objDS.GetSignatureUID() = LTDicomKernelLib.DicomErrorCodes.DICOM_SUCCESS Then
      If objDS.StringValueCount > 0 Then
         sMsg = "Digital Signature UID: " & objDS.StringValues(0) & vbNewLine
      End If
   End If

   ' The Digital Signature DateTime
   Dim sSign As String
   If objDS.GetSignatureDateTime() = LTDicomKernelLib.DicomErrorCodes.DICOM_SUCCESS Then
      If objDS.DateTimeValueCount > 0 Then
         With objDS.DateTimeValues(0)
            If .Offset >= 0 Then
               sSign = "+"
            Else sSign = "-"
            sMsg = sMsg & "Digital Signature DateTime: " & GetFormatted(CStr(.Month), 2) & "/" & GetFormatted(CStr(.Day), 2) & "/" & GetFormatted(CStr(.Year), 4) & " " & GetFormatted(CStr(.Hours), 2) & ":" & GetFormatted(CStr(.Minutes), 2) & ":" & GetFormatted(CStr(.Seconds), 2) & "." & GetFormatted(CStr(.Fractions), 6) & " " & sSign & GetFormatted(CStr(.Offset), 4) & vbNewLine
         End With
      End If
   End If

   ' The MAC Calculation Transfer Syntax UID
   If objDS.GetMacTransferSyntax() = LTDicomKernelLib.DicomErrorCodes.DICOM_SUCCESS Then
      If objDS.StringValueCount > 0 Then
         sMsg = sMsg & "MAC Calculation Transfer Syntax UID: " & objDS.StringValues(0) & vbNewLine
      End If
   End If

   ' The MAC Algorithm
   If objDS.GetMacAlgorithm() = LTDicomKernelLib.DicomErrorCodes.DICOM_SUCCESS Then
      If objDS.StringValueCount > 0 Then
         sMsg = sMsg & "MAC Algorithm: " &
         objDS.StringValues(0) & vbNewLine
      End If
   End If

   ' The Data Elements Signed
   Dim sGroup, sTag, sElement As String
   If objDS.GetSignedElementsCount() > 0 Then
      sMsg = sMsg & "Data Elements Signed: "

      ' We will display only one
      If objDS.MoveSignedElement(0) = LTDicomKernelLib.DicomErrorCodes.DICOM_SUCCESS Then

         sTag = Hex(objDS.CurrentElement.Tag)
         sElement = sTag.Substring(sTag.Length - 4)
         sGroup = sTag.Substring(0, Len(sTag) - 4)

         sMsg = sMsg & "(" & GetFormatted(sGroup, 4) & "," & sElement & "), ..."

         objDS.SetCurrentElement(hSignatureItem)
      End If

      sMsg = sMsg & vbNewLine
   End If

   sMsg = sMsg & vbNewLine & "Do you want to save the Certificate of Signer?"

   ' Display the information we have about the Digital Signature
   If MessageBox.Show(sMsg, "Sample", MessageBoxButtons.YesNo) = DialogResult.Yes Then
      ' Save the Certificate of Signer
      objDS.SaveCertificate("C:\CertOfSigner.cer")
   End If
End Sub