< prev index next >

src/java.base/share/classes/java/util/zip/ZipEntry.java

Print this page

        

@@ -55,10 +55,13 @@
     long csize = -1;    // compressed size of entry data
     int method = -1;    // compression method
     int flag = 0;       // general purpose flag
     byte[] extra;       // optional extra field data for entry
     String comment;     // optional comment string for entry
+    ZipCryption zipCryption = null; // encryption / decryption engine
+    byte[] encryptionHeader; // ZIP encryption header
+    boolean passphraseValidated = false; // passphrase validation is not yet.
 
     /**
      * Compression method for uncompressed entries.
      */
     public static final int STORED = 0;

@@ -129,10 +132,12 @@
         csize = e.csize;
         method = e.method;
         flag = e.flag;
         extra = e.extra;
         comment = e.comment;
+        zipCryption = e.zipCryption;
+        encryptionHeader = e.encryptionHeader;
     }
 
     /**
      * Creates a new un-initialized zip entry
      */

@@ -671,6 +676,53 @@
         } catch (CloneNotSupportedException e) {
             // This should never happen, since we are Cloneable
             throw new InternalError(e);
         }
     }
+
+    /**
+     * Set passphrase for ZIP encryption and decryption.
+     * @param passphrase Passphrase for encryption and decryption.
+     */
+    public void setPassphrase(String passphrase) {
+        zipCryption = new TraditionalZipCryption(passphrase);
+    }
+
+    ZipCryption getZipCryption() {
+        return zipCryption;
+    }
+
+    /**
+     * Get the state whether encrypted entry or not.
+     * @return true if passphrase is required.
+     */
+    public boolean isPassphraseRequired() {
+        return (flag & 1) == 1;
+    }
+
+    /**
+     * Validate passphrase for this entry.
+     * @return true if passphrase is valid, or this entry is not encrypted.
+     * @throws ZipException Passphrase is not set or Encryption header is not set.
+     */
+    public boolean isValidPassphrase() throws ZipException{
+        if (!isPassphraseRequired()) {
+            return true;
+        }
+
+        if (zipCryption == null) {
+            throw new ZipException("Passphrase is not set.");
+        }
+        if (passphraseValidated) {
+            return true;
+        }
+        if (encryptionHeader == null) {
+            throw new ZipException("Encryption header is not set.");
+        }
+
+        zipCryption.reset();
+        byte[] tmp = encryptionHeader.clone();
+        zipCryption.decryptBytes(tmp);
+        passphraseValidated =  zipCryption.isValid(this, tmp);
+        return passphraseValidated;
+    }
 }
< prev index next >