-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
230adc3
commit 90cbb85
Showing
6 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
... Signature/TimestampingPDFwithExternalSigning/.NET/TimestampingPDFwithExternalSigning.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.10.35122.118 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TimestampingPDFwithExternalSigning", "TimestampingPDFwithExternalSigning\TimestampingPDFwithExternalSigning.csproj", "{076C52CD-6650-441A-848E-DF80295CDFD5}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{076C52CD-6650-441A-848E-DF80295CDFD5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{076C52CD-6650-441A-848E-DF80295CDFD5}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{076C52CD-6650-441A-848E-DF80295CDFD5}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{076C52CD-6650-441A-848E-DF80295CDFD5}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {EEFF8B02-E955-4312-AB2F-1774A09D5BD9} | ||
EndGlobalSection | ||
EndGlobal |
Binary file added
BIN
+4.84 KB
...mestampingPDFwithExternalSigning/.NET/TimestampingPDFwithExternalSigning/Data/Barcode.pdf
Binary file not shown.
Binary file added
BIN
+2.52 KB
...e/TimestampingPDFwithExternalSigning/.NET/TimestampingPDFwithExternalSigning/Data/PDF.pfx
Binary file not shown.
Empty file.
103 changes: 103 additions & 0 deletions
103
...ure/TimestampingPDFwithExternalSigning/.NET/TimestampingPDFwithExternalSigning/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using Syncfusion.Pdf.Parsing; | ||
using Syncfusion.Pdf.Security; | ||
using Syncfusion.Drawing; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Security.Cryptography; | ||
using Org.BouncyCastle.Tsp; | ||
using System.Net; | ||
using System.Text; | ||
using Org.BouncyCastle.Math; | ||
|
||
namespace Externally_sign_the_PDF_document | ||
{ | ||
internal class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
//Get the stream from the document | ||
FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/Barcode.pdf"), FileMode.Open, FileAccess.Read); | ||
//Load the existing PDF document | ||
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream); | ||
|
||
//Creates a digital signature. | ||
PdfSignature signature = new PdfSignature(loadedDocument, loadedDocument.Pages[0], null, "Signature"); | ||
//Sets the signature information. | ||
signature.Bounds = new RectangleF(new PointF(0, 0), new SizeF(100, 30)); | ||
signature.Settings.CryptographicStandard = CryptographicStandard.CADES; | ||
signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA1; | ||
|
||
//Create an external signer. | ||
IPdfExternalSigner externalSignature = new ExternalSigner("SHA1"); | ||
|
||
//Add public certificates. | ||
List<X509Certificate2> certificates = new List<X509Certificate2>(); | ||
certificates.Add(new X509Certificate2(new X509Certificate2(Path.GetFullPath(@"Data/PDF.pfx"), "password123"))); | ||
signature.AddExternalSigner(externalSignature, certificates, null); | ||
|
||
//Create file stream. | ||
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) | ||
{ | ||
//Save the PDF document to file stream. | ||
loadedDocument.Save(outputFileStream); | ||
} | ||
//Close the document. | ||
loadedDocument.Close(true); | ||
} | ||
//Create the external signer class and sign the document hash. | ||
class ExternalSigner : IPdfExternalSigner | ||
{ | ||
private string _hashAlgorithm; | ||
public string HashAlgorithm | ||
{ | ||
get { return _hashAlgorithm; } | ||
} | ||
public ExternalSigner(string hashAlgorithm) | ||
{ | ||
_hashAlgorithm = hashAlgorithm; | ||
} | ||
public byte[] Sign(byte[] message, out byte[] timeStampResponse) | ||
{ | ||
byte[] signedBytes = null; | ||
X509Certificate2 digitalID = new X509Certificate2(new X509Certificate2(Path.GetFullPath(@"Data/PDF.pfx"), "password123")); | ||
if (digitalID.PrivateKey is System.Security.Cryptography.RSACryptoServiceProvider) | ||
{ | ||
System.Security.Cryptography.RSACryptoServiceProvider rsa = (System.Security.Cryptography.RSACryptoServiceProvider)digitalID.PrivateKey; | ||
signedBytes = rsa.SignData(message, HashAlgorithm); | ||
} | ||
else if (digitalID.PrivateKey is RSACng) | ||
{ | ||
RSACng rsa = (RSACng)digitalID.PrivateKey; | ||
signedBytes = rsa.SignData(message, System.Security.Cryptography.HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1); | ||
} | ||
timeStampResponse = GetRFC3161TimeStampToken(signedBytes); | ||
|
||
return signedBytes; | ||
} | ||
public byte[] GetRFC3161TimeStampToken(byte[] bytes) | ||
{ | ||
SHA1 sha1 = SHA1CryptoServiceProvider.Create(); | ||
byte[] hash = sha1.ComputeHash(bytes); | ||
TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator(); | ||
reqGen.SetCertReq(true); | ||
TimeStampRequest tsReq = reqGen.Generate(TspAlgorithms.Sha1, hash, BigInteger.ValueOf(100)); | ||
byte[] tsData = tsReq.GetEncoded(); | ||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://rfc3161.ai.moda"); // Update your timestamp Uri here | ||
req.Method = "POST"; | ||
req.ContentType = "application/timestamp-query"; | ||
req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("9024:yourPass"))); | ||
req.ContentLength = tsData.Length; | ||
Stream reqStream = req.GetRequestStream(); | ||
reqStream.Write(tsData, 0, tsData.Length); | ||
reqStream.Close(); | ||
HttpWebResponse res = (HttpWebResponse)req.GetResponse(); | ||
if (res != null) | ||
{ | ||
Stream resStream = new BufferedStream(res.GetResponseStream()); | ||
TimeStampResponse tsRes = new TimeStampResponse(resStream); | ||
return tsRes.TimeStampToken.GetEncoded(); | ||
} | ||
return null; | ||
} | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...Signing/.NET/TimestampingPDFwithExternalSigning/TimestampingPDFwithExternalSigning.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BouncyCastle" Version="*" /> | ||
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="*" /> | ||
</ItemGroup> | ||
|
||
</Project> |