Changed file to writer/reader
This commit is contained in:
2
sign.go
2
sign.go
@@ -130,7 +130,7 @@ func main() {
|
|||||||
Certificate: cert,
|
Certificate: cert,
|
||||||
CertificateChains: certificate_chains,
|
CertificateChains: certificate_chains,
|
||||||
TSA: sign.TSA{
|
TSA: sign.TSA{
|
||||||
URL: "http://aatl-timestamp.globalsign.com/tsa/aohfewat2389535fnasgnlg5m23",
|
URL: "http://aatl-timestamp.globalsign.com/tsa/aohfewat2389535fnasgnlg5m23",
|
||||||
},
|
},
|
||||||
RevocationData: revocation.InfoArchival{},
|
RevocationData: revocation.InfoArchival{},
|
||||||
RevocationFunction: sign.DefaultEmbedRevocationStatusFunction,
|
RevocationFunction: sign.DefaultEmbedRevocationStatusFunction,
|
||||||
|
@@ -6,11 +6,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (context *SignContext) updateByteRange() error {
|
func (context *SignContext) updateByteRange() error {
|
||||||
// Get current filesize. Easier than what should be the current size.
|
context.OutputBuffer.Seek(0, 0)
|
||||||
// @todo: find out of this is safe.
|
output_file_size := int64(context.OutputBuffer.Buff.Len())
|
||||||
output_file_stat, _ := context.OutputFile.Stat()
|
|
||||||
|
|
||||||
output_file_size := output_file_stat.Size()
|
|
||||||
|
|
||||||
// Calculate ByteRange values to replace them.
|
// Calculate ByteRange values to replace them.
|
||||||
context.ByteRangeValues = make([]int64, 4)
|
context.ByteRangeValues = make([]int64, 4)
|
||||||
@@ -32,13 +29,17 @@ func (context *SignContext) updateByteRange() error {
|
|||||||
// Make sure our ByteRange string didn't shrink in length.
|
// Make sure our ByteRange string didn't shrink in length.
|
||||||
new_byte_range += strings.Repeat(" ", len(signatureByteRangePlaceholder)-len(new_byte_range))
|
new_byte_range += strings.Repeat(" ", len(signatureByteRangePlaceholder)-len(new_byte_range))
|
||||||
|
|
||||||
// Seek to ByteRange position in file.
|
context.OutputBuffer.Seek(0, 0)
|
||||||
context.OutputFile.Seek(context.ByteRangeStartByte, 0)
|
file_content := context.OutputBuffer.Buff.Bytes()
|
||||||
|
|
||||||
|
context.OutputBuffer.Write(file_content[:context.ByteRangeStartByte])
|
||||||
|
|
||||||
// Write new ByteRange.
|
// Write new ByteRange.
|
||||||
if _, err := context.OutputFile.Write([]byte(new_byte_range)); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte(new_byte_range)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
context.OutputBuffer.Write(file_content[context.ByteRangeStartByte+int64(len(new_byte_range)):])
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,6 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -121,12 +120,10 @@ func (context *SignContext) fetchRevocationData() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (context *SignContext) createSignature() ([]byte, error) {
|
func (context *SignContext) createSignature() ([]byte, error) {
|
||||||
|
context.OutputBuffer.Seek(0, 0)
|
||||||
|
|
||||||
// Sadly we can't efficiently sign a file, we need to read all the bytes we want to sign.
|
// Sadly we can't efficiently sign a file, we need to read all the bytes we want to sign.
|
||||||
context.OutputFile.Seek(0, 0)
|
file_content := context.OutputBuffer.Buff.Bytes()
|
||||||
sign_buf := bytes.NewBuffer(nil)
|
|
||||||
io.Copy(sign_buf, context.OutputFile)
|
|
||||||
file_content := sign_buf.Bytes()
|
|
||||||
|
|
||||||
// Collect the parts to sign.
|
// Collect the parts to sign.
|
||||||
sign_content := make([]byte, 0)
|
sign_content := make([]byte, 0)
|
||||||
@@ -266,7 +263,17 @@ func (context *SignContext) replaceSignature() error {
|
|||||||
return errors.New("Signature is too big to fit in reserved space.")
|
return errors.New("Signature is too big to fit in reserved space.")
|
||||||
}
|
}
|
||||||
|
|
||||||
context.OutputFile.WriteAt(dst, context.ByteRangeValues[0]+context.ByteRangeValues[1]+1)
|
context.OutputBuffer.Seek(0, 0)
|
||||||
|
file_content := context.OutputBuffer.Buff.Bytes()
|
||||||
|
|
||||||
|
context.OutputBuffer.Write(file_content[:(context.ByteRangeValues[0] + context.ByteRangeValues[1] + 1)])
|
||||||
|
|
||||||
|
// Write new ByteRange.
|
||||||
|
if _, err := context.OutputBuffer.Write([]byte(dst)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
context.OutputBuffer.Write(file_content[(context.ByteRangeValues[0]+context.ByteRangeValues[1]+1)+int64(len(dst)):])
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -35,18 +35,18 @@ func (context *SignContext) writeTrailer() error {
|
|||||||
trailer_string = strings.Replace(trailer_string, info_string, new_info, -1)
|
trailer_string = strings.Replace(trailer_string, info_string, new_info, -1)
|
||||||
|
|
||||||
// Write the new trailer.
|
// Write the new trailer.
|
||||||
if _, err := context.OutputFile.Write([]byte(trailer_string)); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte(trailer_string)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the new xref start position.
|
// Write the new xref start position.
|
||||||
if _, err := context.OutputFile.Write([]byte("\nstartxref\n" + strconv.FormatInt(context.NewXrefStart, 10) + "\n")); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte("\nstartxref\n" + strconv.FormatInt(context.NewXrefStart, 10) + "\n")); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write PDF ending.
|
// Write PDF ending.
|
||||||
if _, err := context.OutputFile.Write([]byte("%%EOF")); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte("%%EOF")); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -20,7 +20,6 @@ func (context *SignContext) createVisualSignature() (visual_signature string, er
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
rootPtr := root.GetPtr()
|
rootPtr := root.GetPtr()
|
||||||
context.CatalogData.RootString = strconv.Itoa(int(rootPtr.GetID())) + " " + strconv.Itoa(int(rootPtr.GetGen())) + " R"
|
context.CatalogData.RootString = strconv.Itoa(int(rootPtr.GetID())) + " " + strconv.Itoa(int(rootPtr.GetGen())) + " R"
|
||||||
|
|
||||||
|
@@ -1,12 +1,12 @@
|
|||||||
package sign
|
package sign
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"compress/zlib"
|
||||||
|
"encoding/binary"
|
||||||
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
"encoding/hex"
|
|
||||||
"compress/zlib"
|
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (context *SignContext) writeXref() error {
|
func (context *SignContext) writeXref() error {
|
||||||
@@ -31,12 +31,12 @@ func (context *SignContext) writeXrefTable() error {
|
|||||||
xref_size := "xref\n0 " + strconv.FormatInt(context.PDFReader.XrefInformation.ItemCount, 10)
|
xref_size := "xref\n0 " + strconv.FormatInt(context.PDFReader.XrefInformation.ItemCount, 10)
|
||||||
new_xref_size := "xref\n0 " + strconv.FormatInt(context.PDFReader.XrefInformation.ItemCount+4, 10)
|
new_xref_size := "xref\n0 " + strconv.FormatInt(context.PDFReader.XrefInformation.ItemCount+4, 10)
|
||||||
|
|
||||||
if _, err := context.OutputFile.Write([]byte(new_xref_size)); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte(new_xref_size)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the old xref table to the output pdf.
|
// Write the old xref table to the output pdf.
|
||||||
if err := writePartFromSourceFileToTargetFile(context.InputFile, context.OutputFile, context.PDFReader.XrefInformation.StartPos+int64(len(xref_size)), context.PDFReader.XrefInformation.Length-int64(len(xref_size))); err != nil {
|
if err := writePartFromSourceFileToTargetFile(context.InputFile, context.OutputBuffer, context.PDFReader.XrefInformation.StartPos+int64(len(xref_size)), context.PDFReader.XrefInformation.Length-int64(len(xref_size))); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,7 +45,7 @@ func (context *SignContext) writeXrefTable() error {
|
|||||||
visual_signature_xref_line := leftPad(visual_signature_object_start_position, "0", 10-len(visual_signature_object_start_position)) + " 00000 n \n"
|
visual_signature_xref_line := leftPad(visual_signature_object_start_position, "0", 10-len(visual_signature_object_start_position)) + " 00000 n \n"
|
||||||
|
|
||||||
// Write the new catalog xref line.
|
// Write the new catalog xref line.
|
||||||
if _, err := context.OutputFile.Write([]byte(visual_signature_xref_line)); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte(visual_signature_xref_line)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@ func (context *SignContext) writeXrefTable() error {
|
|||||||
catalog_xref_line := leftPad(catalog_object_start_position, "0", 10-len(catalog_object_start_position)) + " 00000 n \n"
|
catalog_xref_line := leftPad(catalog_object_start_position, "0", 10-len(catalog_object_start_position)) + " 00000 n \n"
|
||||||
|
|
||||||
// Write the new catalog xref line.
|
// Write the new catalog xref line.
|
||||||
if _, err := context.OutputFile.Write([]byte(catalog_xref_line)); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte(catalog_xref_line)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +63,7 @@ func (context *SignContext) writeXrefTable() error {
|
|||||||
info_xref_line := leftPad(info_object_start_position, "0", 10-len(info_object_start_position)) + " 00000 n \n"
|
info_xref_line := leftPad(info_object_start_position, "0", 10-len(info_object_start_position)) + " 00000 n \n"
|
||||||
|
|
||||||
// Write the new signature xref line.
|
// Write the new signature xref line.
|
||||||
if _, err := context.OutputFile.Write([]byte(info_xref_line)); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte(info_xref_line)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ func (context *SignContext) writeXrefTable() error {
|
|||||||
signature_xref_line := leftPad(signature_object_start_position, "0", 10-len(signature_object_start_position)) + " 00000 n \n"
|
signature_xref_line := leftPad(signature_object_start_position, "0", 10-len(signature_object_start_position)) + " 00000 n \n"
|
||||||
|
|
||||||
// Write the new signature xref line.
|
// Write the new signature xref line.
|
||||||
if _, err := context.OutputFile.Write([]byte(signature_xref_line)); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte(signature_xref_line)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,7 +107,6 @@ func (context *SignContext) writeXrefStream() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
new_info := "Info " + strconv.FormatInt(int64(context.InfoData.ObjectId), 10) + " 0 R"
|
new_info := "Info " + strconv.FormatInt(int64(context.InfoData.ObjectId), 10) + " 0 R"
|
||||||
new_root := "Root " + strconv.FormatInt(int64(context.CatalogData.ObjectId), 10) + " 0 R"
|
new_root := "Root " + strconv.FormatInt(int64(context.CatalogData.ObjectId), 10) + " 0 R"
|
||||||
|
|
||||||
@@ -116,21 +115,21 @@ func (context *SignContext) writeXrefStream() error {
|
|||||||
id0 := hex.EncodeToString([]byte(id.Index(0).RawString()))
|
id0 := hex.EncodeToString([]byte(id.Index(0).RawString()))
|
||||||
id1 := hex.EncodeToString([]byte(id.Index(0).RawString()))
|
id1 := hex.EncodeToString([]byte(id.Index(0).RawString()))
|
||||||
|
|
||||||
new_xref := strconv.Itoa(int(context.SignData.ObjectId + 1)) + " 0 obj\n"
|
new_xref := strconv.Itoa(int(context.SignData.ObjectId+1)) + " 0 obj\n"
|
||||||
new_xref += "<< /Type /XRef /Length " + strconv.Itoa(len(streamBytes)) + " /Filter /FlateDecode /DecodeParms << /Columns 5 /Predictor 12 >> /W [ 1 3 1 ] /Prev " + strconv.FormatInt(context.PDFReader.XrefInformation.StartPos, 10) + " /Size " + strconv.FormatInt(context.PDFReader.XrefInformation.ItemCount+5, 10) + " /Index [ " + strconv.FormatInt(context.PDFReader.XrefInformation.ItemCount, 10) + " 5 ] /" + new_info + " /" + new_root + " /ID [<" + id0 + "><" + id1 + ">] >>\n"
|
new_xref += "<< /Type /XRef /Length " + strconv.Itoa(len(streamBytes)) + " /Filter /FlateDecode /DecodeParms << /Columns 5 /Predictor 12 >> /W [ 1 3 1 ] /Prev " + strconv.FormatInt(context.PDFReader.XrefInformation.StartPos, 10) + " /Size " + strconv.FormatInt(context.PDFReader.XrefInformation.ItemCount+5, 10) + " /Index [ " + strconv.FormatInt(context.PDFReader.XrefInformation.ItemCount, 10) + " 5 ] /" + new_info + " /" + new_root + " /ID [<" + id0 + "><" + id1 + ">] >>\n"
|
||||||
if _, err := context.OutputFile.Write([]byte(new_xref)); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte(new_xref)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := context.OutputFile.Write([]byte("stream\n")); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte("stream\n")); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := context.OutputFile.Write(streamBytes); err != nil {
|
if _, err := context.OutputBuffer.Write(streamBytes); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := context.OutputFile.Write([]byte("\nendstream\n")); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte("\nendstream\n")); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,9 +137,9 @@ func (context *SignContext) writeXrefStream() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func writeXrefStreamLine(b *bytes.Buffer, xreftype byte, offset int, gen byte) {
|
func writeXrefStreamLine(b *bytes.Buffer, xreftype byte, offset int, gen byte) {
|
||||||
b.WriteByte(xreftype);
|
b.WriteByte(xreftype)
|
||||||
b.Write(encodeInt(offset));
|
b.Write(encodeInt(offset))
|
||||||
b.WriteByte(gen);
|
b.WriteByte(gen)
|
||||||
}
|
}
|
||||||
|
|
||||||
func encodeInt(i int) []byte {
|
func encodeInt(i int) []byte {
|
||||||
@@ -178,7 +177,6 @@ func EncodePNGSUBBytes(columns int, data []byte) ([]byte, error) {
|
|||||||
return b.Bytes(), nil
|
return b.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func EncodePNGUPBytes(columns int, data []byte) ([]byte, error) {
|
func EncodePNGUPBytes(columns int, data []byte) ([]byte, error) {
|
||||||
rowCount := len(data) / columns
|
rowCount := len(data) / columns
|
||||||
if len(data)%columns != 0 {
|
if len(data)%columns != 0 {
|
||||||
@@ -215,4 +213,4 @@ func EncodePNGUPBytes(columns int, data []byte) ([]byte, error) {
|
|||||||
w.Close()
|
w.Close()
|
||||||
|
|
||||||
return b.Bytes(), nil
|
return b.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
43
sign/sign.go
43
sign/sign.go
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"bitbucket.org/digitorus/pdf"
|
"bitbucket.org/digitorus/pdf"
|
||||||
"bitbucket.org/digitorus/pdfsign/revocation"
|
"bitbucket.org/digitorus/pdfsign/revocation"
|
||||||
|
"github.com/mattetti/filebuffer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CatalogData struct {
|
type CatalogData struct {
|
||||||
@@ -62,8 +63,9 @@ type SignDataSignatureInfo struct {
|
|||||||
|
|
||||||
type SignContext struct {
|
type SignContext struct {
|
||||||
Filesize int64
|
Filesize int64
|
||||||
InputFile *os.File
|
InputFile io.ReadSeeker
|
||||||
OutputFile *os.File
|
OutputFile io.Writer
|
||||||
|
OutputBuffer *filebuffer.Buffer
|
||||||
SignData SignData
|
SignData SignData
|
||||||
CatalogData CatalogData
|
CatalogData CatalogData
|
||||||
VisualSignData VisualSignData
|
VisualSignData VisualSignData
|
||||||
@@ -100,14 +102,18 @@ func SignFile(input string, output string, sign_data SignData) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Sign(input_file, output_file, rdr, size, sign_data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Sign(input io.ReadSeeker, output io.Writer, rdr *pdf.Reader, size int64, sign_data SignData) error {
|
||||||
sign_data.ObjectId = uint32(rdr.XrefInformation.ItemCount) + 3
|
sign_data.ObjectId = uint32(rdr.XrefInformation.ItemCount) + 3
|
||||||
|
|
||||||
// We do size+1 because we insert a newline.
|
// We do size+1 because we insert a newline.
|
||||||
context := SignContext{
|
context := SignContext{
|
||||||
Filesize: size + 1,
|
Filesize: size + 1,
|
||||||
PDFReader: rdr,
|
PDFReader: rdr,
|
||||||
InputFile: input_file,
|
InputFile: input,
|
||||||
OutputFile: output_file,
|
OutputFile: output,
|
||||||
VisualSignData: VisualSignData{
|
VisualSignData: VisualSignData{
|
||||||
ObjectId: uint32(rdr.XrefInformation.ItemCount),
|
ObjectId: uint32(rdr.XrefInformation.ItemCount),
|
||||||
},
|
},
|
||||||
@@ -120,7 +126,7 @@ func SignFile(input string, output string, sign_data SignData) error {
|
|||||||
SignData: sign_data,
|
SignData: sign_data,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = context.SignPDF()
|
err := context.SignPDF()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -129,18 +135,15 @@ func SignFile(input string, output string, sign_data SignData) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (context *SignContext) SignPDF() error {
|
func (context *SignContext) SignPDF() error {
|
||||||
// Copy old file into new file.
|
context.OutputBuffer = filebuffer.New([]byte{})
|
||||||
if _, err := io.Copy(context.OutputFile, context.InputFile); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err := context.OutputFile.Sync()
|
// Copy old file into new file.
|
||||||
if err != nil {
|
if _, err := io.Copy(context.OutputBuffer, context.InputFile); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// File always needs an empty line after %%EOF.
|
// File always needs an empty line after %%EOF.
|
||||||
if _, err := context.OutputFile.Write([]byte("\n")); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte("\n")); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,7 +168,7 @@ func (context *SignContext) SignPDF() error {
|
|||||||
context.VisualSignData.Length = int64(len(visual_signature))
|
context.VisualSignData.Length = int64(len(visual_signature))
|
||||||
|
|
||||||
// Write the new catalog object.
|
// Write the new catalog object.
|
||||||
if _, err := context.OutputFile.Write([]byte(visual_signature)); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte(visual_signature)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,7 +180,7 @@ func (context *SignContext) SignPDF() error {
|
|||||||
context.CatalogData.Length = int64(len(catalog))
|
context.CatalogData.Length = int64(len(catalog))
|
||||||
|
|
||||||
// Write the new catalog object.
|
// Write the new catalog object.
|
||||||
if _, err := context.OutputFile.Write([]byte(catalog)); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte(catalog)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,7 +195,7 @@ func (context *SignContext) SignPDF() error {
|
|||||||
context.InfoData.Length = int64(len(info))
|
context.InfoData.Length = int64(len(info))
|
||||||
|
|
||||||
// Write the new catalog object.
|
// Write the new catalog object.
|
||||||
if _, err := context.OutputFile.Write([]byte(info)); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte(info)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,7 +209,7 @@ func (context *SignContext) SignPDF() error {
|
|||||||
context.SignatureContentsStartByte = signature_contents_start_byte
|
context.SignatureContentsStartByte = signature_contents_start_byte
|
||||||
|
|
||||||
// Write the new signature object.
|
// Write the new signature object.
|
||||||
if _, err := context.OutputFile.Write([]byte(signature_object)); err != nil {
|
if _, err := context.OutputBuffer.Write([]byte(signature_object)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,10 +232,10 @@ func (context *SignContext) SignPDF() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = context.OutputFile.Sync()
|
context.OutputBuffer.Seek(0, 0)
|
||||||
if err != nil {
|
file_content := context.OutputBuffer.Buff.Bytes()
|
||||||
return err
|
|
||||||
}
|
context.OutputFile.Write(file_content)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
package sign
|
package sign
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -131,7 +133,30 @@ func TestSignPDF(t *testing.T) {
|
|||||||
|
|
||||||
fmt.Printf("Signing file %s\n", f.Name())
|
fmt.Printf("Signing file %s\n", f.Name())
|
||||||
|
|
||||||
err = SignFile("../testfiles/"+f.Name(), "../testfiles/"+f.Name()+".tmp", SignData{
|
input_file, err := os.Open("../testfiles/" + f.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s: %s", f.Name(), err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer input_file.Close()
|
||||||
|
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
output_file := bufio.NewWriter(&buffer)
|
||||||
|
|
||||||
|
finfo, err := input_file.Stat()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s: %s", f.Name(), err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
size := finfo.Size()
|
||||||
|
|
||||||
|
rdr, err := pdf.NewReader(input_file, size)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s: %s", f.Name(), err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = Sign(input_file, output_file, rdr, size, SignData{
|
||||||
Signature: SignDataSignature{
|
Signature: SignDataSignature{
|
||||||
Info: SignDataSignatureInfo{
|
Info: SignDataSignatureInfo{
|
||||||
Name: "Jeroen Bobbeldijk",
|
Name: "Jeroen Bobbeldijk",
|
||||||
@@ -153,9 +178,6 @@ func TestSignPDF(t *testing.T) {
|
|||||||
RevocationFunction: DefaultEmbedRevocationStatusFunction,
|
RevocationFunction: DefaultEmbedRevocationStatusFunction,
|
||||||
})
|
})
|
||||||
|
|
||||||
// Cleanup old files.
|
|
||||||
defer os.Remove("../testfiles/"+f.Name()+".tmp")
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("%s: %s", f.Name(), err.Error())
|
t.Errorf("%s: %s", f.Name(), err.Error())
|
||||||
return
|
return
|
||||||
@@ -191,7 +213,30 @@ func BenchmarkSignPDF(b *testing.B) {
|
|||||||
certificate_chains := make([][]*x509.Certificate, 0)
|
certificate_chains := make([][]*x509.Certificate, 0)
|
||||||
|
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
err := SignFile("../testfiles/testfile20.pdf", "../testfiles/testfile20.pdf.tmp", SignData{
|
input_file, err := os.Open("../testfiles/testfile20.pdf")
|
||||||
|
if err != nil {
|
||||||
|
b.Errorf("%s: %s", "testfile20.pdf", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer input_file.Close()
|
||||||
|
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
output_file := bufio.NewWriter(&buffer)
|
||||||
|
|
||||||
|
finfo, err := input_file.Stat()
|
||||||
|
if err != nil {
|
||||||
|
b.Errorf("%s: %s", "testfile20.pdf", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
size := finfo.Size()
|
||||||
|
|
||||||
|
rdr, err := pdf.NewReader(input_file, size)
|
||||||
|
if err != nil {
|
||||||
|
b.Errorf("%s: %s", "testfile20.pdf", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = Sign(input_file, output_file, rdr, size, SignData{
|
||||||
Signature: SignDataSignature{
|
Signature: SignDataSignature{
|
||||||
Info: SignDataSignatureInfo{
|
Info: SignDataSignatureInfo{
|
||||||
Name: "Jeroen Bobbeldijk",
|
Name: "Jeroen Bobbeldijk",
|
||||||
@@ -213,8 +258,6 @@ func BenchmarkSignPDF(b *testing.B) {
|
|||||||
RevocationFunction: DefaultEmbedRevocationStatusFunction,
|
RevocationFunction: DefaultEmbedRevocationStatusFunction,
|
||||||
})
|
})
|
||||||
|
|
||||||
os.Remove("../testfiles/testfile20.pdf.tmp")
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Errorf("%s: %s", "testfile20.pdf", err.Error())
|
b.Errorf("%s: %s", "testfile20.pdf", err.Error())
|
||||||
return
|
return
|
||||||
|
@@ -12,11 +12,11 @@ import (
|
|||||||
|
|
||||||
"bitbucket.org/digitorus/pdf"
|
"bitbucket.org/digitorus/pdf"
|
||||||
"bitbucket.org/digitorus/pdfsign/revocation"
|
"bitbucket.org/digitorus/pdfsign/revocation"
|
||||||
|
"crypto"
|
||||||
"github.com/digitorus/pkcs7"
|
"github.com/digitorus/pkcs7"
|
||||||
"github.com/digitorus/timestamp"
|
"github.com/digitorus/timestamp"
|
||||||
"log"
|
|
||||||
"golang.org/x/crypto/ocsp"
|
"golang.org/x/crypto/ocsp"
|
||||||
"crypto"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
|
Reference in New Issue
Block a user