David PHAM-VAN

Add document signature support

@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 * Add Document ID 3 * Add Document ID
4 * Add encryption support 4 * Add encryption support
5 * Increase PDF version to 1.7 5 * Increase PDF version to 1.7
  6 +* Add document signature support
6 7
7 ## 1.3.13 8 ## 1.3.13
8 9
@@ -54,6 +54,7 @@ part 'src/page.dart'; @@ -54,6 +54,7 @@ part 'src/page.dart';
54 part 'src/point.dart'; 54 part 'src/point.dart';
55 part 'src/polygon.dart'; 55 part 'src/polygon.dart';
56 part 'src/rect.dart'; 56 part 'src/rect.dart';
  57 +part 'src/signature.dart';
57 part 'src/stream.dart'; 58 part 'src/stream.dart';
58 part 'src/ttf_parser.dart'; 59 part 'src/ttf_parser.dart';
59 part 'src/ttf_writer.dart'; 60 part 'src/ttf_writer.dart';
@@ -59,5 +59,10 @@ class PdfCatalog extends PdfObject { @@ -59,5 +59,10 @@ class PdfCatalog extends PdfObject {
59 // the /PageMode setting 59 // the /PageMode setting
60 params['/PageMode'] = 60 params['/PageMode'] =
61 PdfStream.string(PdfDocument._PdfPageModes[pageMode.index]); 61 PdfStream.string(PdfDocument._PdfPageModes[pageMode.index]);
  62 +
  63 + if (pdfDocument.sign != null) {
  64 + params['/Perms'] = PdfStream.dictionary(
  65 + <String, PdfStream>{'/DocMDP': pdfDocument.sign.ref()});
  66 + }
62 } 67 }
63 } 68 }
@@ -89,6 +89,9 @@ class PdfDocument { @@ -89,6 +89,9 @@ class PdfDocument {
89 /// Object used to encrypt the document 89 /// Object used to encrypt the document
90 PdfEncryption encryption; 90 PdfEncryption encryption;
91 91
  92 + /// Object used to sign the document
  93 + PdfSignature sign;
  94 +
92 /// The PDF specification version 95 /// The PDF specification version
93 final String version = '1.7'; 96 final String version = '1.7';
94 97
@@ -40,6 +40,9 @@ class PdfOutput { @@ -40,6 +40,9 @@ class PdfOutput {
40 /// This is used to track the /Encrypt object (encryption) 40 /// This is used to track the /Encrypt object (encryption)
41 PdfEncryption encryptID; 41 PdfEncryption encryptID;
42 42
  43 + /// This is used to track the /Sign object (signature)
  44 + PdfSignature signatureID;
  45 +
43 /// This method writes a [PdfObject] to the stream. 46 /// This method writes a [PdfObject] to the stream.
44 /// 47 ///
45 /// @param ob [PdfObject] Object to write 48 /// @param ob [PdfObject] Object to write
@@ -55,6 +58,10 @@ class PdfOutput { @@ -55,6 +58,10 @@ class PdfOutput {
55 if (ob is PdfEncryption) { 58 if (ob is PdfEncryption) {
56 encryptID = ob; 59 encryptID = ob;
57 } 60 }
  61 + if (ob is PdfSignature) {
  62 + assert(signatureID == null, 'Only one document signature is allowed');
  63 + signatureID = ob;
  64 + }
58 65
59 offsets.add(PdfXref(ob.objser, os.offset)); 66 offsets.add(PdfXref(ob.objser, os.offset));
60 ob._write(os); 67 ob._write(os);
@@ -131,6 +138,10 @@ class PdfOutput { @@ -131,6 +138,10 @@ class PdfOutput {
131 // end the trailer object 138 // end the trailer object
132 os.putDictionary(params); 139 os.putDictionary(params);
133 os.putString('\nstartxref\n$xref\n%%EOF\n'); 140 os.putString('\nstartxref\n$xref\n%%EOF\n');
  141 +
  142 + if (signatureID != null) {
  143 + signatureID._writeSignature(os);
  144 + }
134 } 145 }
135 146
136 /// Writes a block of references to the Pdf file 147 /// Writes a block of references to the Pdf file
  1 +/*
  2 + * Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the 'License');
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an 'AS IS' BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +part of pdf;
  18 +
  19 +@immutable
  20 +class PdfSignatureRange {
  21 + const PdfSignatureRange(this.start, this.end);
  22 +
  23 + final int start;
  24 + final int end;
  25 +}
  26 +
  27 +abstract class PdfSignature extends PdfObject {
  28 + PdfSignature(PdfDocument pdfDocument) : super(pdfDocument, '/Sig');
  29 +
  30 + int _offsetStart;
  31 + int _offsetEnd;
  32 +
  33 + void preSign();
  34 +
  35 + void sign(PdfStream os, List<PdfSignatureRange> ranges);
  36 +
  37 + @override
  38 + void _write(PdfStream os) {
  39 + preSign();
  40 +
  41 + _offsetStart = os.offset;
  42 + super._write(os);
  43 + _offsetEnd = os.offset;
  44 + }
  45 +
  46 + void _writeSignature(PdfStream os) {
  47 + assert(_offsetStart != null && _offsetEnd != null,
  48 + 'Must reserve the object space before signing the document');
  49 +
  50 + final List<PdfSignatureRange> ranges = <PdfSignatureRange>[
  51 + PdfSignatureRange(0, _offsetStart),
  52 + PdfSignatureRange(_offsetEnd, os.offset),
  53 + ];
  54 +
  55 + sign(os, ranges);
  56 + final PdfStream signature = PdfStream();
  57 + super._write(signature);
  58 +
  59 + assert(signature.offset == _offsetEnd - _offsetStart);
  60 + os.output().replaceRange(_offsetStart, _offsetEnd, signature.output());
  61 + }
  62 +}