dmtSigningFile

Firma un file utilizzando il certificato caricato con dmtLoadCertificate e settato come quello da utilizzare attraverso la funzione dmtSetDefaultCertificate.

 

Da notare:

 

 

Dichiarazione - C

LONG WINAPI  dmtSigningFile (

HDMTOBJ hDmt,

CHAR *szFileName,

CHAR *szAlgHash = szOID_OIWSEC_sha1,

CHAR *szAlgEncrypt = szOID_RSA_RC4,

BOOL fDetachSign = FALSE,

BOOL fEncrypt = FALSE,

BOOL fInclRecipientInMsg = FALSE,

BOOL fUseAllRecipients = FALSE,

CHAR *szPin = "",

BOOL fAddDsiTimeRef = FALSE,

PCHAR szSigningTime = "",

PCHAR szFormat = "");

 

Dichiarazione - Visual Dataflex

External_Function dmtSigningFile "dmtSigningFile" Dimatec.dll ;

Handle hDmt ;

String szFileName ;

String szAlgHash ;

String szAlgEncrypt ;

Integer fDetachSign ;

Integer fEncrypt ;

Integer fInclRecipientInMsg ;

Integer fUseAllRecipients ;

String szPin ;

Integer fAddDsiTimeRef ;

String szSigningTime  ;

String szFormat

Returns Integer

 

Dichiarazione - Visual Basic

Public Declare Function dmtSigningFile Lib "Dimatec.dll" _

(ByVal hDmt As Long, _

ByVal szFileName As String, _

Optional ByVal szAlgHash As String = szOID_OIWSEC_sha1, _

Optional ByVal szAlgEncrypt As String = szOID_RSA_RC4, _

Optional ByVal fDetachSign As Boolean = False, _

Optional ByVal fEncrypt As Boolean = False, _

Optional ByVal fInclRecipientInMsg As Boolean = False, _

Optional ByVal fUseAllRecipients As Boolean = False, _

Optional ByVal szPin As String = vbNullString, _

Optional ByVal fAddDsiTimeRef As Boolean = False, _

Optional ByVal szSigningTime  As String = vbNullString, _

Optional ByVal szFormat As String = vbNullString, _

As Long

 

Dichiarazione - C Sharp

[DllImport("Dimatec.dll")]

public static extern int dmtSigningFile (

int hDmt,

string szFileName,

string szAlgHash /*= szOID_OIWSEC_sha1*/,

string szAlgEncrypt /*= szOID_RSA_RC4*/,

bool fDetachSign /*= false */,

bool fEncrypt /*= false */,

bool fInclRecipientInMsg /*= false */,

bool fUseAllRecipients /*= false */,

string szPin /*= ""*/,

bool fAddDsiTimeRef /*= false */,

string szSigningTime /*= ""*/,

string szFormat /*= ""*/);

 

Parametri di input:

 

Valore di ritorno:

DMT_OK in caso di successo, altrimenti un codice di errore

 

Se la chiamata ha successo verrà creato un file firmato con la seguente estensione:

 

 


Esempi di utilizzo

 

Visual Dataflex

Integer ret

 

Integer iDefaultPersonalCertificateCount

Integer iDefaultPersonalCertificate

Integer iDefaultRecipientCertificateCount

Integer iDefaultRecipientCertificate

                

String sFileDaFirmare

String sPin

                

Get psFileDaFirmare of oFirmaUnFile to sFileDaFirmare

                

// Check

If (Length(Trim(psFileDaFirmare(oFirmaUnFile))) = 0) Begin

    Send Stop_Box "Any file selected to sign" "EseguiFirma"

    Procedure_Return

End

                

Move (dmtGetCertificateCountLoaded( ;

    ghDmt , ;

    False)) to iDefaultPersonalCertificateCount

                    

If (iDefaultPersonalCertificateCount <= 0) Begin

    Send Stop_Box "Personal certificate not loaded" "EseguiFirma"

    Procedure_Return

End

Else Begin

    Move (dmtGetDefaultCertificateLoaded( ;

        ghDmt , ;

        False)) to iDefaultPersonalCertificate

                        

    If (iDefaultPersonalCertificate < 0) Begin

        Send Stop_Box "Default personal certificate not set" "EseguiFirma"

        Procedure_Return

    End

End

                

If (Checked_State(oCheckBox_SignEncrypt)) Begin

    // If I want to encrypt the content of the signed file

    // I need to use a recipient certificate

    Move (dmtGetCertificateCountLoaded( ;

        ghDmt , ;

        True)) to iDefaultRecipientCertificateCount

                        

    If (iDefaultRecipientCertificateCount <= 0) Begin

        Send Stop_Box "Recipient certificate not loaded" "EseguiFirma"

        Procedure_Return

    End

    Else Begin

        If (Checked_State(oCheckBox_SignEncryptAllRecipient)) Begin

            // If I don't want to use all recipient certificate

            // I need to set the default recipient certificate

            Move (dmtGetDefaultCertificateLoaded( ;

                ghDmt , ;

                True)) to iDefaultRecipientCertificate

                                

            If (iDefaultRecipientCertificate < 0) Begin

                Send Stop_Box "Default recipient certificate not set" "EseguiFirma"

                Procedure_Return

            End

        End

    End

End

                

If (Length(Trim(Value(oForm_Pin))) > 0) Begin

    // Exec signature sending the PIN

    Get Value of oForm_Pin to sPin

                    

    Move (dmtSigningFile( ;

        ghDmt , ;

        sFileDaFirmare , ;

        szOID_OIWSEC_sha1 , ;

        szOID_RSA_RC4 , ;

        (Checked_State(oCheckBox_Detached)) , ;

        (Checked_State(oCheckBox_SignEncrypt)) , ;

        False , ;

        (Checked_State(oCheckBox_SignEncryptAllRecipient)) , ;

        sPin , ;

        False)) to ret

End

Else Begin

    // Exec signature without sending the PIN - CSP will ask me for the correct smart card PIN

    Move (dmtSigningFile( ;

        ghDmt , ;

        sFileDaFirmare , ;

        szOID_OIWSEC_sha1 , ;

        szOID_RSA_RC4 , ;

        (Checked_State(oCheckBox_Detached)) , ;

        (Checked_State(oCheckBox_SignEncrypt)) , ;

        False , ;

        (Checked_State(oCheckBox_SignEncryptAllRecipient)) , ;

        "" , ;

        False)) to ret

End

Visual Basic

Dim iDefaultPersonalCertificateCount As Long

Dim iDefaultPersonalCertificate As Long

Dim iDefaultRecipientCertificateCount As Long

Dim iDefaultRecipientCertificate As Long

    

Dim res As Long

    

' Check

If (Len(Trim(txt_FileToSign.Text)) = 0) Then

    MsgBox "Any file selected", vbCritical, "ExecSign"

    Exit Sub

End If

    

iDefaultPersonalCertificateCount = dmtGetCertificateCountLoaded(g_hDmt, False)

If (iDefaultPersonalCertificateCount <= 0) Then

    MsgBox "Personal certificate not loaded", vbCritical, "ExecSign"

    Exit Sub

Else

    iDefaultPersonalCertificate = dmtGetDefaultCertificateLoaded(g_hDmt, False)

    If (iDefaultPersonalCertificate < 0) Then

        MsgBox "Default personal certificate not set", vbCritical, "ExecSign"

        Exit Sub

    End If

End If

    

If (chk_Encrypt.Value = 1) Then

    ' If I want to encrypt the content of the signed file

    ' I need to use a recipient certificate

    iDefaultRecipientCertificateCount = dmtGetCertificateCountLoaded(g_hDmt, True)

    If (iDefaultRecipientCertificateCount <= 0) Then

        MsgBox "Recipient certificate not loaded", vbCritical, "ExecSign"

        Exit Sub

    Else

        If (chk_UseAllRecipient.Value = 0) Then

            ' If I don't want to use all recipient certificate

            ' I need to set the default recipient certificate

            iDefaultRecipientCertificate = dmtGetDefaultCertificateLoaded(g_hDmt, True)

            If (iDefaultRecipientCertificate < 0) Then

                MsgBox "Default recipent certificate not set", vbCritical, "ExecSign"

                Exit Sub

            End If

        End If

    End If

End If

    

If Len(Trim(txt_PinForSign.Text)) > 0 Then

    ' Exec signature sending the PIN

    res = dmtSigningFile(g_hDmt, _

        txt_FileToSign.Text, _

        szOID_OIWSEC_sha1, _

        szOID_RSA_RC4, _

        False, _

        chk_Encrypt.Value, _

        False, _

        chk_UseAllRecipient.Value, _

        Trim(txt_PinForSign.Text))

Else

    ' Exec signature without sending the PIN - CSP will ask me for the correct smart card PIN

    res = dmtSigningFile(g_hDmt, _

        txt_FileToSign.Text, _

        szOID_OIWSEC_sha1, _

        szOID_RSA_RC4, _

        False, _

        chk_Encrypt.Value, _

        False, _

        chk_UseAllRecipient.Value)

End If

C Sharp

if (LabelFileToSign.Text.Length == 0)

{

    MessageBox.Show(this, "Any file selected to sign", "SigningFile",

        MessageBoxButtons.OK, MessageBoxIcon.Error);

    return;

}

if (CDIMATECLIBRARY.dmtGetCertificateCountLoaded(CDIMATECLIBRARY.ghDimatecObj, false) <= 0)

{

    MessageBox.Show(this, "Personal certificate not loaded", "SigningFile",

        MessageBoxButtons.OK, MessageBoxIcon.Error);

    return;

}

if (CDIMATECLIBRARY.dmtGetDefaultCertificateLoaded(CDIMATECLIBRARY.ghDimatecObj, false) < 0)

{

    MessageBox.Show(this, "Default personal certificate not set", "SigningFile",

        MessageBoxButtons.OK, MessageBoxIcon.Error);

    return;

}

if (CheckBoxSignEncrypt.Checked)

{

    // If I want to encrypt the content of the signed file

    // I need to use a recipient certificate

    if (CDIMATECLIBRARY.dmtGetCertificateCountLoaded(CDIMATECLIBRARY.ghDimatecObj, true) <= 0)

    {

        MessageBox.Show(this, "Recipient certificate not loaded", "SigningFile",

            MessageBoxButtons.OK, MessageBoxIcon.Error);

        return;

    }

    if (CDIMATECLIBRARY.dmtGetDefaultCertificateLoaded(CDIMATECLIBRARY.ghDimatecObj, true) < 0)

    {

        MessageBox.Show(this, "Default recipient certificate not set", "SigningFile",

            MessageBoxButtons.OK, MessageBoxIcon.Error);

        return;

    }

}

 

string RefTime = "";

string RefTimeFormat = "";

if (CheckBoxAddSignRefTime.Checked)

{

    RefTime = UserRefTimeMaskedTextBox.Text;

    RefTimeFormat = "%d/%m/%Y %H.%M";

}

 

int SigningFileResult = CDIMATECLIBRARY.DMT_OK;

if (SignaturePinTextBox.Text.Length > 0)

{

    SigningFileResult = CDIMATECLIBRARY.dmtSigningFile(

        CDIMATECLIBRARY.ghDimatecObj,

        LabelFileToSign.Text,

        CDIMATECLIBRARY.szOID_OIWSEC_sha1,

        CDIMATECLIBRARY.szOID_RSA_RC4,

        false,

        CheckBoxSignEncrypt.Checked,

        false,

        CheckBoxSignEncryptAllRecipient.Checked,

        SignaturePinTextBox.Text,

        CheckBoxAddDsiSignRefTime.Checked,

        RefTime,

        RefTimeFormat);

}

else

{

    SigningFileResult = CDIMATECLIBRARY.dmtSigningFile(

        CDIMATECLIBRARY.ghDimatecObj,

        LabelFileToSign.Text,

        CDIMATECLIBRARY.szOID_OIWSEC_sha1,

        CDIMATECLIBRARY.szOID_RSA_RC4,

        false,

        CheckBoxSignEncrypt.Checked,

        false,

        CheckBoxSignEncryptAllRecipient.Checked,

        "",

        CheckBoxAddDsiSignRefTime.Checked,

        RefTime,

        RefTimeFormat);

}