コード例 #1
0
    private static int RunSignAndReturnExitCode(SignOptions opts)
    {
        var certificates = WindowsCertificateStore.LoadPersonalCurrentUser().GetCertificatesWithKey().Where(c => c.Certificate.PkiBrazil.CPF != null).ToList();
        var certificate  = certificates[opts.Certificate];
        var fileName     = opts.InputFile;

        using var stream = File.OpenRead(fileName);
        var digestAlgorithm = DigestAlgorithm.SHA256;
        var digest          = digestAlgorithm.ComputeHash(stream);
        var signer          = new CadesSigner();

        signer.SetSigningCertificate(certificate);
        signer.SetPolicy(CadesPoliciesForGeneration.GetPkiBrazilAdrBasica());
        signer.SetEncapsulatedContent(false);
        signer.SetDataDigestToSign(digestAlgorithm, digest);
        signer.ComputeSignature();

        var cades = signer.GetSignature();

        File.WriteAllBytes(opts.SignedFile, cades);
        stream.Close();
        return(0);
    }