136 lines
14 KiB
Go
Raw Normal View History

2020-08-27 21:45:09 +00:00
//
// Copyright 2020 FoxyUtils ehf. All rights reserved.
//
// This is a commercial product and requires a license to operate.
// A trial license can be obtained at https://unidoc.io
//
// DO NOT EDIT: generated by unitwist Go source code obfuscator.
//
// Use of this source code is governed by the UniDoc End User License Agreement
// terms that can be accessed at https://unidoc.io/eula/
2021-10-22 10:53:20 +00:00
package crypt ;import (_e "crypto/aes";_ae "crypto/cipher";_ec "crypto/md5";_d "crypto/rand";_f "crypto/rc4";_a "fmt";_fd "github.com/unidoc/unipdf/v3/common";_fc "github.com/unidoc/unipdf/v3/core/security";_c "io";);func init (){_fa ("\u0041\u0045\u0053V\u0032",_ad )};
func (filterIdentity )PDFVersion ()[2]int {return [2]int {}};
2021-07-30 00:21:16 +00:00
2021-09-23 22:37:42 +00:00
// PDFVersion implements Filter interface.
2021-10-22 10:53:20 +00:00
func (filterAESV3 )PDFVersion ()[2]int {return [2]int {2,0}};
2021-08-13 01:33:42 +00:00
2021-10-22 10:53:20 +00:00
// MakeKey implements Filter interface.
func (filterAESV2 )MakeKey (objNum ,genNum uint32 ,ekey []byte )([]byte ,error ){return _dce (objNum ,genNum ,ekey ,true );};func _gbb (_cgf FilterDict )(Filter ,error ){if _cgf .Length %8!=0{return nil ,_a .Errorf ("\u0063\u0072\u0079p\u0074\u0020\u0066\u0069\u006c\u0074\u0065\u0072\u0020\u006c\u0065\u006e\u0067\u0074\u0068\u0020\u006e\u006f\u0074\u0020\u006d\u0075\u006c\u0074\u0069\u0070\u006c\u0065\u0020o\u0066\u0020\u0038\u0020\u0028\u0025\u0064\u0029",_cgf .Length );
};if _cgf .Length < 5||_cgf .Length > 16{if _cgf .Length ==40||_cgf .Length ==64||_cgf .Length ==128{_fd .Log .Debug ("\u0053\u0054\u0041\u004e\u0044AR\u0044\u0020V\u0049\u004f\u004c\u0041\u0054\u0049\u004f\u004e\u003a\u0020\u0043\u0072\u0079\u0070\u0074\u0020\u004c\u0065\u006e\u0067\u0074\u0068\u0020\u0061\u0070\u0070\u0065\u0061\u0072s\u0020\u0074\u006f \u0062\u0065\u0020\u0069\u006e\u0020\u0062\u0069\u0074\u0073\u0020\u0072\u0061t\u0068\u0065\u0072\u0020\u0074h\u0061\u006e\u0020\u0062\u0079\u0074\u0065\u0073\u0020-\u0020\u0061s\u0073u\u006d\u0069\u006e\u0067\u0020\u0062\u0069t\u0073\u0020\u0028\u0025\u0064\u0029",_cgf .Length );
_cgf .Length /=8;}else {return nil ,_a .Errorf ("\u0063\u0072\u0079\u0070\u0074\u0020\u0066\u0069\u006c\u0074\u0065\u0072\u0020\u006c\u0065\u006e\u0067\u0074h\u0020\u006e\u006f\u0074\u0020\u0069\u006e \u0072\u0061\u006e\u0067\u0065\u0020\u0034\u0030\u0020\u002d\u00201\u0032\u0038\u0020\u0062\u0069\u0074\u0020\u0028\u0025\u0064\u0029",_cgf .Length );
};};return filterV2 {_gec :_cgf .Length },nil ;};
2021-06-21 14:01:56 +00:00
// KeyLength implements Filter interface.
2021-10-22 10:53:20 +00:00
func (filterAESV2 )KeyLength ()int {return 128/8};func (filterIdentity )MakeKey (objNum ,genNum uint32 ,fkey []byte )([]byte ,error ){return fkey ,nil };func (filterAES )DecryptBytes (buf []byte ,okey []byte )([]byte ,error ){_aea ,_db :=_e .NewCipher (okey );
if _db !=nil {return nil ,_db ;};if len (buf )< 16{_fd .Log .Debug ("\u0045R\u0052\u004f\u0052\u0020\u0041\u0045\u0053\u0020\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0062\u0075\u0066\u0020\u0025\u0073",buf );return buf ,_a .Errorf ("\u0041\u0045\u0053\u003a B\u0075\u0066\u0020\u006c\u0065\u006e\u0020\u003c\u0020\u0031\u0036\u0020\u0028\u0025d\u0029",len (buf ));
};_cb :=buf [:16];buf =buf [16:];if len (buf )%16!=0{_fd .Log .Debug ("\u0020\u0069\u0076\u0020\u0028\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (_cb ),_cb );_fd .Log .Debug ("\u0062\u0075\u0066\u0020\u0028\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );
return buf ,_a .Errorf ("\u0041\u0045\u0053\u0020\u0062\u0075\u0066\u0020\u006c\u0065\u006e\u0067\u0074\u0068\u0020\u006e\u006f\u0074\u0020\u006d\u0075\u006c\u0074\u0069p\u006c\u0065\u0020\u006f\u0066 \u0031\u0036 \u0028\u0025\u0064\u0029",len (buf ));
};_bd :=_ae .NewCBCDecrypter (_aea ,_cb );_fd .Log .Trace ("A\u0045\u0053\u0020\u0044ec\u0072y\u0070\u0074\u0020\u0028\u0025d\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );_fd .Log .Trace ("\u0063\u0068\u006f\u0070\u0020\u0041\u0045\u0053\u0020\u0044\u0065c\u0072\u0079\u0070\u0074\u0020\u0028\u0025\u0064\u0029\u003a \u0025\u0020\u0078",len (buf ),buf );
_bd .CryptBlocks (buf ,buf );_fd .Log .Trace ("\u0074\u006f\u0020(\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );if len (buf )==0{_fd .Log .Trace ("\u0045\u006d\u0070\u0074\u0079\u0020b\u0075\u0066\u002c\u0020\u0072\u0065\u0074\u0075\u0072\u006e\u0069\u006e\u0067 \u0065\u006d\u0070\u0074\u0079\u0020\u0073t\u0072\u0069\u006e\u0067");
return buf ,nil ;};_ge :=int (buf [len (buf )-1]);if _ge > len (buf ){_fd .Log .Debug ("\u0049\u006c\u006c\u0065g\u0061\u006c\u0020\u0070\u0061\u0064\u0020\u006c\u0065\u006eg\u0074h\u0020\u0028\u0025\u0064\u0020\u003e\u0020%\u0064\u0029",_ge ,len (buf ));
return buf ,_a .Errorf ("\u0069n\u0076a\u006c\u0069\u0064\u0020\u0070a\u0064\u0020l\u0065\u006e\u0067\u0074\u0068");};buf =buf [:len (buf )-_ge ];return buf ,nil ;};var _ Filter =filterAESV3 {};var _ Filter =filterAESV2 {};func init (){_fa ("\u0041\u0045\u0053V\u0033",_acc )};
2021-06-21 14:01:56 +00:00
2021-10-22 10:53:20 +00:00
// HandlerVersion implements Filter interface.
func (_ecb filterV2 )HandlerVersion ()(V ,R int ){V ,R =2,3;return ;};
2021-05-11 00:01:27 +00:00
2021-08-13 01:33:42 +00:00
// PDFVersion implements Filter interface.
2021-10-22 10:53:20 +00:00
func (_ca filterV2 )PDFVersion ()[2]int {return [2]int {}};
2021-04-23 20:28:14 +00:00
2021-10-22 10:53:20 +00:00
// Name implements Filter interface.
func (filterAESV2 )Name ()string {return "\u0041\u0045\u0053V\u0032"};var _ Filter =filterV2 {};
2021-08-13 01:33:42 +00:00
2021-10-22 10:53:20 +00:00
// MakeKey implements Filter interface.
func (_ebb filterV2 )MakeKey (objNum ,genNum uint32 ,ekey []byte )([]byte ,error ){return _dce (objNum ,genNum ,ekey ,false );};
2020-12-06 13:03:03 +00:00
2021-07-30 00:21:16 +00:00
// HandlerVersion implements Filter interface.
2021-10-22 10:53:20 +00:00
func (filterAESV3 )HandlerVersion ()(V ,R int ){V ,R =5,6;return ;};func _ad (_de FilterDict )(Filter ,error ){if _de .Length ==128{_fd .Log .Debug ("\u0041\u0045S\u0056\u0032\u0020c\u0072\u0079\u0070\u0074\u0020f\u0069\u006c\u0074\u0065\u0072 l\u0065\u006e\u0067\u0074\u0068\u0020\u0061\u0070\u0070\u0065\u0061\u0072\u0073\u0020\u0074\u006f\u0020\u0062e\u0020i\u006e\u0020\u0062\u0069\u0074\u0073 ra\u0074\u0068\u0065\u0072\u0020\u0074\u0068\u0061\u006e\u0020\u0062\u0079te\u0073 \u002d\u0020\u0061\u0073s\u0075m\u0069n\u0067\u0020b\u0069\u0074s \u0028\u0025\u0064\u0029",_de .Length );
_de .Length /=8;};if _de .Length !=0&&_de .Length !=16{return nil ,_a .Errorf ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0041\u0045\u0053\u0056\u0032\u0020\u0063\u0072\u0079\u0070\u0074\u0020\u0066\u0069\u006c\u0074e\u0072\u0020\u006c\u0065\u006eg\u0074\u0068 \u0028\u0025\u0064\u0029",_de .Length );
};return filterAESV2 {},nil ;};type filterV2 struct{_gec int };func (filterAES )EncryptBytes (buf []byte ,okey []byte )([]byte ,error ){_eb ,_ea :=_e .NewCipher (okey );if _ea !=nil {return nil ,_ea ;};_fd .Log .Trace ("A\u0045\u0053\u0020\u0045nc\u0072y\u0070\u0074\u0020\u0028\u0025d\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );
const _fdc =_e .BlockSize ;_ff :=_fdc -len (buf )%_fdc ;for _adb :=0;_adb < _ff ;_adb ++{buf =append (buf ,byte (_ff ));};_fd .Log .Trace ("\u0050a\u0064d\u0065\u0064\u0020\u0074\u006f \u0025\u0064 \u0062\u0079\u0074\u0065\u0073",len (buf ));_cf :=make ([]byte ,_fdc +len (buf ));
_ee :=_cf [:_fdc ];if _ ,_cfd :=_c .ReadFull (_d .Reader ,_ee );_cfd !=nil {return nil ,_cfd ;};_ce :=_ae .NewCBCEncrypter (_eb ,_ee );_ce .CryptBlocks (_cf [_fdc :],buf );buf =_cf ;_fd .Log .Trace ("\u0074\u006f\u0020(\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );
return buf ,nil ;};type filterAES struct{};func _acc (_fe FilterDict )(Filter ,error ){if _fe .Length ==256{_fd .Log .Debug ("\u0041\u0045S\u0056\u0033\u0020c\u0072\u0079\u0070\u0074\u0020f\u0069\u006c\u0074\u0065\u0072 l\u0065\u006e\u0067\u0074\u0068\u0020\u0061\u0070\u0070\u0065\u0061\u0072\u0073\u0020\u0074\u006f\u0020\u0062e\u0020i\u006e\u0020\u0062\u0069\u0074\u0073 ra\u0074\u0068\u0065\u0072\u0020\u0074\u0068\u0061\u006e\u0020\u0062\u0079te\u0073 \u002d\u0020\u0061\u0073s\u0075m\u0069n\u0067\u0020b\u0069\u0074s \u0028\u0025\u0064\u0029",_fe .Length );
_fe .Length /=8;};if _fe .Length !=0&&_fe .Length !=32{return nil ,_a .Errorf ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0041\u0045\u0053\u0056\u0033\u0020\u0063\u0072\u0079\u0070\u0074\u0020\u0066\u0069\u006c\u0074e\u0072\u0020\u006c\u0065\u006eg\u0074\u0068 \u0028\u0025\u0064\u0029",_fe .Length );
};return filterAESV3 {},nil ;};func (filterIdentity )DecryptBytes (p []byte ,okey []byte )([]byte ,error ){return p ,nil };func init (){_fa ("\u0056\u0032",_gbb )};
2021-04-23 20:28:14 +00:00
2021-09-23 22:37:42 +00:00
// NewFilterAESV2 creates an AES-based filter with a 128 bit key (AESV2).
2021-10-22 10:53:20 +00:00
func NewFilterAESV2 ()Filter {_ac ,_cg :=_ad (FilterDict {});if _cg !=nil {_fd .Log .Error ("E\u0052\u0052\u004f\u0052\u003a\u0020\u0063\u006f\u0075l\u0064\u0020\u006e\u006f\u0074\u0020\u0063re\u0061\u0074\u0065\u0020A\u0045\u0053\u0020\u0056\u0032\u0020\u0063\u0072\u0079pt\u0020\u0066i\u006c\u0074\u0065\u0072\u003a\u0020\u0025\u0076",_cg );
return filterAESV2 {};};return _ac ;};
2021-04-23 20:28:14 +00:00
2021-08-13 01:33:42 +00:00
// KeyLength implements Filter interface.
2021-10-22 10:53:20 +00:00
func (_bgg filterV2 )KeyLength ()int {return _bgg ._gec };
2021-07-30 00:21:16 +00:00
2021-10-22 10:53:20 +00:00
// NewFilter creates CryptFilter from a corresponding dictionary.
func NewFilter (d FilterDict )(Filter ,error ){_fbb ,_ggf :=_aec (d .CFM );if _ggf !=nil {return nil ,_ggf ;};_dea ,_ggf :=_fbb (d );if _ggf !=nil {return nil ,_ggf ;};return _dea ,nil ;};
// EncryptBytes implements Filter interface.
func (filterV2 )EncryptBytes (buf []byte ,okey []byte )([]byte ,error ){_eeb ,_aef :=_f .NewCipher (okey );if _aef !=nil {return nil ,_aef ;};_fd .Log .Trace ("\u0052\u00434\u0020\u0045\u006ec\u0072\u0079\u0070\u0074\u003a\u0020\u0025\u0020\u0078",buf );
_eeb .XORKeyStream (buf ,buf );_fd .Log .Trace ("\u0074o\u003a\u0020\u0025\u0020\u0078",buf );return buf ,nil ;};type filterAESV2 struct{filterAES };
2021-07-30 00:21:16 +00:00
2021-08-13 01:33:42 +00:00
// Name implements Filter interface.
2021-10-22 10:53:20 +00:00
func (filterV2 )Name ()string {return "\u0056\u0032"};type filterFunc func (_bbe FilterDict )(Filter ,error );
// HandlerVersion implements Filter interface.
func (filterAESV2 )HandlerVersion ()(V ,R int ){V ,R =4,4;return ;};func _aec (_acg string )(filterFunc ,error ){_fba :=_fgb [_acg ];if _fba ==nil {return nil ,_a .Errorf ("\u0075\u006e\u0073\u0075p\u0070\u006f\u0072\u0074\u0065\u0064\u0020\u0063\u0072\u0079p\u0074 \u0066\u0069\u006c\u0074\u0065\u0072\u003a \u0025\u0071",_acg );
};return _fba ,nil ;};
2021-08-13 01:33:42 +00:00
// MakeKey implements Filter interface.
2021-10-22 10:53:20 +00:00
func (filterAESV3 )MakeKey (_ ,_ uint32 ,ekey []byte )([]byte ,error ){return ekey ,nil };
// NewFilterAESV3 creates an AES-based filter with a 256 bit key (AESV3).
func NewFilterAESV3 ()Filter {_gb ,_dc :=_acc (FilterDict {});if _dc !=nil {_fd .Log .Error ("E\u0052\u0052\u004f\u0052\u003a\u0020\u0063\u006f\u0075l\u0064\u0020\u006e\u006f\u0074\u0020\u0063re\u0061\u0074\u0065\u0020A\u0045\u0053\u0020\u0056\u0033\u0020\u0063\u0072\u0079pt\u0020\u0066i\u006c\u0074\u0065\u0072\u003a\u0020\u0025\u0076",_dc );
return filterAESV3 {};};return _gb ;};
// FilterDict represents information from a CryptFilter dictionary.
type FilterDict struct{CFM string ;AuthEvent _fc .AuthEvent ;Length int ;};func _dce (_bdc ,_fg uint32 ,_cga []byte ,_acd bool )([]byte ,error ){_bg :=make ([]byte ,len (_cga )+5);for _dd :=0;_dd < len (_cga );_dd ++{_bg [_dd ]=_cga [_dd ];};for _gg :=0;
_gg < 3;_gg ++{_gaf :=byte ((_bdc >>uint32 (8*_gg ))&0xff);_bg [_gg +len (_cga )]=_gaf ;};for _aa :=0;_aa < 2;_aa ++{_ecc :=byte ((_fg >>uint32 (8*_aa ))&0xff);_bg [_aa +len (_cga )+3]=_ecc ;};if _acd {_bg =append (_bg ,0x73);_bg =append (_bg ,0x41);_bg =append (_bg ,0x6C);
_bg =append (_bg ,0x54);};_cgd :=_ec .New ();_cgd .Write (_bg );_ab :=_cgd .Sum (nil );if len (_cga )+5< 16{return _ab [0:len (_cga )+5],nil ;};return _ab ,nil ;};
// NewFilterV2 creates a RC4-based filter with a specified key length (in bytes).
func NewFilterV2 (length int )Filter {_bbg ,_ed :=_gbb (FilterDict {Length :length });if _ed !=nil {_fd .Log .Error ("E\u0052\u0052\u004f\u0052\u003a\u0020\u0063\u006f\u0075l\u0064\u0020\u006e\u006f\u0074\u0020\u0063re\u0061\u0074\u0065\u0020R\u0043\u0034\u0020\u0056\u0032\u0020\u0063\u0072\u0079pt\u0020\u0066i\u006c\u0074\u0065\u0072\u003a\u0020\u0025\u0076",_ed );
return filterV2 {_gec :length };};return _bbg ;};
2020-12-06 13:03:03 +00:00
2021-09-23 22:37:42 +00:00
// NewIdentity creates an identity filter that bypasses all data without changes.
2021-10-22 10:53:20 +00:00
func NewIdentity ()Filter {return filterIdentity {}};
// Name implements Filter interface.
func (filterAESV3 )Name ()string {return "\u0041\u0045\u0053V\u0033"};
2020-12-06 13:03:03 +00:00
2020-08-27 21:45:09 +00:00
// Filter is a common interface for crypt filter methods.
type Filter interface{
// Name returns a name of the filter that should be used in CFM field of Encrypt dictionary.
Name ()string ;
// KeyLength returns a length of the encryption key in bytes.
KeyLength ()int ;
// PDFVersion reports the minimal version of PDF document that introduced this filter.
PDFVersion ()[2]int ;
// HandlerVersion reports V and R parameters that should be used for this filter.
HandlerVersion ()(V ,R int );
// MakeKey generates a object encryption key based on file encryption key and object numbers.
// Used only for legacy filters - AESV3 doesn't change the key for each object.
2021-10-22 10:53:20 +00:00
MakeKey (_cef ,_dbd uint32 ,_df []byte )([]byte ,error );
2020-08-27 21:45:09 +00:00
// EncryptBytes encrypts a buffer using object encryption key, as returned by MakeKey.
// Implementation may reuse a buffer and encrypt data in-place.
2021-10-22 10:53:20 +00:00
EncryptBytes (_fcd []byte ,_cc []byte )([]byte ,error );
2020-08-27 21:45:09 +00:00
// DecryptBytes decrypts a buffer using object encryption key, as returned by MakeKey.
// Implementation may reuse a buffer and decrypt data in-place.
2021-10-22 10:53:20 +00:00
DecryptBytes (_aae []byte ,_bc []byte )([]byte ,error );};func _fa (_af string ,_add filterFunc ){if _ ,_bf :=_fgb [_af ];_bf {panic ("\u0061l\u0072e\u0061\u0064\u0079\u0020\u0072e\u0067\u0069s\u0074\u0065\u0072\u0065\u0064");};_fgb [_af ]=_add ;};
2021-09-23 22:37:42 +00:00
2021-10-22 10:53:20 +00:00
// PDFVersion implements Filter interface.
func (filterAESV2 )PDFVersion ()[2]int {return [2]int {1,5}};func (filterIdentity )KeyLength ()int {return 0};func (filterIdentity )HandlerVersion ()(V ,R int ){return ;};type filterAESV3 struct{filterAES };var (_fgb =make (map[string ]filterFunc ););
2021-02-22 02:29:48 +00:00
2021-10-22 10:53:20 +00:00
// DecryptBytes implements Filter interface.
func (filterV2 )DecryptBytes (buf []byte ,okey []byte )([]byte ,error ){_gd ,_fff :=_f .NewCipher (okey );if _fff !=nil {return nil ,_fff ;};_fd .Log .Trace ("\u0052\u00434\u0020\u0044\u0065c\u0072\u0079\u0070\u0074\u003a\u0020\u0025\u0020\u0078",buf );
_gd .XORKeyStream (buf ,buf );_fd .Log .Trace ("\u0074o\u003a\u0020\u0025\u0020\u0078",buf );return buf ,nil ;};func (filterIdentity )EncryptBytes (p []byte ,okey []byte )([]byte ,error ){return p ,nil };func (filterIdentity )Name ()string {return "\u0049\u0064\u0065\u006e\u0074\u0069\u0074\u0079"};
type filterIdentity struct{};
2021-09-23 22:37:42 +00:00
2021-10-22 10:53:20 +00:00
// KeyLength implements Filter interface.
func (filterAESV3 )KeyLength ()int {return 256/8};