diff options
Diffstat (limited to 'unipdf/core/security/crypt')
| -rw-r--r-- | unipdf/core/security/crypt/crypt.go | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/unipdf/core/security/crypt/crypt.go b/unipdf/core/security/crypt/crypt.go new file mode 100644 index 0000000..cf341ef --- /dev/null +++ b/unipdf/core/security/crypt/crypt.go @@ -0,0 +1,135 @@ +// +// 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/ + +package crypt ;import (_e "crypto/aes";_ef "crypto/cipher";_db "crypto/md5";_ab "crypto/rand";_d "crypto/rc4";_f "fmt";_dc "github.com/unidoc/unipdf/v4/common";_da "github.com/unidoc/unipdf/v4/core/security";_c "io";);func init (){_dba ("\u0041\u0045\u0053V\u0032",_cd )}; + + +// Name implements Filter interface. +func (filterAESV2 )Name ()string {return "\u0041\u0045\u0053V\u0032"}; + +// PDFVersion implements Filter interface. +func (_abc filterV2 )PDFVersion ()[2]int {return [2]int {}};func (filterIdentity )DecryptBytes (p []byte ,okey []byte )([]byte ,error ){return p ,nil };func (filterAES )EncryptBytes (buf []byte ,okey []byte )([]byte ,error ){_cg ,_eab :=_e .NewCipher (okey ); +if _eab !=nil {return nil ,_eab ;};_dc .Log .Trace ("A\u0045\u0053\u0020\u0045nc\u0072y\u0070\u0074\u0020\u0028\u0025d\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );const _af =_e .BlockSize ;_bg :=_af -len (buf )%_af ;for _g :=0;_g < _bg ;_g ++{buf =append (buf ,byte (_bg )); +};_dc .Log .Trace ("\u0050a\u0064d\u0065\u0064\u0020\u0074\u006f \u0025\u0064 \u0062\u0079\u0074\u0065\u0073",len (buf ));_ac :=make ([]byte ,_af +len (buf ));_ed :=_ac [:_af ];if _ ,_gb :=_c .ReadFull (_ab .Reader ,_ed );_gb !=nil {return nil ,_gb ;}; +_cbe :=_ef .NewCBCEncrypter (_cg ,_ed );_cbe .CryptBlocks (_ac [_af :],buf );buf =_ac ;_dc .Log .Trace ("\u0074\u006f\u0020(\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );return buf ,nil ;}; + +// KeyLength implements Filter interface. +func (filterAESV2 )KeyLength ()int {return 128/8}; + +// NewFilterAESV2 creates an AES-based filter with a 128 bit key (AESV2). +func NewFilterAESV2 ()Filter {_cb ,_abb :=_cd (FilterDict {});if _abb !=nil {_dc .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",_abb ); +return filterAESV2 {};};return _cb ;};func (filterAES )DecryptBytes (buf []byte ,okey []byte )([]byte ,error ){_ga ,_acc :=_e .NewCipher (okey );if _acc !=nil {return nil ,_acc ;};if len (buf )< 16{_dc .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 ,_f .Errorf ("\u0041\u0045\u0053\u003a B\u0075\u0066\u0020\u006c\u0065\u006e\u0020\u003c\u0020\u0031\u0036\u0020\u0028\u0025d\u0029",len (buf ));};_gc :=buf [:16];buf =buf [16:];if len (buf )%16!=0{_dc .Log .Debug ("\u0020\u0069\u0076\u0020\u0028\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (_gc ),_gc ); +_dc .Log .Debug ("\u0062\u0075\u0066\u0020\u0028\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );return buf ,_f .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 )); +};_cc :=_ef .NewCBCDecrypter (_ga ,_gc );_dc .Log .Trace ("A\u0045\u0053\u0020\u0044ec\u0072y\u0070\u0074\u0020\u0028\u0025d\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );_dc .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 ); +_cc .CryptBlocks (buf ,buf );_dc .Log .Trace ("\u0074\u006f\u0020(\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );if len (buf )==0{_dc .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 ;};_edb :=int (buf [len (buf )-1]);if _edb > len (buf ){_dc .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",_edb ,len (buf )); +return buf ,_f .Errorf ("\u0069n\u0076a\u006c\u0069\u0064\u0020\u0070a\u0064\u0020l\u0065\u006e\u0067\u0074\u0068");};buf =buf [:len (buf )-_edb ];return buf ,nil ;};func (filterIdentity )MakeKey (objNum ,genNum uint32 ,fkey []byte )([]byte ,error ){return fkey ,nil }; + + +// KeyLength implements Filter interface. +func (_abae filterV2 )KeyLength ()int {return _abae ._eef };func init (){_dba ("\u0041\u0045\u0053V\u0033",_eag )}; + +// HandlerVersion implements Filter interface. +func (filterAESV3 )HandlerVersion ()(V ,R int ){V ,R =5,6;return ;};var _ Filter =filterAESV3 {}; + +// MakeKey implements Filter interface. +func (filterAESV3 )MakeKey (_ ,_ uint32 ,ekey []byte )([]byte ,error ){return ekey ,nil }; + +// MakeKey implements Filter interface. +func (_cag filterV2 )MakeKey (objNum ,genNum uint32 ,ekey []byte )([]byte ,error ){return _bf (objNum ,genNum ,ekey ,false );}; + +// Name implements Filter interface. +func (filterAESV3 )Name ()string {return "\u0041\u0045\u0053V\u0033"};func (filterIdentity )PDFVersion ()[2]int {return [2]int {}}; + +// HandlerVersion implements Filter interface. +func (filterAESV2 )HandlerVersion ()(V ,R int ){V ,R =4,4;return ;}; + +// HandlerVersion implements Filter interface. +func (_abbb filterV2 )HandlerVersion ()(V ,R int ){V ,R =2,3;return ;};func _cd (_ca FilterDict )(Filter ,error ){if _ca .Length ==128{_dc .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",_ca .Length ); +_ca .Length /=8;};if _ca .Length !=0&&_ca .Length !=16{return nil ,_f .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",_ca .Length ); +};return filterAESV2 {},nil ;};type filterAES struct{};func (filterIdentity )HandlerVersion ()(V ,R int ){return ;}; + +// NewFilterV2 creates a RC4-based filter with a specified key length (in bytes). +func NewFilterV2 (length int )Filter {_aba ,_ee :=_cga (FilterDict {Length :length });if _ee !=nil {_dc .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",_ee ); +return filterV2 {_eef :length };};return _aba ;};func init (){_dba ("\u0056\u0032",_cga )}; + +// EncryptBytes implements Filter interface. +func (filterV2 )EncryptBytes (buf []byte ,okey []byte )([]byte ,error ){_ad ,_cgf :=_d .NewCipher (okey );if _cgf !=nil {return nil ,_cgf ;};_dc .Log .Trace ("\u0052\u00434\u0020\u0045\u006ec\u0072\u0079\u0070\u0074\u003a\u0020\u0025\u0020\u0078",buf ); +_ad .XORKeyStream (buf ,buf );_dc .Log .Trace ("\u0074o\u003a\u0020\u0025\u0020\u0078",buf );return buf ,nil ;}; + +// Name implements Filter interface. +func (filterV2 )Name ()string {return "\u0056\u0032"};type filterIdentity struct{};func (filterIdentity )Name ()string {return "\u0049\u0064\u0065\u006e\u0074\u0069\u0074\u0079"}; + +// MakeKey implements Filter interface. +func (filterAESV2 )MakeKey (objNum ,genNum uint32 ,ekey []byte )([]byte ,error ){return _bf (objNum ,genNum ,ekey ,true );}; + +// 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. +MakeKey (_bb ,_ce uint32 ,_ff []byte )([]byte ,error ); + +// EncryptBytes encrypts a buffer using object encryption key, as returned by MakeKey. +// Implementation may reuse a buffer and encrypt data in-place. +EncryptBytes (_cbd []byte ,_fcd []byte )([]byte ,error ); + +// DecryptBytes decrypts a buffer using object encryption key, as returned by MakeKey. +// Implementation may reuse a buffer and decrypt data in-place. +DecryptBytes (_cda []byte ,_abf []byte )([]byte ,error );}; + +// PDFVersion implements Filter interface. +func (filterAESV2 )PDFVersion ()[2]int {return [2]int {1,5}}; + +// KeyLength implements Filter interface. +func (filterAESV3 )KeyLength ()int {return 256/8};func _gaf (_fgd string )(filterFunc ,error ){_fgf :=_ege [_fgd ];if _fgf ==nil {return nil ,_f .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",_fgd ); +};return _fgf ,nil ;};type filterAESV3 struct{filterAES };func (filterIdentity )EncryptBytes (p []byte ,okey []byte )([]byte ,error ){return p ,nil };type filterAESV2 struct{filterAES };func _bf (_bd ,_fb uint32 ,_dd []byte ,_ec bool )([]byte ,error ){_dde :=make ([]byte ,len (_dd )+5); +copy (_dde ,_dd );for _gf :=0;_gf < 3;_gf ++{_fc :=byte ((_bd >>uint32 (8*_gf ))&0xff);_dde [_gf +len (_dd )]=_fc ;};for _fd :=0;_fd < 2;_fd ++{_fgg :=byte ((_fb >>uint32 (8*_fd ))&0xff);_dde [_fd +len (_dd )+3]=_fgg ;};if _ec {_dde =append (_dde ,0x73); +_dde =append (_dde ,0x41);_dde =append (_dde ,0x6C);_dde =append (_dde ,0x54);};_gd :=_db .New ();_gd .Write (_dde );_dca :=_gd .Sum (nil );if len (_dd )+5< 16{return _dca [0:len (_dd )+5],nil ;};return _dca ,nil ;};var _ Filter =filterAESV2 {};type filterFunc func (_ge FilterDict )(Filter ,error ); +func _dba (_fgga string ,_abd filterFunc ){if _ ,_gee :=_ege [_fgga ];_gee {panic ("\u0061l\u0072e\u0061\u0064\u0079\u0020\u0072e\u0067\u0069s\u0074\u0065\u0072\u0065\u0064");};_ege [_fgga ]=_abd ;};var _ Filter =filterV2 {}; + +// PDFVersion implements Filter interface. +func (filterAESV3 )PDFVersion ()[2]int {return [2]int {2,0}};var (_ege =make (map[string ]filterFunc );); + +// DecryptBytes implements Filter interface. +func (filterV2 )DecryptBytes (buf []byte ,okey []byte )([]byte ,error ){_cf ,_cdg :=_d .NewCipher (okey );if _cdg !=nil {return nil ,_cdg ;};_dc .Log .Trace ("\u0052\u00434\u0020\u0044\u0065c\u0072\u0079\u0070\u0074\u003a\u0020\u0025\u0020\u0078",buf ); +_cf .XORKeyStream (buf ,buf );_dc .Log .Trace ("\u0074o\u003a\u0020\u0025\u0020\u0078",buf );return buf ,nil ;};type filterV2 struct{_eef int }; + +// NewFilterAESV3 creates an AES-based filter with a 256 bit key (AESV3). +func NewFilterAESV3 ()Filter {_b ,_ea :=_eag (FilterDict {});if _ea !=nil {_dc .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",_ea ); +return filterAESV3 {};};return _b ;};func _cga (_aca FilterDict )(Filter ,error ){if _aca .Length %8!=0{return nil ,_f .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",_aca .Length ); +};if _aca .Length < 5||_aca .Length > 16{if _aca .Length ==40||_aca .Length ==64||_aca .Length ==128{_dc .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",_aca .Length ); +_aca .Length /=8;}else {return nil ,_f .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",_aca .Length ); +};};return filterV2 {_eef :_aca .Length },nil ;}; + +// NewFilter creates CryptFilter from a corresponding dictionary. +func NewFilter (d FilterDict )(Filter ,error ){_dg ,_bgb :=_gaf (d .CFM );if _bgb !=nil {return nil ,_bgb ;};_aad ,_bgb :=_dg (d );if _bgb !=nil {return nil ,_bgb ;};return _aad ,nil ;}; + +// NewIdentity creates an identity filter that bypasses all data without changes. +func NewIdentity ()Filter {return filterIdentity {}};func _eag (_eaa FilterDict )(Filter ,error ){if _eaa .Length ==256{_dc .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",_eaa .Length ); +_eaa .Length /=8;};if _eaa .Length !=0&&_eaa .Length !=32{return nil ,_f .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",_eaa .Length ); +};return filterAESV3 {},nil ;}; + +// FilterDict represents information from a CryptFilter dictionary. +type FilterDict struct{CFM string ;AuthEvent _da .AuthEvent ;Length int ;};func (filterIdentity )KeyLength ()int {return 0};
\ No newline at end of file |
