mirror of
https://github.com/unidoc/unipdf.git
synced 2025-05-05 19:30:30 +08:00
135 lines
14 KiB
Go
135 lines
14 KiB
Go
//
|
|
// 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 (_c "crypto/aes";_ed "crypto/cipher";_e "crypto/md5";_b "crypto/rand";_f "crypto/rc4";_ac "fmt";_cc "github.com/unidoc/unipdf/v3/common";_gb "github.com/unidoc/unipdf/v3/core/security";_g "io";);func init (){_ecg ("\u0041\u0045\u0053V\u0032",_bg )};
|
|
func (filterIdentity )DecryptBytes (p []byte ,okey []byte )([]byte ,error ){return p ,nil };
|
|
|
|
// FilterDict represents information from a CryptFilter dictionary.
|
|
type FilterDict struct{CFM string ;AuthEvent _gb .AuthEvent ;Length int ;};
|
|
|
|
// DecryptBytes implements Filter interface.
|
|
func (filterV2 )DecryptBytes (buf []byte ,okey []byte )([]byte ,error ){_ada ,_bfd :=_f .NewCipher (okey );if _bfd !=nil {return nil ,_bfd ;};_cc .Log .Trace ("\u0052\u00434\u0020\u0044\u0065c\u0072\u0079\u0070\u0074\u003a\u0020\u0025\u0020\u0078",buf );
|
|
_ada .XORKeyStream (buf ,buf );_cc .Log .Trace ("\u0074o\u003a\u0020\u0025\u0020\u0078",buf );return buf ,nil ;};
|
|
|
|
// MakeKey implements Filter interface.
|
|
func (filterAESV2 )MakeKey (objNum ,genNum uint32 ,ekey []byte )([]byte ,error ){return _aa (objNum ,genNum ,ekey ,true );};type filterV2 struct{_ega int };type filterFunc func (_gcc FilterDict )(Filter ,error );func (filterAES )DecryptBytes (buf []byte ,okey []byte )([]byte ,error ){_ge ,_ec :=_c .NewCipher (okey );
|
|
if _ec !=nil {return nil ,_ec ;};if len (buf )< 16{_cc .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 ,_ac .Errorf ("\u0041\u0045\u0053\u003a B\u0075\u0066\u0020\u006c\u0065\u006e\u0020\u003c\u0020\u0031\u0036\u0020\u0028\u0025d\u0029",len (buf ));
|
|
};_ba :=buf [:16];buf =buf [16:];if len (buf )%16!=0{_cc .Log .Debug ("\u0020\u0069\u0076\u0020\u0028\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (_ba ),_ba );_cc .Log .Debug ("\u0062\u0075\u0066\u0020\u0028\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );
|
|
return buf ,_ac .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 ));
|
|
};_cag :=_ed .NewCBCDecrypter (_ge ,_ba );_cc .Log .Trace ("A\u0045\u0053\u0020\u0044ec\u0072y\u0070\u0074\u0020\u0028\u0025d\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );_cc .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 );
|
|
_cag .CryptBlocks (buf ,buf );_cc .Log .Trace ("\u0074\u006f\u0020(\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );if len (buf )==0{_cc .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 ;};_d :=int (buf [len (buf )-1]);if _d > len (buf ){_cc .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",_d ,len (buf ));
|
|
return buf ,_ac .Errorf ("\u0069n\u0076a\u006c\u0069\u0064\u0020\u0070a\u0064\u0020l\u0065\u006e\u0067\u0074\u0068");};buf =buf [:len (buf )-_d ];return buf ,nil ;};var _ Filter =filterAESV2 {};
|
|
|
|
// PDFVersion implements Filter interface.
|
|
func (filterAESV3 )PDFVersion ()[2]int {return [2]int {2,0}};func init (){_ecg ("\u0041\u0045\u0053V\u0033",_ef )};
|
|
|
|
// Name implements Filter interface.
|
|
func (filterV2 )Name ()string {return "\u0056\u0032"};func (filterIdentity )PDFVersion ()[2]int {return [2]int {}};
|
|
|
|
// EncryptBytes implements Filter interface.
|
|
func (filterV2 )EncryptBytes (buf []byte ,okey []byte )([]byte ,error ){_efg ,_fb :=_f .NewCipher (okey );if _fb !=nil {return nil ,_fb ;};_cc .Log .Trace ("\u0052\u00434\u0020\u0045\u006ec\u0072\u0079\u0070\u0074\u003a\u0020\u0025\u0020\u0078",buf );_efg .XORKeyStream (buf ,buf );
|
|
_cc .Log .Trace ("\u0074o\u003a\u0020\u0025\u0020\u0078",buf );return buf ,nil ;};
|
|
|
|
// PDFVersion implements Filter interface.
|
|
func (_cbc filterV2 )PDFVersion ()[2]int {return [2]int {}};type filterIdentity struct{};
|
|
|
|
// MakeKey implements Filter interface.
|
|
func (_da filterV2 )MakeKey (objNum ,genNum uint32 ,ekey []byte )([]byte ,error ){return _aa (objNum ,genNum ,ekey ,false );};
|
|
|
|
// NewFilterAESV3 creates an AES-based filter with a 256 bit key (AESV3).
|
|
func NewFilterAESV3 ()Filter {_ad ,_bb :=_ef (FilterDict {});if _bb !=nil {_cc .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",_bb );
|
|
return filterAESV3 {};};return _ad ;};
|
|
|
|
// KeyLength implements Filter interface.
|
|
func (filterAESV3 )KeyLength ()int {return 256/8};
|
|
|
|
// NewFilter creates CryptFilter from a corresponding dictionary.
|
|
func NewFilter (d FilterDict )(Filter ,error ){_ffc ,_egf :=_efe (d .CFM );if _egf !=nil {return nil ,_egf ;};_ffca ,_egf :=_ffc (d );if _egf !=nil {return nil ,_egf ;};return _ffca ,nil ;};
|
|
|
|
// NewFilterV2 creates a RC4-based filter with a specified key length (in bytes).
|
|
func NewFilterV2 (length int )Filter {_ee ,_cad :=_bf (FilterDict {Length :length });if _cad !=nil {_cc .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",_cad );
|
|
return filterV2 {_ega :length };};return _ee ;};var (_ebc =make (map[string ]filterFunc ););func (filterIdentity )MakeKey (objNum ,genNum uint32 ,fkey []byte )([]byte ,error ){return fkey ,nil };func init (){_ecg ("\u0056\u0032",_bf )};
|
|
|
|
// NewFilterAESV2 creates an AES-based filter with a 128 bit key (AESV2).
|
|
func NewFilterAESV2 ()Filter {_ea ,_cg :=_bg (FilterDict {});if _cg !=nil {_cc .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 _ea ;};
|
|
|
|
// MakeKey implements Filter interface.
|
|
func (filterAESV3 )MakeKey (_ ,_ uint32 ,ekey []byte )([]byte ,error ){return ekey ,nil };
|
|
|
|
// NewIdentity creates an identity filter that bypasses all data without changes.
|
|
func NewIdentity ()Filter {return filterIdentity {}};
|
|
|
|
// HandlerVersion implements Filter interface.
|
|
func (filterAESV2 )HandlerVersion ()(V ,R int ){V ,R =4,4;return ;};func (filterIdentity )Name ()string {return "\u0049\u0064\u0065\u006e\u0074\u0069\u0074\u0079"};func _bg (_cb FilterDict )(Filter ,error ){if _cb .Length ==128{_cc .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",_cb .Length );
|
|
_cb .Length /=8;};if _cb .Length !=0&&_cb .Length !=16{return nil ,_ac .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",_cb .Length );
|
|
};return filterAESV2 {},nil ;};type filterAES struct{};
|
|
|
|
// PDFVersion implements Filter interface.
|
|
func (filterAESV2 )PDFVersion ()[2]int {return [2]int {1,5}};
|
|
|
|
// HandlerVersion implements Filter interface.
|
|
func (_de filterV2 )HandlerVersion ()(V ,R int ){V ,R =2,3;return ;};
|
|
|
|
// HandlerVersion implements Filter interface.
|
|
func (filterAESV3 )HandlerVersion ()(V ,R int ){V ,R =5,6;return ;};
|
|
|
|
// KeyLength implements Filter interface.
|
|
func (filterAESV2 )KeyLength ()int {return 128/8};var _ Filter =filterV2 {};
|
|
|
|
// 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 (_dfa ,_fg uint32 ,_aef []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 (_cdg []byte ,_fa []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 (_bgg []byte ,_dga []byte )([]byte ,error );};func (filterIdentity )KeyLength ()int {return 0};func _bf (_eda FilterDict )(Filter ,error ){if _eda .Length %8!=0{return nil ,_ac .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",_eda .Length );
|
|
};if _eda .Length < 5||_eda .Length > 16{if _eda .Length ==40||_eda .Length ==64||_eda .Length ==128{_cc .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",_eda .Length );
|
|
_eda .Length /=8;}else {return nil ,_ac .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",_eda .Length );
|
|
};};return filterV2 {_ega :_eda .Length },nil ;};func _efe (_gf string )(filterFunc ,error ){_eac :=_ebc [_gf ];if _eac ==nil {return nil ,_ac .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",_gf );
|
|
};return _eac ,nil ;};func (filterIdentity )EncryptBytes (p []byte ,okey []byte )([]byte ,error ){return p ,nil };
|
|
|
|
// KeyLength implements Filter interface.
|
|
func (_fdb filterV2 )KeyLength ()int {return _fdb ._ega };func _aa (_gbd ,_afa uint32 ,_cba []byte ,_eag bool )([]byte ,error ){_dg :=make ([]byte ,len (_cba )+5);copy (_dg ,_cba );for _cf :=0;_cf < 3;_cf ++{_caa :=byte ((_gbd >>uint32 (8*_cf ))&0xff);
|
|
_dg [_cf +len (_cba )]=_caa ;};for _ae :=0;_ae < 2;_ae ++{_gc :=byte ((_afa >>uint32 (8*_ae ))&0xff);_dg [_ae +len (_cba )+3]=_gc ;};if _eag {_dg =append (_dg ,0x73);_dg =append (_dg ,0x41);_dg =append (_dg ,0x6C);_dg =append (_dg ,0x54);};_fe :=_e .New ();
|
|
_fe .Write (_dg );_df :=_fe .Sum (nil );if len (_cba )+5< 16{return _df [0:len (_cba )+5],nil ;};return _df ,nil ;};func (filterAES )EncryptBytes (buf []byte ,okey []byte )([]byte ,error ){_af ,_fc :=_c .NewCipher (okey );if _fc !=nil {return nil ,_fc ;
|
|
};_cc .Log .Trace ("A\u0045\u0053\u0020\u0045nc\u0072y\u0070\u0074\u0020\u0028\u0025d\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );const _eg =_c .BlockSize ;_bdg :=_eg -len (buf )%_eg ;for _bbe :=0;_bbe < _bdg ;_bbe ++{buf =append (buf ,byte (_bdg ));
|
|
};_cc .Log .Trace ("\u0050a\u0064d\u0065\u0064\u0020\u0074\u006f \u0025\u0064 \u0062\u0079\u0074\u0065\u0073",len (buf ));_eb :=make ([]byte ,_eg +len (buf ));_adg :=_eb [:_eg ];if _ ,_gg :=_g .ReadFull (_b .Reader ,_adg );_gg !=nil {return nil ,_gg ;};
|
|
_edg :=_ed .NewCBCEncrypter (_af ,_adg );_edg .CryptBlocks (_eb [_eg :],buf );buf =_eb ;_cc .Log .Trace ("\u0074\u006f\u0020(\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );return buf ,nil ;};var _ Filter =filterAESV3 {};
|
|
|
|
// Name implements Filter interface.
|
|
func (filterAESV2 )Name ()string {return "\u0041\u0045\u0053V\u0032"};func _ecg (_ebd string ,_db filterFunc ){if _ ,_eage :=_ebc [_ebd ];_eage {panic ("\u0061l\u0072e\u0061\u0064\u0079\u0020\u0072e\u0067\u0069s\u0074\u0065\u0072\u0065\u0064");};_ebc [_ebd ]=_db ;
|
|
};func (filterIdentity )HandlerVersion ()(V ,R int ){return ;};func _ef (_ca FilterDict )(Filter ,error ){if _ca .Length ==256{_cc .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",_ca .Length );
|
|
_ca .Length /=8;};if _ca .Length !=0&&_ca .Length !=32{return nil ,_ac .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",_ca .Length );
|
|
};return filterAESV3 {},nil ;};type filterAESV3 struct{filterAES };
|
|
|
|
// Name implements Filter interface.
|
|
func (filterAESV3 )Name ()string {return "\u0041\u0045\u0053V\u0033"};type filterAESV2 struct{filterAES }; |