2015-10-27 12:12:51 +03:00
|
|
|
This is a fork of the Go archive/zip package to add support
|
2015-11-05 06:37:46 +03:00
|
|
|
for reading/writing password protected .zip files.
|
|
|
|
Only supports Winzip's AES extension: http://www.winzip.com/aes_info.htm.
|
|
|
|
|
|
|
|
This package DOES NOT intend to implement the encryption methods
|
2015-10-27 12:12:51 +03:00
|
|
|
mentioned in the original PKWARE spec (sections 6.0 and 7.0):
|
|
|
|
https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
|
|
|
|
|
2015-12-03 13:44:20 +03:00
|
|
|
Status - Alpha. More tests and code clean up next.
|
2015-11-05 06:37:46 +03:00
|
|
|
|
2015-12-03 13:57:36 +03:00
|
|
|
Documentation -
|
2015-12-03 13:58:29 +03:00
|
|
|
https://godoc.org/github.com/alexmullins/zip
|
2015-12-03 13:57:36 +03:00
|
|
|
|
2015-10-30 00:14:19 +03:00
|
|
|
Roadmap
|
2015-11-06 08:12:14 +03:00
|
|
|
========
|
2015-11-05 06:37:46 +03:00
|
|
|
Reading - Done.
|
2015-12-03 13:44:20 +03:00
|
|
|
Writing - Done.
|
2015-10-30 00:14:19 +03:00
|
|
|
Testing - Needs more.
|
|
|
|
|
2015-12-03 13:44:20 +03:00
|
|
|
The process
|
|
|
|
============
|
|
|
|
1. hello.txt -> compressed -> encrypted -> .zip
|
|
|
|
2. .zip -> decrypted -> decompressed -> hello.txt
|
|
|
|
|
2017-11-17 05:38:45 +03:00
|
|
|
Example Encrypt zip
|
|
|
|
==========
|
|
|
|
```
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"github.com/alexmullins/zip"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
contents := []byte("Hello World")
|
|
|
|
fzip, err := os.Create(`./test.zip`)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
zipw := zip.NewWriter(fzip)
|
|
|
|
defer zipw.Close()
|
|
|
|
w, err := zipw.Encrypt(`test.txt`, `golang`)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = io.Copy(w, bytes.NewReader(contents))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
zipw.Flush()
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-10-27 12:12:51 +03:00
|
|
|
WinZip AES specifies
|
2015-11-06 08:12:14 +03:00
|
|
|
=====================
|
2015-10-27 12:12:51 +03:00
|
|
|
1. Encryption-Decryption w/ AES-CTR (128, 192, or 256 bits)
|
|
|
|
2. Key generation with PBKDF2-HMAC-SHA1 (1000 iteration count) that
|
|
|
|
generates a master key broken into the following:
|
|
|
|
a. First m bytes is for the encryption key
|
|
|
|
b. Next n bytes is for the authentication key
|
|
|
|
c. Last 2 bytes is the password verification value.
|
|
|
|
3. Following salt lengths are used w/ password during keygen:
|
|
|
|
------------------------------
|
|
|
|
AES Key Size | Salt Size
|
|
|
|
------------------------------
|
|
|
|
128bit(16bytes) | 8 bytes
|
|
|
|
192bit(24bytes) | 12 bytes
|
|
|
|
256bit(32bytes) | 16 bytes
|
|
|
|
-------------------------------
|
|
|
|
4. Master key len = AESKeyLen + AuthKeyLen + PWVLen:
|
|
|
|
a. AES 128 = 16 + 16 + 2 = 34 bytes of key material
|
|
|
|
b. AES 192 = 24 + 24 + 2 = 50 bytes of key material
|
|
|
|
c. AES 256 = 32 + 32 + 2 = 66 bytes of key material
|
|
|
|
5. Authentication Key is same size as AES key.
|
|
|
|
6. Authentication with HMAC-SHA1-80 (truncated to 80bits).
|
|
|
|
7. A new master key is generated for every file.
|
|
|
|
8. The file header and directory header compression method will
|
2015-10-27 12:25:11 +03:00
|
|
|
be 99 (decimal) indicating Winzip AES encryption. The actual
|
|
|
|
compression method will be in the extra's payload at the end
|
|
|
|
of the headers.
|
2015-10-27 12:12:51 +03:00
|
|
|
9. A extra field will be added to the file header and directory
|
2015-10-27 12:25:11 +03:00
|
|
|
header identified by the ID 0x9901 and contains the following info:
|
2015-10-27 12:12:51 +03:00
|
|
|
a. Header ID (2 bytes)
|
|
|
|
b. Data Size (2 bytes)
|
|
|
|
c. Vendor Version (2 bytes)
|
|
|
|
d. Vendor ID (2 bytes)
|
|
|
|
e. AES Strength (1 byte)
|
|
|
|
f. Compression Method (2 bytes)
|
|
|
|
10. The Data Size is always 7.
|
2015-10-27 12:25:11 +03:00
|
|
|
11. The Vendor Version can be either 0x0001 (AE-1) or
|
2015-10-27 12:12:51 +03:00
|
|
|
0x0002 (AE-2).
|
|
|
|
12. Vendor ID is ASCII "AE"
|
|
|
|
13. AES Strength:
|
|
|
|
a. 0x01 - AES-128
|
|
|
|
b. 0x02 - AES-192
|
|
|
|
c. 0x03 - AES-256
|
|
|
|
14. Compression Method is the actual compression method
|
2015-10-27 12:25:11 +03:00
|
|
|
used that was replaced by the encryption process mentioned in #8.
|
2015-10-27 12:12:51 +03:00
|
|
|
15. AE-1 keeps the CRC and should be verified after decompression.
|
2015-10-27 12:25:11 +03:00
|
|
|
AE-2 removes the CRC and shouldn't be verified after decompression.
|
2015-10-27 12:12:51 +03:00
|
|
|
Refer to http://www.winzip.com/aes_info.htm#winzip11 for the reasoning.
|
2015-11-06 08:12:14 +03:00
|
|
|
16. Storage Format (file data payload totals CompressedSize64 bytes):
|
2015-10-30 01:56:21 +03:00
|
|
|
a. Salt - 8, 12, or 16 bytes depending on keysize
|
|
|
|
b. Password Verification Value - 2 bytes
|
2015-11-06 08:12:14 +03:00
|
|
|
c. Encrypted Data - compressed size - salt - pwv - auth code lengths
|
2015-10-30 01:56:21 +03:00
|
|
|
d. Authentication code - 10 bytes
|