-
Notifications
You must be signed in to change notification settings - Fork 1
/
DigestCalcBlankSigner.cs
57 lines (48 loc) · 1.95 KB
/
DigestCalcBlankSigner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using iText.Kernel.Pdf;
using iText.Signatures;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PdfSignSamplePkcs1
{
/// <summary>
/// This is a dummy implementation of the IExternalSignatureContainer
/// It is only used to prepare the pdf (reserve space) and to create the hash(Digest) of the pdf
/// </summary>
internal class DigestCalcBlankSigner : IExternalSignatureContainer
{
private readonly PdfName _filter;
private readonly PdfName _subFilter;
public byte[] PdfDigest { get; set; }
internal DigestCalcBlankSigner(PdfName filter, PdfName subFilter)
{
_filter = filter;
_subFilter = subFilter;
}
/// <summary>
/// This Sign method is called internally during iText SignExternalContainer() method
/// Usually, the signature would be returned here. But since we only need the hash we just calculate the current hash
/// and set it as a variable. It must be read after the SignExternalContainer() call
/// We then return a 0 byte signature which is applied as a placeholder
/// </summary>
/// <param name="data">document data</param>
/// <returns></returns>
public virtual byte[] Sign(Stream data)
{
PdfDigest = DigestAlgorithms.Digest(data, DigestAlgorithms.GetMessageDigest("SHA256"));
return new byte[0];
}
/// <summary>
/// We need to set PdfName.Adobe_PPKLite, PdfName.Adbe_pkcs7_detached which are supplied in the constructor
/// </summary>
/// <param name="signDic">The PdfDictionary to chich the filters must be applied</param>
public virtual void ModifySigningDictionary(PdfDictionary signDic)
{
signDic.Put(PdfName.Filter, _filter);
signDic.Put(PdfName.SubFilter, _subFilter);
}
}
}