Skip to content

Instantly share code, notes, and snippets.

View SumindaD's full-sized avatar

Suminda Niroshan SumindaD

  • 1BillionTech
  • Colombo, Sri Lanka
View GitHub Profile
/// <summary>
/// Verify the integrity of the image by checking the signature using the certificate public key
/// </summary>
/// <param name="xmlFilePath">Path to the signed xml image document</param>
/// <param name="certificateBuffer">The public key certificate byte[] to verify the integrity of the signed xml image document</param>
/// <param name="certPassword">Private key certificate Password</param>
/// <returns>True - If image integrity is intact. False - If image has been tampered with</returns>
public static bool VerifyImage(string xmlFilePath, byte[] certificateBuffer, string certPassword)
{
var certificate = new X509Certificate2(certificateBuffer, certPassword);
/// <summary>
/// Verify the integrity of the signed XML document
/// </summary>
/// <param name="xmlFilePath">Path to the signed xml document</param>
/// <param name="certificate">X509Certificate2 Public key certificate</param>
/// <returns></returns>
private static bool VerifyXMLDocument(string xmlFilePath, X509Certificate2 certificate)
{
var xmlDocument = ReadXMLDocumentFromPath(xmlFilePath);
/// <summary>
/// Sign the image data with the given certificate and write to the xmlFilePath
/// </summary>
/// <param name="imageBuffer">Image data to sign</param>
/// <param name="certificateBuffer">Private key certificate byte[]</param>
/// <param name="certPassword">Private key certificate Password</param>
/// <param name="xmlFilePath">The path to save the signed XML</param>
public static void SignImage(byte[] imageBuffer, byte[] certificateBuffer,string certPassword, string xmlFilePath)
{
var certificate = new X509Certificate2(certificateBuffer, certPassword);
/// <summary>
/// Sign the xml image document using private key certificate
/// </summary>
/// <param name="xmlDocumentBuffer">Byte[] of the xml image document</param>
/// <param name="certificate">X509Certificate2 private key certificate to sign the xml document</param>
/// <param name="signedXMLPath">Path to save the signed xml document</param>
private static void SignXMLDocument(byte[] xmlDocumentBuffer, X509Certificate2 certificate, string signedXMLPath)
{
// Load xmlDocument data in to an XML Document
XmlDocument xmlDocument = new XmlDocument();
/// <summary>
/// Serialize an image byte[] to an XML byte[]
/// </summary>
/// <param name="imageBuffer">Byte[] of the image to be serialized into XML</param>
/// <returns>Byte[] of the XML</returns>
private static byte[] SerializeImageToXML(byte[] imageBuffer)
{
XmlSerializer x = new XmlSerializer(typeof(byte[]));
using (MemoryStream myFileStream = new MemoryStream())
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using GemBox.Pdf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SignPDF
{
class Program
Parameters:
Stage:
Type: String
ContainerPort:
Type: Number
ImageURI:
Type: String
Parameters:
Stage:
Type: String
Default: dev
GithubUserName:
Type: String
Default: <USERNAME>
GithubRepo:
Type: String
Default: Fargate-ECSCluster-Cloudformation
TaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Memory: 1024
Cpu: 512
NetworkMode: awsvpc
RequiresCompatibilities:
- 'FARGATE'
TaskRoleArn: !Ref ExecutionRole
ExecutionRoleArn: !Ref ExecutionRole