@@ -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" )
@@ -467,17 +470,29 @@ func (h *FileHeader) SetPassword(password string) {
467470// as a byte slice
468471type passwordFn func () []byte
469472
470- // Encrypt adds a file to the zip file using the provided name.
473+ // EncryptTime adds a file to the zip file using the provided name.
471474// It returns a Writer to which the file contents should be written. File
472475// contents will be encrypted with AES-256 using the given password. The
473476// 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 ) {
477+ // to Create, CreateHeader, or Close. It uses the provided modTime as
478+ // the file's last modified date&time
479+ func (w * Writer ) EncryptTime (name string , password string , enc EncryptionMethod , modTime time.Time ) (io.Writer , error ) {
476480 fh := & FileHeader {
477481 Name : name ,
478482 Method : Deflate ,
479483 }
484+ fh .SetModTime (modTime )
480485 fh .SetPassword (password )
481486 fh .setEncryptionMethod (enc )
482487 return w .CreateHeader (fh )
483488}
489+
490+ // Encrypt adds a file to the zip file using the provided name.
491+ // It returns a Writer to which the file contents should be written. File
492+ // contents will be encrypted with AES-256 using the given password. The
493+ // file's contents must be written to the io.Writer before the next call
494+ // to Create, CreateHeader, or Close. It uses the current time as
495+ // the file's last modified date&time
496+ func (w * Writer ) Encrypt (name string , password string , enc EncryptionMethod ) (io.Writer , error ) {
497+ return w .EncryptTime (name , password , enc , time .Now ())
498+ }
0 commit comments