Changeset 8085 for branches/3.19-stable


Ignore:
Timestamp:
Oct 27, 2022, 7:25:30 AM (7 months ago)
Author:
Nicklas Nordborg
Message:

References #2284: Add support for encryption to the Packed file exporter plug-in

The ZIP exporter now supports encryption.

Location:
branches/3.19-stable/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/3.19-stable/src/core/net/sf/basedb/util/zip/FilePacker.java

    r7551 r8085  
    2727import java.io.InputStream;
    2828import java.io.OutputStream;
     29
     30import net.sf.basedb.core.PluginParameter;
     31import net.sf.basedb.core.StringParameterType;
    2932
    3033/**
     
    6568 
    6669  /**
     70    Does the packer support encryption or not?
     71    The default implementation return false.
     72    @since 3.19.5
     73  */
     74  public default boolean supportsEncryption()
     75  {
     76    return false;
     77  }
     78 
     79  /**
     80    The default implementation return a parameter asking for a password.
     81    The implementing class may create another parameter as long as it is
     82    named "password". This method is only called if the implementation
     83    supports encryption.
     84    @since 3.19.5
     85  */
     86  public default PluginParameter<String> getPasswordParameter()
     87  {
     88    PluginParameter<String> passwordParameter = new PluginParameter<String>
     89    (
     90      "password", "Password", "Enter a password to encrypt the archive." , new StringParameterType()
     91    );
     92    return passwordParameter;
     93  }
     94 
     95  /**
     96    Set the password to use for encrypting the archive. This method
     97    is only called if the implementation supports encryption and the
     98    user has entered a password.
     99    @since 3.19.5
     100  */
     101  public default void setPassword(String password)
     102  {}
     103 
     104  /**
    67105    The output stream that the compressed files should be written to.
    68106    @param out The output stream to write to
  • branches/3.19-stable/src/core/net/sf/basedb/util/zip/ZipFilePacker.java

    r6127 r8085  
    2323package net.sf.basedb.util.zip;
    2424
     25import net.lingala.zip4j.io.outputstream.ZipOutputStream;
     26import net.lingala.zip4j.model.ZipParameters;
     27import net.lingala.zip4j.model.enums.AesKeyStrength;
     28import net.lingala.zip4j.model.enums.CompressionLevel;
     29import net.lingala.zip4j.model.enums.CompressionMethod;
     30import net.lingala.zip4j.model.enums.EncryptionMethod;
     31import net.sf.basedb.core.BaseException;
     32import net.sf.basedb.core.PluginParameter;
     33import net.sf.basedb.core.StringParameterType;
    2534import net.sf.basedb.util.FileUtil;
    2635
     
    2837import java.io.InputStream;
    2938import java.io.OutputStream;
    30 import java.util.zip.ZipEntry;
    31 import java.util.zip.ZipOutputStream;
    3239
    3340/**
     
    4249{
    4350  private ZipOutputStream zip;
     51  private String password;
    4452 
    4553  /**
     
    7886  }
    7987  /**
    80    * Wrap the output stream in a {@link ZipOutputStream}.
    81    */
     88    Wrap the output stream in a {@link ZipOutputStream}.
     89  */
    8290  @Override
    8391  public void setOutputStream(OutputStream out)
    8492  {
    85     this.zip = new ZipOutputStream(out);
    86     zip.setMethod(ZipOutputStream.DEFLATED);
     93    try
     94    {
     95      this.zip = password == null ? new ZipOutputStream(out) : new ZipOutputStream(out, password.toCharArray());
     96    }
     97    catch (IOException ex)
     98    {
     99      throw new BaseException(ex);
     100    }
    87101  }
     102 
    88103  /**
    89     Create a new {@link ZipEntry} and write the compressed
    90     data to it.
     104    Encryption is supported.
     105    @since 3.19.5
     106  */
     107  @Override
     108  public boolean supportsEncryption()
     109  {
     110    return true;
     111  }
     112
     113  /**
     114    Get the encyption password parameter.
     115    @since 3.19.5
     116  */
     117  @Override
     118  public PluginParameter<String> getPasswordParameter()
     119  {
     120    PluginParameter<String> passwordParameter = new PluginParameter<String>
     121    (
     122      "password", "Password", "Enter a password to encrypt the archive with strong AES256 encryption. "
     123        + "If empty, the ZIP archive will not be encrypted." , new StringParameterType()
     124    );
     125    return passwordParameter;
     126  }
     127
     128  /**
     129    @since 3.19.5
     130  */
     131  @Override
     132  public void setPassword(String password)
     133  {
     134    this.password = password;
     135  }
     136
     137  /**
     138    Create a new entry and write the compressed data to it.
    91139  */
    92140  @Override
     
    96144    boolean isDirectory = in == null;
    97145    if (isDirectory && !entryName.endsWith("/")) entryName += "/";
    98     ZipEntry entry = new ZipEntry(entryName);
    99     if (lastModified > 0) entry.setTime(lastModified);
    100     if (isDirectory)
     146   
     147    ZipParameters zipParameters = new ZipParameters();
     148    zipParameters.setFileNameInZip(entryName);
     149    if (lastModified > 0)
    101150    {
    102       entry.setMethod(ZipOutputStream.STORED);
    103       entry.setSize(0);
    104       entry.setCrc(0);
     151      zipParameters.setLastModifiedFileTime(lastModified);
     152    }
     153    if (isDirectory)
     154    {
     155      zipParameters.setCompressionMethod(CompressionMethod.STORE);
     156      zipParameters.setEntrySize(0);
     157      zipParameters.setEntryCRC(0);
    105158    }
    106159    else
    107160    {
    108       entry.setSize(size);
     161      zipParameters.setCompressionMethod(CompressionMethod.DEFLATE);
     162      zipParameters.setCompressionLevel(CompressionLevel.NORMAL);
     163      zipParameters.setEntrySize(size);
     164      if (password != null)
     165      {
     166        zipParameters.setEncryptFiles(true);
     167        zipParameters.setEncryptionMethod(EncryptionMethod.AES);
     168        zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
     169      }
    109170    }
    110     zip.putNextEntry(entry);
     171    zip.putNextEntry(zipParameters);
    111172    if (!isDirectory) FileUtil.copy(in, zip);
    112173    zip.flush();
  • branches/3.19-stable/src/plugins/core/net/sf/basedb/plugins/PackedFileExporter.java

    r7605 r8085  
    308308       
    309309        // Other options
     310        if (packer.supportsEncryption())
     311        {
     312          storeValue(job, request, ri.getParameter("password"));
     313        }
    310314        storeValue(job, request, ri.getParameter("removeItems"));
    311315        if (saveAs != null)
     
    358362    List<Integer> directories = job.getValues("directories");
    359363    boolean removeItems = Boolean.TRUE.equals(job.getValue("removeItems"));
     364    String password = job.getValue("password");
    360365    FilePacker packer = getPacker();
     366    if (packer.supportsEncryption() && password != null)
     367    {
     368      packer.setPassword(password);
     369    }
    361370
    362371    ChainedProgressReporter chained = null;
     
    528537        parameters.add(getSaveAsParameter(null, null, defaultPath, requireFile));
    529538        parameters.add(getOverwriteParameter(null, null));
     539        // Encryption
     540        if (packer.supportsEncryption())
     541        {
     542          parameters.add(packer.getPasswordParameter());
     543        }
    530544
    531545        // Remove parameter
Note: See TracChangeset for help on using the changeset viewer.