Sign document with secure custom Metadata signatures
GroupDocs.Signature provides additional features with MetadataSignature class like following
- ability to embedded custom objects into metadata
- ability to specify custom objects encryption and serialization
- ability to collect and populate standard document signatures
Here are the steps to embed custom object into Metadata signature with GroupDocs.Signature:
- Implement if needed custom data serialization class that implement IDataSerializer interface. By default GroupDocs.Signature uses embedded json format serialization but allows user to customize it.
- Implement if needed custom data encryption class that implements IDataEncryption interface. By default GroupDocs.Signature has several encryption implementation you can use but allows user to customize it.
- Implement class with properties and specify if needed class attributes (like custom serialization attribute, custom encryption attribute), specify attributes for properties like FormatAttribute to specify serialization name and display format, same as SkipSerializationAttribute to mark property of class as not serialize
- Create new instance of Signature class and pass source document path as a constructor parameter.
- Create one or several objects of proper MetadataSignature object for document (like PdfMetadataSignature, ImageMetadataSignature, PresentationMetadataSignature, SpreadsheetMetadataSignature, WordProcessingMetadataSignature) and setup
- Instantiate the MetadataSignOptions object according to your requirements and pass all metadata signatures to it.
- Call Sign method of Signature class instance and pass MetadataSignOptions to it.
Following topics show more details of these features
This example shows how to specify custom serialization class. This class should be implemented as Attribute and IDataSerializer interface.
/// <summary>
/// Creates class that implements IDataSerializer interface
/// It cam support common serialization like JSon or custom data format
/// </summary>
class CustomSerializationAttribute : Attribute, IDataSerializer
{
public T Deserialize<T>(string source) where T : class
{
DocumentSignatureData result = new DocumentSignatureData();
byte[] bytes = Encoding.UTF8.GetBytes(source);
using (MemoryStream stream = new MemoryStream(bytes))
{
stream.Seek(0, SeekOrigin.Begin);
using (BinaryReader reader = new BinaryReader(stream))
{
result.ID = reader.ReadString();
result.Author = reader.ReadString();
result.Signed = new DateTime( reader.ReadInt64());
result.DataFactor = reader.ReadDecimal();
}
}
var converter = TypeDescriptor.GetConverter(typeof(T));
return (T)converter.ConvertFrom(result);
}
public string Serialize(object data)
{
string result = string.Empty;
DocumentSignatureData signatureData = data as DocumentSignatureData;
if (signatureData != null)
{
using (MemoryStream stream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(signatureData.ID);
writer.Write(signatureData.Author);
writer.Write(signatureData.Signed.Ticks);
writer.Write(signatureData.DataFactor);
}
result = Encoding.UTF8.GetString(stream.ToArray());
}
}
return result;
}
}
This example shows how to specify custom serialization class. This class could be implemented also as Attribute (optional) to specify as class attribute.
// Define class that implements IDataEncryption interface
private class CustomXOREncryptionAttribute : Attribute, IDataEncryption
{
/// <summary>
/// Gets or sets non empty key for encryption (at least one character)
/// </summary>
public int Key { get; set; }
/// <summary>
/// Encode method to encrypt string.
/// </summary>
/// <param name="source">Source string to encode.</param>
/// <returns>Returns enccrypted string</returns>
public string Encode(string source)
{
return Process(source);
}
/// <summary>
/// Decode method to obtain decrypted string.
/// </summary>
/// <param name="source">Source string to decode.</param>
/// <returns>Returns decrypted string</returns>
public string Decode(string source)
{
return Process(source);
}
/// <summary>
/// Using XOR operation get encoded / decoded string
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
private string Process(string source)
{
StringBuilder src = new StringBuilder(source);
StringBuilder dst = new StringBuilder(src.Length);
char chTmp;
for (int index = 0; index < src.Length; ++index)
{
chTmp = src[index];
chTmp = (char)(chTmp ^ this.Key);
dst.Append(chTmp);
}
return dst.ToString();
}
}
This example shows how to define custom class with serialization and encryption properties and setup Format attributes for properties.
// setup CustomSerialization Attribute to setup customer serialization(see example above)
[CustomSerialization]
// setup CustomXOREncryption Attribute for custom encryption (see example above)
[CustomXOREncryption]
private class DocumentSignatureData
{
[Format("SignID")]
public string ID { get; set; }
[Format("SAuth")]
public string Author { get; set; }
[Format("SDate", "yyyy-MM-dd")]
public DateTime Signed { get; set; }
[Format("SDFact", "N2")]
public decimal DataFactor { get; set; }
[SkipSerialization]
public string Comments { get; set; }
}
This example shows how to add custom object into metadata signature to Image document.
using (Signature signature = new Signature("sample.jpg"))
{
// setup key and passphrase
string key = "1234567890";
string salt = "1234567890";
// create data encryption
IDataEncryption encryption = new SymmetricEncryption(SymmetricAlgorithmType.Rijndael, key, salt);
// setup options with text of signature
MetadataSignOptions options = new MetadataSignOptions();
// create custom object
DocumentSignatureData documentSignature = new DocumentSignatureData()
{
ID = Guid.NewGuid().ToString(),
Author = Environment.UserName,
Signed = DateTime.Now,
DataFactor = 11.22M
};
// Specify different Metadata Signatures and add them to options signature collection
ushort imgsMetadataId = 41996;
// Specify different Metadata Signatures and add them to options signature collection
// setup Author property
ImageMetadataSignature mdDocument = new ImageMetadataSignature(imgsMetadataId++, documentSignature);
// set encryption
mdDocument.DataEncryption = encryption;
// setup Author property
ImageMetadataSignature mdAuthor = new ImageMetadataSignature(imgsMetadataId++, "Mr.Scherlock Holmes");
// set encryption
mdAuthor.DataEncryption = encryption;
// setup data of document id
ImageMetadataSignature mdDocId = new ImageMetadataSignature(imgsMetadataId++, Guid.NewGuid().ToString());
// set encryption
mdDocId.DataEncryption = encryption;
// add signatures to options
options.Signatures.Add(mdDocument);
options.Signatures.Add(mdAuthor);
options.Signatures.Add(mdDocId);
// sign document to file
signature.Sign("signed.jpg", options);
}
This example shows how to add or update standard embedded PDF document metadata signatures.
using (Signature signature = new Signature("sample.pdf"))
{
// setup options with text of signature
MetadataSignOptions options = new MetadataSignOptions();
// create custom object
DocumentSignatureData documentSignature = new DocumentSignatureData()
{
ID = Guid.NewGuid().ToString(),
Author = Environment.UserName,
Signed = DateTime.Now,
DataFactor = 11.22M
};
IDataEncryption encryption = new CustomXOREncryptionAttribute();
// Specify different Metadata Signatures and add them to options signature collection
// for this metadata encryption will be used from class attribute
PdfMetadataSignature mdDocument = new PdfMetadataSignature("DocumentSignature", documentSignature);
// setup Author property
PdfMetadataSignature mdAuthor = new PdfMetadataSignature("Author", "Mr.Scherlock Holmes");
// set encryption
mdAuthor.DataEncryption = encryption;
// setup data of document id
PdfMetadataSignature mdDocId = new PdfMetadataSignature("DocumentId", Guid.NewGuid().ToString());
// set encryption
mdDocId.DataEncryption = encryption;
// add signatures to options
options.Signatures.Add(mdDocument);
options.Signatures.Add(mdAuthor);
options.Signatures.Add(mdDocId);
// sign document to file
signature.Sign("sample_signed.pdf", options);
}
NoteExamples above also work for different document types. For Presentations documents only objects of PresentationMetadataSignature should be used with same properties and behavior, for Spreadsheet documents only objects of SpreadsheetMetadataSignature should be used with same properties and behavior, with WordProcessing documents the class WordProcessingMetadataSignature should be used
You may easily run the code above and see the feature in action in our GitHub examples:
- GroupDocs.Signature for .NET examples, plugins, and showcase
- GroupDocs.Signature for Java examples, plugins, and showcase
- Document Signature for .NET MVC UI Example
- Document Signature for .NET App WebForms UI Example
- Document Signature for Java App Dropwizard UI Example
- Document Signature for Java Spring UI Example
Along with the full-featured .NET library, we provide simple but powerful free online apps.
To sign PDF, Word, Excel, PowerPoint, and other documents you can use the online apps from the GroupDocs.Signature App Product Family.