Skip to content

Commit 6c433e6

Browse files
committed
Change README to include example
1 parent d851e40 commit 6c433e6

File tree

5 files changed

+100
-121
lines changed

5 files changed

+100
-121
lines changed

README.md

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
This fork add support for Standard Zip Encryption.
2+
3+
The work is based on https://github.com/alexmullins/zip
4+
5+
Available encryption:
6+
7+
```
8+
zip.StandardEncryption
9+
zip.AES128Encryption
10+
zip.AES192Encryption
11+
zip.AES256Encryption
12+
```
13+
14+
## Warning
15+
16+
Zip Standard Encryption isn't actually secure.
17+
Unless you have to work with it, please use AES encryption instead.
18+
19+
## Example Encrypt Zip
20+
21+
```
22+
package main
23+
24+
import (
25+
"bytes"
26+
"log"
27+
"os"
28+
29+
"github.com/yeka/zip"
30+
)
31+
32+
func main() {
33+
contents := []byte("Hello World")
34+
fzip, err := os.Create(`./test.zip`)
35+
if err != nil {
36+
log.Fatalln(err)
37+
}
38+
zipw := zip.NewWriter(fzip)
39+
defer zipw.Close()
40+
w, err := zipw.Encrypt(`test.txt`, `golang`, zip.AES256Encryption)
41+
if err != nil {
42+
log.Fatal(err)
43+
}
44+
_, err = io.Copy(w, bytes.NewReader(contents))
45+
if err != nil {
46+
log.Fatal(err)
47+
}
48+
zipw.Flush()
49+
}
50+
```
51+
52+
## Example Decrypt Zip
53+
54+
```
55+
package main
56+
57+
import (
58+
"fmt"
59+
"io/ioutil"
60+
"log"
61+
62+
"github.com/yeka/zip"
63+
)
64+
65+
func main() {
66+
r, err := zip.OpenReader("encrypted.zip")
67+
if err != nil {
68+
log.Fatal(err)
69+
}
70+
defer r.Close()
71+
72+
for _, f := range r.File {
73+
if f.IsEncrypted() {
74+
f.SetPassword("12345")
75+
}
76+
77+
r, err := f.Open()
78+
if err != nil {
79+
log.Fatal(err)
80+
}
81+
82+
buf, err := ioutil.ReadAll(r)
83+
if err != nil {
84+
log.Fatal(err)
85+
}
86+
defer r.Close()
87+
88+
fmt.Printf("Size of %v: %v byte(s)\n", f.Name, len(buf))
89+
}
90+
}
91+
```

README.txt

-112
This file was deleted.

crypto.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import (
2222
type EncryptionMethod int
2323

2424
const (
25-
ZipStandardEncryption EncryptionMethod = 1
26-
AES128Encryption EncryptionMethod = 2
27-
AES192Encryption EncryptionMethod = 3
28-
AES256Encryption EncryptionMethod = 4
25+
StandardEncryption EncryptionMethod = 1
26+
AES128Encryption EncryptionMethod = 2
27+
AES192Encryption EncryptionMethod = 3
28+
AES256Encryption EncryptionMethod = 4
2929

3030
// AES key lengths
3131
aes128 = 16

crypto_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func TestPasswordWriteSimple(t *testing.T) {
175175
contents := []byte("Hello World")
176176
conLen := len(contents)
177177

178-
for _, enc := range []EncryptionMethod{ZipStandardEncryption, AES128Encryption, AES192Encryption, AES256Encryption} {
178+
for _, enc := range []EncryptionMethod{StandardEncryption, AES128Encryption, AES192Encryption, AES256Encryption} {
179179
raw := new(bytes.Buffer)
180180
zipw := NewWriter(raw)
181181
w, err := zipw.Encrypt("hello.txt", "golang", enc)
@@ -223,7 +223,7 @@ func TestZipCrypto(t *testing.T) {
223223

224224
raw := new(bytes.Buffer)
225225
zipw := NewWriter(raw)
226-
w, err := zipw.Encrypt("hello.txt", "golang", ZipStandardEncryption)
226+
w, err := zipw.Encrypt("hello.txt", "golang", StandardEncryption)
227227
if err != nil {
228228
t.Errorf("Expected to create a new FileHeader")
229229
}

writer.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) {
229229
// check for password
230230
var sw io.Writer = fw.compCount
231231
if fh.password != nil {
232-
if fh.encryption == ZipStandardEncryption {
232+
if fh.encryption == StandardEncryption {
233233
ew, err := ZipCryptoEncryptor(sw, fh.password, fw)
234234
if err != nil {
235235
return nil, err
@@ -321,7 +321,7 @@ func (w *fileWriter) close() error {
321321
return err
322322
}
323323
// if encrypted grab the hmac and write it out
324-
if w.header.IsEncrypted() && w.header.encryption != ZipStandardEncryption {
324+
if w.header.IsEncrypted() && w.header.encryption != StandardEncryption {
325325
authCode := w.hmac.Sum(nil)
326326
authCode = authCode[:10]
327327
_, err := w.compCount.Write(authCode)
@@ -332,7 +332,7 @@ func (w *fileWriter) close() error {
332332
// update FileHeader
333333
fh := w.header.FileHeader
334334
// ae-2 we don't write out CRC
335-
if !fh.IsEncrypted() || fh.encryption == ZipStandardEncryption {
335+
if !fh.IsEncrypted() || fh.encryption == StandardEncryption {
336336
fh.CRC32 = w.crc32.Sum32()
337337
}
338338
fh.CompressedSize64 = uint64(w.compCount.count)

0 commit comments

Comments
 (0)