From c5fb195ee33b8881c9e40b8b0f9ef753423c99d8 Mon Sep 17 00:00:00 2001 From: Meik Kreyenkoetter Date: Mon, 11 Jun 2018 15:57:19 +0200 Subject: [PATCH 1/2] Fixed test error. f.FileInfo().Name is a func value, not called --- crypto_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto_test.go b/crypto_test.go index cc5e2de..6e95af5 100644 --- a/crypto_test.go +++ b/crypto_test.go @@ -56,7 +56,7 @@ func TestPasswordHelloWorldAes(t *testing.T) { var b bytes.Buffer for _, f := range r.File { if !f.IsEncrypted() { - t.Errorf("Expected %s to be encrypted.", f.FileInfo().Name) + t.Errorf("Expected %s to be encrypted.", f.FileInfo().Name()) } f.SetPassword("golang") rc, err := f.Open() From 790e8f4b4e1bfb33323e3336fd78f7dce3906310 Mon Sep 17 00:00:00 2001 From: Meik Kreyenkoetter Date: Mon, 11 Jun 2018 15:58:11 +0200 Subject: [PATCH 2/2] Export SetEncryptionMethod. Allows creation of own header. For example UTF-8 file names by setting bit 11 on header. --- crypto.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crypto.go b/crypto.go index df23856..7da7640 100644 --- a/crypto.go +++ b/crypto.go @@ -388,7 +388,7 @@ func encryptStream(key []byte, w io.Writer) (io.Writer, error) { // data. The authcode will be written out in fileWriter.close(). func newEncryptionWriter(w io.Writer, password passwordFn, fw *fileWriter, aesstrength byte) (io.Writer, error) { keysize := aesKeyLen(aesstrength) - salt := make([]byte, keysize / 2) + salt := make([]byte, keysize/2) _, err := rand.Read(salt[:]) if err != nil { return nil, errors.New("zip: unable to generate random salt") @@ -437,7 +437,8 @@ func (h *FileHeader) writeWinZipExtra() { h.Extra = append(h.Extra, buf[:]...) } -func (h *FileHeader) setEncryptionMethod(enc EncryptionMethod) { +// SetEncryptionMethod sets the encryption method. +func (h *FileHeader) SetEncryptionMethod(enc EncryptionMethod) { h.encryption = enc switch enc { case AES128Encryption: @@ -478,6 +479,6 @@ func (w *Writer) Encrypt(name string, password string, enc EncryptionMethod) (io Method: Deflate, } fh.SetPassword(password) - fh.setEncryptionMethod(enc) + fh.SetEncryptionMethod(enc) return w.CreateHeader(fh) }