o
    &zh/x                     @   s  d Z 	 ddlZddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ zddlZddlmZ W n eyZ   dZY nw dd ZdZdZejZdZdZ dZ!dZ"dZ#dZ$dZ%e&ddD ]Z'e%de'> B Z%qyej(rda)dZ*dd Z+nej,Z+G dd dZ-G dd deZ.dZ/dd Z0d d! Z1ed"2d#d$ e/3 4 D Z5d%d& Z6dId'd(Z7d)d* Z8e5ddfd+d,Z9d-d. Z:dId/d0Z;d1d2 Z<d3d4 Z=			5dJd6d7Z>G d8d9 d9e-eZ?			5dJd:d;Z@			5dJd<d=ZA			5dJd>d?ZBd@dA ZCdBdC ZDeEdDkr>dEdF ejFD ZGeGr.dGdF ejFD e_FeHeGZdHejFv r9e=  dS eD  dS dS )Kz3.3.0    N)hexlify	unhexlify)md5)BytesIO)asBytesint2ByterawBytesasNative)Canvas)	PDFObject)Flowable)	rl_config)sha256c                    s"    dkr|S t  fdd|D S )z9xor's each byte of the key with the number, which is <256r   c                 3   s    | ]} |A V  qd S N ).0knumr   S/var/www/html/kangema/venv/lib/python3.10/site-packages/reportlab/lib/pdfencrypt.py	<genexpr>   s    zxorKey.<locals>.<genexpr>)bytes)r   keyr   r   r   xorKey   s   r                            s   ܫovp\CB~Eg8fSj'yGY-QL>iĴ'kpͪ| Ǘ;,\H}܅>|ŗTT!+E#Oj"ˌHBB~71.ש2}m3/WK;?#`W입#zO(zi!	|w,I_D`A}+r-%F(@\cL2(BMUGc                 C   s*   dd t | D }t|}t|  d a|S )Nc                 S   s   g | ]
}t |t d   qS )   )_os_random_b_os_random_x)r   ir   r   r   
<listcomp>2   s    zos_urandom.<locals>.<listcomp>r"   )ranger   r$   )nbr   r   r   
os_urandom0   s   r*   c                   @   sL   e Zd ZdZdddZdd Zdd	 Zd
d ZdddZdd Z	dd Z
dS )StandardEncryptionr   Nr   c                 C   s   || _ |r	|| _n|| _|du rtj}|dkrd| _n|dkr#d| _n|dkr1ts-tdd| _ntd	t| || _|| _	|| _
|| _d | _ | _ | _ | _ | _ | _| _dS )
a*  
        This class defines the encryption properties to be used while creating a pdf document.
        Once initiated, a StandardEncryption object can be applied to a Canvas or a BaseDocTemplate.
        The userPassword parameter sets the user password on the encrypted pdf.
        The ownerPassword parameter sets the owner password on the encrypted pdf.
        The boolean flags canPrint, canModify, canCopy, canAnnotate determine wether a user can
        perform the corresponding actions on the pdf when only a user password has been supplied.
        If the user supplies the owner password while opening the pdf, all actions can be performed regardless
        of the flags.
        Note that the security provided by these encryption settings (and even more so for the flags) is very weak.
        N(   r         r"   Astrength==256 is not supported as package pyaes is not importable   Unknown encryption strength=%s)userPasswordownerPasswordr   encryptionStrengthrevisionpyaes
ValueErrorreprcanPrint	canModifycanCopycanAnnotateOUPr   OEUEPerms)selfr2   r3   r9   r:   r;   r<   strengthr   r   r   __init__<   s(   .zStandardEncryption.__init__c                 C   s   | | _  | _ | _| _d S r   )r9   r:   r;   r<   )rC   valuer   r   r   setAllPermissions^   s   z$StandardEncryption.setAllPermissionsc                 C   sH   d}| j r	|tB }| jr|tB }| jr|tB }| jr|tB }|tB }|S )Nr   )	r9   	printabler:   
modifiabler;   copypastabler<   annotatable
higherbits)rC   pr   r   r   permissionBitsc   s   z!StandardEncryption.permissionBitsc                 C   s:   | j std| jdu rtdt| j| j| j|| jdS )zencode a string, stream, textencryption not prepared!Nznot registered in PDF objectr5   )preparedr7   objnum	encodePDFr   versionr5   )rC   tr   r   r   encodek   s
   
zStandardEncryption.encodec                 C   s  t dkrtd| j  | jrtd|r|}n| }|j }tr$d}t dkr<td| j	  td| j
  td|  t|  d | _trJd	| _t dkrWtd
t| j  | jdkrd}td}td}ttd| _t dkrtdt|  tdt|  tdt| j  tt| j	d d | }| | | | _t dkrtdt| j  tt| j	d d | }ttj| |d}	|	| j| _|  j|	 7  _t dkrtdt| j  td}
td}tt| j
d d |
 | j }| |
 | | _t dkrtdt| j  tt| j
d d | | j }ttj| |d}	|	| j| _|  j|	 7  _t dkrMtdt| j  | jd@ | jd? d@ | jd? d@ | jd? d@ ddddtdtdtdtdddddg}ttj| j|d}	|	t|| _|  j|	 7  _t dkrtd t| j  nR| jd!v rt | j	| j
| j| _t dkrtd"t| j  t!| j	| j| j|| jd#| _t dkrtdt| j  t"| j| j|d$| _t dkrtd%t| j  d  | _#| _$d| _d S )&N
   z-StandardEncryption.prepare(...) - revision %dzencryption already prepared!xxxxxxxxxxxxxxxxzuserPassword    = %rzownerPassword   = %rzinternalID      = %rl        izself.P          = %sr0   s                   r   r   zuvs      (hex)  = %szuks      (hex)  = %szself.key (hex)  = %s   zself.U (hex)  = %sivzself.UE (hex)  = %szself.O (hex)  = %szself.OE (hex)  = %s   r      Tadr)   r   zself.Perms (hex)  = %sr   r.   zself.O (as hex) = %srP   )r5   
documentIdzself.U (as hex) = %s)%DEBUGprintr5   rQ   r7   ID	signaturedigest	CLOBBERIDr2   r3   intrN   r?   CLOBBERPERMISSIONSr8   r*   r   r   hexTextr   r>   r6   	EncrypterAESModeOfOperationCBCfeedrA   r=   r@   ordr   rB   computeOencryptionkeycomputeUrR   rT   )rC   document
overrideID
internalID
externalIDr[   uvsuksmd	encrypterovsokspermsarrr   r   r   preparer   s   

 
 





zStandardEncryption.preparec                 C   s   | j std|| _|| _d S )NrO   )rQ   r7   rR   rT   )rC   rR   rT   r   r   r   register   s   
zStandardEncryption.registerc              	   C   s2   | j stdt| j| j| j| j| j| j| j	dS )NrO   r=   r@   r>   rA   r?   rB   r5   )
rQ   r7   StandardEncryptionDictionaryr=   r@   r>   rA   r?   rB   r5   rC   r   r   r   info   s   $zStandardEncryption.info)Nr   r   r   r   Nr   )__name__
__module____qualname__rQ   rE   rG   rN   rV   r~   r   r   r   r   r   r   r+   :   s    
"
|r+   c                   @       e Zd ZdZdd Zdd ZdS )r   r   c                 C   s2   ||||||f\| _ | _| _| _| _| _|| _d S r   r   )rC   r=   r@   r>   rA   r?   rB   r5   r   r   r   rE      s   (
z%StandardEncryptionDictionary.__init__c           
      C   s.  ddl m}m}m} | }|dt| jt| j| jd}| jdkrtd|d< d|d< d|d	< t| j|d
< t| j|d< t| j	|d< t| j
|d< t| j|d< |d|d< |d|d< d|d|dd}d||i}|||d< n| jdkrd|d< d|d< d|d	< nd|d< d|d	< ||}	|	|S )Nr   )DummyDocPDFDictionaryPDFNameStandard)Filterr=   r>   r?   r0   r"   LengthRVr=   r>   r@   rA   rB   StdCFStrFStmFr   DocOpenAESV3)r   	AuthEventCFMCFr.   r-   r   r   )reportlab.pdfbase.pdfdocr   r   r   rk   r=   r>   r?   r5   r@   rA   rB   format)
rC   rs   r   r   r   dummydictstdcfcfpdfdictr   r   r   r      sB   



z#StandardEncryptionDictionary.formatN)r   r   r   __RefOnly__rE   r   r   r   r   r   r      s    r   za
28 BF 4E 5E 4E 75 8A 41 64 00 4E 56 FF FA 01 08
2E 2E 00 B6 D0 68 3E 80 2F 0C A9 FE 64 53 69 7A
c                 C   s   dt tt|   S )z'a legitimate way to show strings in PDFz<%s>)r	   r   r   upper)textr   r   r   rk   -  s   rk   c                 C   s0   t | d dd t | d dd t| dd S )Nr   <zbad hex text>r   )equalityCheckr   )rk   r   r   r   	unHexText1  s   r    c                 c   s    | ]
}t t|d V  qdS )r   N)chrri   )r   cr   r   r   r   6  s    r   c                 C   s^   | d u r-t j}|dkrd} | S |dkrd} | S |dkr%ts!tdd} | S tdt| | S )	Nr,   r   r-   r.   r"   r/   r0   r1   )r   r4   r6   r7   r8   )r5   rD   r   r   r   checkRevision8  s   	r   c              
   C   s
  t |}t| t } | d d } |}d}tdD ]}|d@ }|d? }|t|d 7 }qtt| }	|	t| |	t| |	t| |	 }
|dkrU|
d d }n|d	krltd
D ]}t|
 }
q]|
d d }tdkrt	dt
dd | |||||fD   |S )Nr       r   r\   r   r"   r   r0   r.   2   r   rW   z"encryptionkey(%s,%s,%s,%s,%s)==>%sc                 S      g | ]}t t|qS r   rk   strr   xr   r   r   r&   e      z!encryptionkey.<locals>.<listcomp>)r   r   	PadStringr'   r   r   updaterg   rc   rd   tuple)passwordOwnerKeyPermissionsFileId1r5   rM   permissionsStringr%   bytehash	md5outputr   r   r   r   r   rq   G  s,   .rq   c                 C   s>  ddl m} |dv sJ d| |s| }t|t }|dd }t| t }|d d }t| }tdkrLtdttt|t|t|t||f  |dkr\||d d	 	|}n,|d
krt
dD ]}	t| }qd|d d }|}t
dD ]}	t|	|}
||
	|}qytdkrtdtdd | |||fD   |S )Nr   ArcIVra   zUnknown algorithm revision %sr   rW   zEPadString=%s
ownerPad=%s
password=%s
userPad=%s
digest=%s
revision=%sr   r0   r.   r   r      zcomputeO(%s,%s,%s)==>%sc                 S   r   r   r   r   r   r   r   r&     r   zcomputeO.<locals>.<listcomp>)reportlab.lib.arcivr   r   r   r   rg   rc   rd   asciirV   r'   r   r   )r2   r3   r5   r   ownerPadr   userPadrg   r=   r%   thisKeyr   r   r   rp   h  s,   4
*rp   c           
   	   C   s   t |}ddlm} |dkr|| |}nH|dkr^|d us"J dtt}|t| | }|| |}t	ddD ]}t
|| }	||	|}q=t|dk r\|d	7 }t|dk sR|}td
krttdtdd | ||||fD   |S )Nr   r   r   r.   z+Revision 3 algorithm needs the document ID!r   r   r       rW   zcomputeU(%s,%s,%s,%s)==>%sc                 S   r   r   r   r   r   r   r   r&     r   zcomputeU.<locals>.<listcomp>)r   r   r   rV   r   r   r   r   rg   r'   r   lenrc   rd   r   )
rq   encodestringr5   rb   r   resulthtmpr(   r   r   r   r   rr     s&   
,rr   c                 C   s6   t | |}|tkrt|ttkrtdtdd S )Nz&lengths don't match! (password failed)z;decode of U doesn't match fixed padstring (password failed))rr   r   r   r7   )rq   r>   decodedr   r   r   checkU  s   
r   c              
   C   s  t |}|dv rY| }|}tdD ]}|t|d@ 7 }|d? }q|}tdD ]}|t|d@ 7 }|d? }q%t| }|dkrE|dd } n|dkrK|} dd	lm}	 |	| |}
nP|d
krtd}t	
t	j| |d}t|}d}|dkr{d|d  nd| }|dkrt|| }t|tr|| d}n|t|7 }||| }
|
| 7 }
tdkrtdtdd | |||||
fD   |
S )zEncodes a string or streamra   r.   r\   r   r   NrW   r   r   r0   r   rZ   r   zutf-8zencodePDF(%s,%s,%s,%s,%s)==>%sc                 S   r   r   r   r   r   r   r   r&     r   zencodePDF.<locals>.<listcomp>)r   r'   r   r   rg   r   r   rV   r*   r6   rl   rm   r   r   
isinstancer   r   rn   rc   rd   r   )r   objectNumbergenerationNumberstringr5   newkeyr(   r%   r   r   	encryptedr[   rz   
string_lenpaddingpadding_lenr   r   r   rS     sB   


.rS   c                 C   s   | |ksJ d||| f d S )Nz%s
 expected=%s
 observed=%sr   )observedexpectedlabelr   r   r   r     s   r   c                  C   s   t dddd} | jd dd d}d}d	}tt| j|d
 tt| j|d tt| j|d t dddd} | jd dd d}d}d}tt| j|d tt| j|d tt| j|d d S )NUserOwnerr,   rD   rX   )rt   zB<FA7F558FACF8205D25A7F1ABFA02629F707AE7B0211A2BB26F5DF4C30F684301>zB<09F26CF46190AF8F93B304AD50C16B615DC43C228C9B2D2EA34951A80617B2B1>z<BB2C00EB3D>z40 bit O valuez40 bit U valuez40 bit key valueuserpass	ownerpassr-   zB<68E5704AC779A5F0CD89704406587A52F25BF61CADC56A0F8DB6C4DB0052534D>zB<A9AE45CDE827FE0B7D6536267948836A00000000000000000000000000000000>z"<13DDE7585D9BE366C976DDD56AF541D1>z128 bit O valuez128 bit U valuez128 key value)r+   r~   r   rk   r=   r>   r   )enc	expectedO	expectedUexpectedKeyr   r   r   test  s    r   r,   c           	   	   C   s"   t |||||||d}|| j_dS )z2Applies encryption to the document being generatedr   N)r+   _docencrypt)	canvasr2   r3   r9   r:   r;   r<   rD   r   r   r   r   encryptCanvas  s
   r   c                   @   r   )EncryptionFlowablezDrop this in your Platypus story and it will set up the encryption options.

    If you do it multiple times, the last one before saving will win.c                 C   s   dS )N)r   r   r   )rC   
availWidthavailHeightr   r   r   wrap  s   zEncryptionFlowable.wrapc                 C   s&   t | j| j| j| j| j| j| j d S r   )r   canvr2   r3   r9   r:   r;   r<   r   r   r   r   draw	  s   zEncryptionFlowable.drawN)r   r   r   __doc__r   r   r   r   r   r   r     s    r   c                 C   s   t d)z*For use in Platypus.  Call before build().zNot implemented yet)	Exception)dtr2   r3   r9   r:   r;   r<   rD   r   r   r   encryptDocTemplate  s   r   c              
   C   s   z
ddl m}m}	 W n ty   tdw || ddd\}
}t|
 }|
d dd }t }t||d	}tr<d
|j	_
|	||}|D ]}||
| dd  || |  qCt||||||||d |  | S )a  accepts a PDF file 'as a byte array in memory'; return encrypted one.

    This is a high level convenience and does not touch the hard disk in any way.
    If you are encrypting the same file over and over again, it's better to use
    pageCatcher and cache the results.r   )storeFormsInMemoryrestoreFormsInMemoryz~reportlab.lib.pdfencrypt.encryptPdfInMemory failed because rlextra cannot be imported.
See https://www.reportlab.com/downloadsr   )allBBoxes
PageForms0r   N)pagesizez&[(xxxxxxxxxxxxxxxx)(xxxxxxxxxxxxxxxx)]r   )rlextra.pageCatcher.pageCatcherr   r   ImportErrorlistkeysr   r
   rh   r   _IDsetPageSizedoFormshowPager   savegetvalue)inputPDFr2   r3   r9   r:   r;   r<   rD   r   r   bboxInfopickledFormsnamesfirstPageSizebufr   	formNamesformNamer   r   r   encryptPdfInMemory  s0   



r
  c	              
   C   s>   t | d }	t|	|||||||d}
t |d|
 t|
S )z>Creates encrypted file OUTPUTFILENAME.  Returns size in bytes.rbr   wb)openreadr
  writer   )inputFileNameoutputFileNamer2   r3   r9   r:   r;   r<   rD   r  	outputPDFr   r   r   encryptPdfOnDiskE  s   r  c                  C   sR  t jd d  } d}g d}d}d}d}d}d}d}d}	| d }
t| dd  }t|dkr#|d dks:|d dkr@t| d S t|d	k rJtd
|d |vrg|d }|dd  }tj|sftd| ntdd}dD ]\}}|D ]}||v r|}|	| quqod|v sd|v rd|v r|
d}d}nd|v r|
d}d}z
t||d  }W n   d}Y |	||d   |	| nddlm} dd|dfdd|dfdd|dfdd|dfdd|dfdd|dfdd|d fd!d|d fd"d#|d$fd%d#|d$fd&d'|d(fd)d'|d(fd*d+|	d,fd-d+|	d,ff}d.}|D ]|}|d |v r|
|d }|d |v r=||d  d/vr=td0|d  zI||d  |vr|d |v rYt|d d1 t  n
t|d d2 t  |rttd3|d4 ||d  f  |	||d   |	|d  W q   d5|d4  q|d6krtd7| td8| td9|	 td:| td;| td<| td=| td>| td?| td9|	 td@| |	dkr|dAd  dBks|dAd  dCkr|d dA }n|}|dD }	t||	|||||||dE	}|rtdF|	|||f  t|dkr!tdGt|ddH |f d S t| d S )INan  PDFENCRYPT USAGE:

PdfEncrypt encrypts your PDF files.

Line mode usage:

% pdfencrypt.exe pdffile [-o ownerpassword] | [owner ownerpassword],
	[-u userpassword] | [user userpassword],
	[-p 1|0] | [printable 1|0],
	[-m 1|0] | [modifiable 1|0],
	[-c 1|0] | [copypastable 1|0],
	[-a 1|0] | [annotatable 1|0],
	[-s savefilename] | [savefile savefilename],
	[-v 1|0] | [verbose 1|0],
	[-e128], [encrypt128],
	[-h] | [help]

-o or owner set the owner password.
-u or user set the user password.
-p or printable set the printable attribute (must be 1 or 0).
-m or modifiable sets the modifiable attribute (must be 1 or 0).
-c or copypastable sets the copypastable attribute (must be 1 or 0).
-a or annotatable sets the annotatable attribute (must be 1 or 0).
-s or savefile sets the name for the output PDF file
-v or verbose prints useful output to the screen.
      (this defaults to 'pdffile_encrypted.pdf').
'-e128' or 'encrypt128' allows you to use 128 bit encryption (in beta).
'-e256' or 'encrypt256' allows you to use 256 bit encryption (in beta AES).

-h or help prints this message.

See PdfEncryptIntro.pdf for more information.
)-oowner-uuser-prH   -mrI   -crJ   -arK   -ssavefile-vverbose-hhelp-e128
encrypt128-e256
encryptAESr   r   zencrypted.pdfr   r   r!  r   z2Must include a filename and one or more arguments!zCan't open input file '%s'!z2First argument must be name of the PDF input file!r,   ))r-   )r"  r#  )r"   )r$  
encrypt256r  r  )r  r  OWNERzOwner passwordr  r  USERzUser passwordr  r  	PRINTABLEz'Printable'rH   r  
MODIFIABLEz'Modifiable'rI   r  COPYPASTABLEz'Copypastable'rJ   r  ANNOTATABLEz'Annotatable'rK   r  SAVEFILEzOutput filer  )r  rH   r  rI   rJ   r  rK   r  )10z#%s value must be either '1' or '0'!z = int(argv[pos+1])z = argv[pos+1]z%s set to: '%s'.r.   zUnable to set %s.r   z
infile:z	STRENGTH:z	SAVEFILE:zUSER:zOWNER:z
PRINTABLE:zMODIFIABLE:zCOPYPASTABLE:zANNOTATABLE:zVERBOSE:z.pdfz.PDFz_encrypted.pdfr   zQwrote output file '%s'(%s bytes)
  owner password is '%s'
  user password is '%s'z4
Unrecognised arguments : %s
known arguments are:
%sr   )sysargvr   r   rd   r7   ospathisfileremoveindexri   reportlab.rl_configr  execvarsr  r   )sys_argvusageknown_modesr'  r(  r)  r*  r+  r,  r-  callerr2  infileSTRENGTHs_ar_   posargr  arglistbinaryrequiredthisargtinfilefilesizer   r   r   scriptInterpT  s   "





























$
rJ  c                   C   s
   t   d S r   )rJ  r   r   r   r   main  s   
rK  __main__c                 C   s    g | ]}|d d dkr|qS N   z--debugr   r   r   r   r   r&          r&   c                 C   s    g | ]}|d d dkr|qS rM  r   r   r   r   r   r&     rO  z--testr   )Nr   r   r   r   r,   )I__version__r1  r3  binasciir   r   hashlibr   ior   reportlab.lib.utilsr   r   r   r	   reportlab.pdfgen.canvasr
   r   r   reportlab.platypus.flowablesr   	reportlabr   r6   r   r   r   rh   rj   debugrc   	reserved1	reserved2rH   rI   rJ   rK   rL   r'   r%   	invariantr$   r#   r*   urandomr+   r   r   rk   r   joinstripsplitr   r   rq   rp   rr   r   rS   r   r   r   r   r   r
  r  rJ  rK  r   r2  r_   r   r   r   r   r   <module>   s   
 A. 
!
. 

	
*
 ,

