Examining a Digital Signature Example for C#
public string GetFormatted(string sValue, short nWidth)
{
string tempGetFormatted = null;
tempGetFormatted = sValue;
while (tempGetFormatted.Length < nWidth)
{
tempGetFormatted = "0" +
tempGetFormatted;
}
return tempGetFormatted;
}
public void ExamineSignature(LTDICLib.LEADDicomDS objDS,
int hSignatureItem)
{
short nRet = 0;
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(0);
switch ( nRet )
{
case (short)LTDicomKernelLib.DicomErrorCodes.DICOM_SUCCESS:
{
if (hSignatureItem
!= 0)
MessageBox.Show("The
Digital Signature was verified.", "Sample");
else
{
MessageBox.Show("All
Digital Signatures were verified (if there are any).", "Sample");
return;
}
break;
}
case (short)LTDicomKernelLib.DicomErrorCodes.DICOM_ERROR_INVALID_SIGNATURE:
{
if (hSignatureItem
!= 0)
MessageBox.Show("The
Digital Signature is invalid.", "Sample");
else
MessageBox.Show("At
least one Digital Signature is invalid.", "Sample");
return;
}
default:
{
MessageBox.Show("An
error occurred [Error: " + nRet + "].", "Sample");
return;
}
}
string sMsg = null;
// The Digital Signature UID
if (objDS.GetSignatureUID()
== (short)LTDicomKernelLib.DicomErrorCodes.DICOM_SUCCESS)
{
if (objDS.StringValueCount
> 0)
sMsg = "Digital
Signature UID: " + objDS.get_StringValues(0)
+ System.Environment.NewLine;
}
// The Digital Signature DateTime string sSign = null;
if (objDS.GetSignatureDateTime()
== (short)LTDicomKernelLib.DicomErrorCodes.DICOM_SUCCESS)
{
if (objDS.DateTimeValueCount > 0)
{
if (objDS.get_DateTimeValues(0).Offset
>= 0)
sSign = "+";
else
sSign = "-";
short tempnWidth1
= 2;
short tempnWidth2
= 2;
short tempnWidth3
= 4;
short tempnWidth4
= 2;
short tempnWidth5
= 2;
short tempnWidth6
= 2;
short tempnWidth7
= 6;
short tempnWidth8
= 4;
sMsg = sMsg + "Digital
Signature DateTime: " + GetFormatted(System.Convert.ToString(objDS.get_DateTimeValues(0).Month),
tempnWidth1) + "/" + GetFormatted(System.Convert.ToString(objDS.get_DateTimeValues(0).Day),
tempnWidth2) + "/" + GetFormatted(System.Convert.ToString(objDS.get_DateTimeValues(0).Year),
tempnWidth3) + " " + GetFormatted(System.Convert.ToString(objDS.get_DateTimeValues(0).Hours),
tempnWidth4) + ":" + GetFormatted(System.Convert.ToString(objDS.get_DateTimeValues(0).Minutes),
tempnWidth5) + ":" + GetFormatted(System.Convert.ToString(objDS.get_DateTimeValues(0).Seconds),
tempnWidth6) + "." + GetFormatted(System.Convert.ToString(objDS.get_DateTimeValues(0).Fractions),
tempnWidth7) + " " + sSign + GetFormatted(System.Convert.ToString(objDS.get_DateTimeValues(0).Offset),
tempnWidth8) + System.Environment.NewLine;
}
}
// The MAC Calculation Transfer Syntax UID
if (objDS.GetMacTransferSyntax()
== (short)LTDicomKernelLib.DicomErrorCodes.DICOM_SUCCESS)
{
if (objDS.StringValueCount
> 0)
sMsg = sMsg + "MAC
Calculation Transfer Syntax UID: " + objDS.get_StringValues(0)
+ System.Environment.NewLine;
}
// The MAC Algorithm
if (objDS.GetMacAlgorithm()
== (short)LTDicomKernelLib.DicomErrorCodes.DICOM_SUCCESS)
{
if (objDS.StringValueCount
> 0)
sMsg = sMsg + "MAC
Algorithm: " + objDS.get_StringValues(0)
+ System.Environment.NewLine;
}
// The Data Elements Signed string
sGroup = null;
string sTag = null;
string sElement = null;
if (objDS.GetSignedElementsCount()
> 0)
{
sMsg = sMsg + "Data Elements Signed:
";
// We will display only one
if (objDS.MoveSignedElement(0)
== (short)LTDicomKernelLib.DicomErrorCodes.DICOM_SUCCESS)
{
sTag = System.Convert.ToString(objDS.get_CurrentElement().Tag,
16).ToUpper();
sElement = sTag.Substring(sTag.Length
- 4);
sGroup = sTag.Substring(0,
sTag.Length - 4);
short tempnWidth9
= 4;
sMsg = sMsg + "("
+ GetFormatted(sGroup, tempnWidth9) + "," + sElement + "),
...";
objDS.SetCurrentElement(hSignatureItem);
}
sMsg = sMsg + System.Environment.NewLine;
}
sMsg = sMsg + System.Environment.NewLine + "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)
{
// Save the Certificate of Signer
objDS.SaveCertificate("C:\\CertOfSigner.cer",
LTDICLib.DicomCertificateFormat.DICOM_CERTIFICATE_FORMAT_PEM);
}
}