Examining a Digital Signature Example for Visual Basic
Function GetFormatted(sValue As String, nWidth As Integer) As String
GetFormatted = sValue
Do While Len(GetFormatted) < nWidth
GetFormatted = "0" & GetFormatted
Loop
End Function
Sub ExamineSignature(objDS As LEADDicomDS, hSignatureItem As Long)
Dim nRet As Integer
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 DICOM_SUCCESS
If hSignatureItem <> 0 Then
MsgBox "The Digital Signature was verified.", , "Sample"
Else
MsgBox "All Digital Signatures were verified (if there are any).", , _
"Sample"
Exit Sub
End If
Case DICOM_ERROR_INVALID_SIGNATURE
If hSignatureItem <> 0 Then
MsgBox "The Digital Signature is invalid.", , "Sample"
Else
MsgBox "At least one Digital Signature is invalid.", , "Sample"
End If
Exit Sub
Case Else
MsgBox "An error occurred [Error: " & nRet & "].", , "Sample"
Exit Sub
End Select
Dim sMsg As String
' The Digital Signature UID
If objDS.GetSignatureUID () = DICOM_SUCCESS Then
If objDS.StringValueCount > 0 Then
sMsg = "Digital Signature UID: " & objDS.StringValues(0) & vbNewLine
End If
End If
' The Digital Signature DateTime
If objDS.GetSignatureDateTime () = DICOM_SUCCESS Then
If objDS.DateTimeValueCount > 0 Then
With objDS.DateTimeValues(0)
Dim sSign As String
If .Offset >= 0 Then sSign = "+" Else sSign = "-"
sMsg = sMsg & "Digital Signature DateTime: " & _
GetFormatted(.Month, 2) & "/" & _
GetFormatted(.Day, 2) & "/" & _
GetFormatted(.Year, 4) & " " & _
GetFormatted(.Hours, 2) & ":" & _
GetFormatted(.Minutes, 2) & ":" & _
GetFormatted(.Seconds, 2) & "." & _
GetFormatted(.Fractions, 6) & " " & _
sSign & _
GetFormatted(.Offset, 4) & _
vbNewLine
End With
End If
End If
' The MAC Calculation Transfer Syntax UID
If objDS.GetMacTransferSyntax() = 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 () = DICOM_SUCCESS Then
If objDS.StringValueCount > 0 Then
sMsg = sMsg & "MAC Algorithm: " & objDS.StringValues(0) & vbNewLine
End If
End If
' The Data Elements Signed
If objDS.GetSignedElementsCount () > 0 Then
sMsg = sMsg & "Data Elements Signed: "
' We will display only one
If objDS.MoveSignedElement(0) = DICOM_SUCCESS Then
Dim sTag As String, sGroup As String, sElement As String
sTag = Hex(objDS.CurrentElement.Tag)
sElement = Right(sTag, 4)
sGroup = Left(sTag, 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 MsgBox(sMsg, vbYesNo, "Sample") = vbYes Then
' Save the Certificate of Signer
objDS.SaveCertificate "C:\CertOfSigner.cer"
End If
End Sub