
Imports System.IO
Imports TSpdf.Kernel.Font

Imports System.Security.Cryptography.X509Certificates

Imports System.Security.Cryptography
Imports TSpdf.Kernel.Pdf
Imports TSpdf.Kernel.Utils
Imports TSpdf.Forms
Imports TSpdf.Signatures
Imports System.Text
Imports TSpdf.Kernel.Pdf.Canvas
Imports TSpdf.Kernel.Geom
Imports TSpdf.Layout.Element
Imports System.Text.RegularExpressions
Imports Org.BouncyCastle.Pkcs
Imports TSpdf.Commons.Bouncycastle.Cert
Imports TSpdf.Commons.Bouncycastle.Crypto
Imports TSpdf.Bouncycastle.Crypto
Imports TSpdf.Bouncycastle.Cert
Imports TSpdf
Imports Org.BouncyCastle.Crypto.Agreement
Imports TSpdf.Layout
Imports TSpdf.IO.Font.Constants
Imports TSpdf.Layout.Properties
Imports System.Drawing
Imports System.Drawing.Imaging
Imports Rectangle = System.Drawing.Rectangle
Imports TSpdf.Kernel.Pdf.Colorspace.PdfDeviceCs
Imports TSpdf.Layout.Renderer
Imports System.Drawing.Text
Imports System.Runtime.InteropServices
Imports TSpdf.Forms.Fields
Imports System.Runtime.CompilerServices
Imports TSpdf.Signatures.PdfSigner
Imports tsPDFUtilsCore

Public Class ExternalTimestampContainer
    Implements IExternalSignatureContainer

    Private ReadOnly tsaClient As ITSAClient

    Public Sub New(ByVal tsaClient As ITSAClient)
        Me.tsaClient = tsaClient
    End Sub

    Public Function Sign(ByVal data As Stream) As Byte() Implements IExternalSignatureContainer.Sign
        ' Calcula el hash del contenido usando SHA256
        Dim hash As Byte()
        Using hasher As HashAlgorithm = SHA256.Create()
            hash = hasher.ComputeHash(data)
        End Using

        ' Solicita la marca de tiempo a la TSA
        Return tsaClient.GetTimeStampToken(hash)
    End Function

    Public Sub ModifySigningDictionary(ByVal signDic As PdfDictionary) Implements IExternalSignatureContainer.ModifySigningDictionary
        signDic.Put(PdfName.Filter, PdfName.Adobe_PPKLite)
        signDic.Put(PdfName.SubFilter, PdfName.ETSI_RFC3161)
    End Sub

End Class
