src/reportlab/lib/pdfencrypt.py
branchpy33
changeset 3723 99aa837b6703
parent 3721 0c93dd8ff567
child 3746 b7143a1c34c6
equal deleted inserted replaced
3722:29c11b905751 3723:99aa837b6703
     2 #see license.txt for license details
     2 #see license.txt for license details
     3 __version__=''' $Id$ '''
     3 __version__=''' $Id$ '''
     4 
     4 
     5 """helpers for pdf encryption/decryption"""
     5 """helpers for pdf encryption/decryption"""
     6 
     6 
     7 import string, sys, os
     7 import sys, os
     8 try:
     8 try:
     9     from hashlib import md5
     9     from hashlib import md5
    10 except ImportError:
    10 except ImportError:
    11     from md5 import md5
    11     from md5 import md5
    12 
    12 
    13 from reportlab.lib.utils import getStringIO
    13 from reportlab.lib.utils import getBytesIO
    14 import tempfile
    14 import tempfile
    15 
    15 
    16 from reportlab.pdfgen.canvas import Canvas
    16 from reportlab.pdfgen.canvas import Canvas
    17 from reportlab.pdfbase import pdfutils
    17 from reportlab.pdfbase import pdfutils
    18 from reportlab.platypus.flowables import Flowable
    18 from reportlab.platypus.flowables import Flowable
   174         for ch in key:
   174         for ch in key:
   175             out = out + chr(xor(num, ord(ch)))
   175             out = out + chr(xor(num, ord(ch)))
   176         return out
   176         return out
   177 
   177 
   178 def hexchar(x):
   178 def hexchar(x):
   179     return chr(string.atoi(x, 16))
   179     return chr(int(x, 16))
   180 
   180 
   181 def hexText(text):
   181 def hexText(text):
   182     "a legitimate way to show strings in PDF"
   182     "a legitimate way to show strings in PDF"
   183     out = ''
   183     out = ''
   184     for char in text:
   184     for char in text:
   194         slice = hexText[i*2: i*2+2]
   194         slice = hexText[i*2: i*2+2]
   195         char = chr(eval('0x'+slice))
   195         char = chr(eval('0x'+slice))
   196         out = out + char
   196         out = out + char
   197     return out
   197     return out
   198 
   198 
   199 PadString = string.join(list(map(hexchar, string.split(string.strip(padding)))), "")
   199 PadString = ''.join(map(hexchar, padding.strip().split()))
   200 
   200 
   201 def encryptionkey(password, OwnerKey, Permissions, FileId1, revision=2):
   201 def encryptionkey(password, OwnerKey, Permissions, FileId1, revision=2):
   202     # FileId1 is first string of the fileid array
   202     # FileId1 is first string of the fileid array
   203     # add padding string
   203     # add padding string
   204     #AR force same as iText example
   204     #AR force same as iText example
   406     names = list(bboxInfo.keys())
   406     names = list(bboxInfo.keys())
   407 
   407 
   408     firstPageSize = bboxInfo['PageForms0'][2:]
   408     firstPageSize = bboxInfo['PageForms0'][2:]
   409 
   409 
   410     #now make a new PDF document
   410     #now make a new PDF document
   411     buf = getStringIO()
   411     buf = getBytesIO()
   412     canv = Canvas(buf, pagesize=firstPageSize)
   412     canv = Canvas(buf, pagesize=firstPageSize)
   413 
   413 
   414     # set a standard ID while debugging
   414     # set a standard ID while debugging
   415     if CLOBBERID:
   415     if CLOBBERID:
   416         canv._doc._ID = "[(xxxxxxxxxxxxxxxx)(xxxxxxxxxxxxxxxx)]"
   416         canv._doc._ID = "[(xxxxxxxxxxxxxxxx)(xxxxxxxxxxxxxxxx)]"