@@ -15,12 +15,15 @@ import (
1515 "errors"
1616 "hash"
1717 "io"
18+ "time"
1819
1920 "golang.org/x/crypto/pbkdf2"
2021)
2122
23+ // EncryptionMethod is a type which defines encryption methods
2224type EncryptionMethod int
2325
26+ // Encryption methods and constants
2427const (
2528 StandardEncryption EncryptionMethod = 1
2629 AES128Encryption EncryptionMethod = 2
@@ -388,7 +391,7 @@ func encryptStream(key []byte, w io.Writer) (io.Writer, error) {
388391// data. The authcode will be written out in fileWriter.close().
389392func newEncryptionWriter (w io.Writer , password passwordFn , fw * fileWriter , aesstrength byte ) (io.Writer , error ) {
390393 keysize := aesKeyLen (aesstrength )
391- salt := make ([]byte , keysize / 2 )
394+ salt := make ([]byte , keysize / 2 )
392395 _ , err := rand .Read (salt [:])
393396 if err != nil {
394397 return nil , errors .New ("zip: unable to generate random salt" )
@@ -437,7 +440,8 @@ func (h *FileHeader) writeWinZipExtra() {
437440 h .Extra = append (h .Extra , buf [:]... )
438441}
439442
440- func (h * FileHeader ) setEncryptionMethod (enc EncryptionMethod ) {
443+ // SetEncryptionMethod sets the encryption method.
444+ func (h * FileHeader ) SetEncryptionMethod (enc EncryptionMethod ) {
441445 h .encryption = enc
442446 switch enc {
443447 case AES128Encryption :
@@ -467,17 +471,29 @@ func (h *FileHeader) SetPassword(password string) {
467471// as a byte slice
468472type passwordFn func () []byte
469473
470- // Encrypt adds a file to the zip file using the provided name.
474+ // EncryptTime adds a file to the zip file using the provided name.
471475// It returns a Writer to which the file contents should be written. File
472476// contents will be encrypted with AES-256 using the given password. The
473477// file's contents must be written to the io.Writer before the next call
474- // to Create, CreateHeader, or Close.
475- func (w * Writer ) Encrypt (name string , password string , enc EncryptionMethod ) (io.Writer , error ) {
478+ // to Create, CreateHeader, or Close. It uses the provided modTime as
479+ // the file's last modified date&time
480+ func (w * Writer ) EncryptTime (name string , password string , enc EncryptionMethod , modTime time.Time ) (io.Writer , error ) {
476481 fh := & FileHeader {
477482 Name : name ,
478483 Method : Deflate ,
479484 }
485+ fh .SetModTime (modTime )
480486 fh .SetPassword (password )
481- fh .setEncryptionMethod (enc )
487+ fh .SetEncryptionMethod (enc )
482488 return w .CreateHeader (fh )
483489}
490+
491+ // Encrypt adds a file to the zip file using the provided name.
492+ // It returns a Writer to which the file contents should be written. File
493+ // contents will be encrypted with AES-256 using the given password. The
494+ // file's contents must be written to the io.Writer before the next call
495+ // to Create, CreateHeader, or Close. It uses the current time as
496+ // the file's last modified date&time
497+ func (w * Writer ) Encrypt (name string , password string , enc EncryptionMethod ) (io.Writer , error ) {
498+ return w .EncryptTime (name , password , enc , time .Now ())
499+ }
0 commit comments