66 "crypto/rsa"
77 "crypto/sha256"
88 "encoding/base64"
9- "errors"
109 "io"
1110 "path"
1211 "strconv"
@@ -30,10 +29,6 @@ func SignPluginWithPrivateKey(
3029 return nil , err
3130 }
3231
33- if verification == nil {
34- return nil , errors .New ("verification cannot be nil" )
35- }
36-
3732 // create a new zip writer
3833 zipBuffer := new (bytes.Buffer )
3934 zipWriter := zip .NewWriter (zipBuffer )
@@ -74,116 +69,31 @@ func SignPluginWithPrivateKey(
7469 return nil , err
7570 }
7671
77- // write the verification into data
78- // NOTE: .verification.dify.json is a special file that contains the verification information
79- // and it will be placed at the end of the zip file, checksum is calculated using it also
80- verificationBytes := parser .MarshalJsonBytes (verification )
81-
82- // write verification into the zip file
83- fileWriter , err := zipWriter .Create (consts .VERIFICATION_FILE )
84- if err != nil {
85- return nil , err
86- }
87-
88- if _ , err := fileWriter .Write (verificationBytes ); err != nil {
89- return nil , err
90- }
91-
92- // hash the verification
93- hash := sha256 .New ()
94- hash .Write (verificationBytes )
95- hashed := hash .Sum (nil )
96-
97- // write the hash into data
98- if _ , err := data .Write (hashed ); err != nil {
99- return nil , err
100- }
101-
102- // get current time
103- ct := time .Now ().Unix ()
104-
105- // convert time to bytes
106- timeString := strconv .FormatInt (ct , 10 )
107-
108- // write the time into data
109- data .Write ([]byte (timeString ))
110-
111- // sign the data
112- signature , err := encryption .RSASign (privateKey , data .Bytes ())
113- if err != nil {
114- return nil , err
115- }
116-
117- // write the signature into the comment field of the zip file
118- comments := parser .MarshalJson (map [string ]any {
119- "signature" : base64 .StdEncoding .EncodeToString (signature ),
120- "time" : ct ,
121- })
122-
123- // write signature
124- err = zipWriter .SetComment (comments )
125- if err != nil {
126- return nil , err
127- }
128-
129- // close the zip writer
130- err = zipWriter .Close ()
131- if err != nil {
132- return nil , err
133- }
134-
135- return zipBuffer .Bytes (), nil
136- }
137-
138- // Only used for testing
139- // WARNING: This function is deprecated, use SignPluginWithPrivateKey instead
140- func TraditionalSignPlugin (
141- plugin []byte ,
142- privateKey * rsa.PrivateKey ,
143- ) ([]byte , error ) {
144- decoder , err := decoder .NewZipPluginDecoder (plugin )
145- if err != nil {
146- return nil , err
147- }
72+ if verification != nil {
73+ // write the verification into data
74+ // NOTE: .verification.dify.json is a special file that contains the verification information
75+ // and it will be placed at the end of the zip file, checksum is calculated using it also
76+ verificationBytes := parser .MarshalJsonBytes (verification )
14877
149- // create a new zip writer
150- zipBuffer := new (bytes.Buffer )
151- zipWriter := zip .NewWriter (zipBuffer )
152-
153- defer zipWriter .Close ()
154- // store temporary hash
155- data := new (bytes.Buffer )
156- // read one by one
157- err = decoder .Walk (func (filename , dir string ) error {
158- file , err := decoder .ReadFile (path .Join (dir , filename ))
78+ // write verification into the zip file
79+ fileWriter , err := zipWriter .Create (consts .VERIFICATION_FILE )
15980 if err != nil {
160- return err
81+ return nil , err
16182 }
16283
163- // calculate sha256 hash of the file
84+ if _ , err := fileWriter .Write (verificationBytes ); err != nil {
85+ return nil , err
86+ }
87+
88+ // hash the verification
16489 hash := sha256 .New ()
165- hash .Write (file )
90+ hash .Write (verificationBytes )
16691 hashed := hash .Sum (nil )
16792
16893 // write the hash into data
169- data .Write (hashed )
170-
171- // create a new file in the zip writer
172- fileWriter , err := zipWriter .Create (path .Join (dir , filename ))
173- if err != nil {
174- return err
175- }
176-
177- _ , err = io .Copy (fileWriter , bytes .NewReader (file ))
178- if err != nil {
179- return err
94+ if _ , err := data .Write (hashed ); err != nil {
95+ return nil , err
18096 }
181-
182- return nil
183- })
184-
185- if err != nil {
186- return nil , err
18797 }
18898
18999 // get current time
@@ -221,3 +131,4 @@ func TraditionalSignPlugin(
221131
222132 return zipBuffer .Bytes (), nil
223133}
134+
0 commit comments