ttomcat-1778514358873.zip-extract/_dependencies/maven/com.h2database_h2-2.2.220/org/h2/tools/ChangeFileEncryption.java

Path
ttomcat-1778514358873.zip-extract/_dependencies/maven/com.h2database_h2-2.2.220/org/h2/tools/ChangeFileEncryption.java
Status
scanned
Type
file
Name
ChangeFileEncryption.java
Extension
.java
Programming language
Java
Mime type
text/html
File type
HTML document, ASCII text
Tag

      
    
Rootfs path

      
    
Size
9430 (9.2 KB)
MD5
78ab41a365b48a1c0b111b5de45331af
SHA1
76d01eb5778f4b131d6c1c057b76f0fcc3f05fc2
SHA256
932cc97dd2bfb56ff4391d3a367c82187454985f7ebfe84c181c1d23d6c5ac5c
SHA512

      
    
SHA1_git
0b558bcacf58d9ca2b3e36d89449e60fbb686196
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
ChangeFileEncryption.java | 9.2 KB |

/* * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, * and the EPL 1.0 (https://h2database.com/html/license.html). * Initial Developer: H2 Group */ package org.h2.tools; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.sql.SQLException; import java.util.ArrayList; import java.util.concurrent.TimeUnit; import org.h2.engine.Constants; import org.h2.message.DbException; import org.h2.mvstore.MVStore; import org.h2.store.FileLister; import org.h2.store.fs.FilePath; import org.h2.store.fs.FileUtils; import org.h2.store.fs.encrypt.FileEncrypt; import org.h2.store.fs.encrypt.FilePathEncrypt; import org.h2.util.Tool; /** * Allows changing the database file encryption password or algorithm. * * This tool can not be used to change a password of a user. * The database must be closed before using this tool. */ public class ChangeFileEncryption extends Tool { private String directory; private String cipherType; private byte[] decryptKey; private byte[] encryptKey; /** * Options are case sensitive. * <table> * <caption>Supported options</caption> * <tr><td>[-help] or [-?]</td> * <td>Print the list of options</td></tr> * <tr><td>[-cipher type]</td> * <td>The encryption type (AES)</td></tr> * <tr><td>[-dir &lt;dir&gt;]</td> * <td>The database directory (default: .)</td></tr> * <tr><td>[-db &lt;database&gt;]</td> * <td>Database name (all databases if not set)</td></tr> * <tr><td>[-decrypt &lt;pwd&gt;]</td> * <td>The decryption password (if not set: not yet encrypted)</td></tr> * <tr><td>[-encrypt &lt;pwd&gt;]</td> * <td>The encryption password (if not set: do not encrypt)</td></tr> * <tr><td>[-quiet]</td> * <td>Do not print progress information</td></tr> * </table> * * @param args the command line arguments */ public static void main(String... args) { try { new ChangeFileEncryption().runTool(args); } catch (SQLException ex) { ex.printStackTrace(System.err); System.exit(1); } } @Override public void runTool(String... args) throws SQLException { String dir = "."; String cipher = null; char[] decryptPassword = null; char[] encryptPassword = null; String db = null; boolean quiet = false; for (int i = 0; args != null && i < args.length; i++) { String arg = args[i]; if (arg.equals("-dir")) { dir = args[++i]; } else if (arg.equals("-cipher")) { cipher = args[++i]; } else if (arg.equals("-db")) { db = args[++i]; } else if (arg.equals("-decrypt")) { decryptPassword = args[++i].toCharArray(); } else if (arg.equals("-encrypt")) { encryptPassword = args[++i].toCharArray(); } else if (arg.equals("-quiet")) { quiet = true; } else if (arg.equals("-help") || arg.equals("-?")) { showUsage(); return; } else { showUsageAndThrowUnsupportedOption(arg); } } if ((encryptPassword == null && decryptPassword == null) || cipher == null) { showUsage(); throw new SQLException( "Encryption or decryption password not set, or cipher not set"); } try { process(dir, db, cipher, decryptPassword, encryptPassword, quiet); } catch (Exception e) { throw DbException.toSQLException(e); } } /** * Changes the password for a database. The passwords must be supplied as * char arrays and are cleaned in this method. The database must be closed * before calling this method. * * @param dir the directory (. for the current directory) * @param db the database name (null for all databases) * @param cipher the cipher (AES) * @param decryptPassword the decryption password as a char array * @param encryptPassword the encryption password as a char array * @param quiet don't print progress information * @throws SQLException on failure */ public static void execute(String dir, String db, String cipher, char[] decryptPassword, char[] encryptPassword, boolean quiet) throws SQLException { try { new ChangeFileEncryption().process(dir, db, cipher, decryptPassword, encryptPassword, quiet); } catch (Exception e) { throw DbException.toSQLException(e); } } private void process(String dir, String db, String cipher, char[] decryptPassword, char[] encryptPassword, boolean quiet) throws SQLException { dir = FileLister.getDir(dir); ChangeFileEncryption change = new ChangeFileEncryption(); if (encryptPassword != null) { for (char c : encryptPassword) { if (c == ' ') { throw new SQLException("The file password may not contain spaces"); } } change.encryptKey = FilePathEncrypt.getPasswordBytes(encryptPassword); } if (decryptPassword != null) { change.decryptKey = FilePathEncrypt.getPasswordBytes(decryptPassword); } change.out = out; change.directory = dir; change.cipherType = cipher; ArrayList<String> files = FileLister.getDatabaseFiles(dir, db, true); FileLister.tryUnlockDatabase(files, "encryption"); files = FileLister.getDatabaseFiles(dir, db, false); if (files.isEmpty() && !quiet) { printNoDatabaseFilesFound(dir, db); } // first, test only if the file can be renamed // (to find errors with locked files early) for (String fileName : files) { String temp = dir + "/temp.db"; FileUtils.delete(temp); FileUtils.move(fileName, temp); FileUtils.move(temp, fileName); } // if this worked, the operation will (hopefully) be successful // TODO changeFileEncryption: this is a workaround! // make the operation atomic (all files or none) for (String fileName : files) { // don't process a lob directory, just the files in the directory. if (!FileUtils.isDirectory(fileName)) { change.process(fileName, quiet, decryptPassword); } } } private void process(String fileName, boolean quiet, char[] decryptPassword) throws SQLException { if (fileName.endsWith(Constants.SUFFIX_MV_FILE)) { try { copyMvStore(fileName, quiet, decryptPassword); } catch (IOException e) { throw DbException.convertIOException(e, "Error encrypting / decrypting file " + fileName); } return; } } private void copyMvStore(String fileName, boolean quiet, char[] decryptPassword) throws IOException, SQLException { if (FileUtils.isDirectory(fileName)) { return; } // check that we have the right encryption key try { final MVStore source = new MVStore.Builder(). fileName(fileName). readOnly(). encryptionKey(decryptPassword). open(); source.close(); } catch (IllegalStateException ex) { throw new SQLException("error decrypting file " + fileName, ex); } String temp = directory + "/temp.db"; try (FileChannel fileIn = getFileChannel(fileName, "r", decryptKey)){ try (InputStream inStream = Channels.newInputStream(fileIn)) { FileUtils.delete(temp); try (OutputStream outStream = Channels.newOutputStream(getFileChannel(temp, "rw", encryptKey))) { final byte[] buffer = new byte[4 * 1024]; long remaining = fileIn.size(); long total = remaining; long time = System.nanoTime(); while (remaining > 0) { if (!quiet && System.nanoTime() - time > TimeUnit.SECONDS.toNanos(1)) { out.println(fileName + ": " + (100 - 100 * remaining / total) + "%"); time = System.nanoTime(); } int len = (int) Math.min(buffer.length, remaining); len = inStream.read(buffer, 0, len); outStream.write(buffer, 0, len); remaining -= len; } } } } FileUtils.delete(fileName); FileUtils.move(temp, fileName); } private static FileChannel getFileChannel(String fileName, String r, byte[] decryptKey) throws IOException { FileChannel fileIn = FilePath.get(fileName).open(r); if (decryptKey != null) { fileIn = new FileEncrypt(fileName, decryptKey, fileIn); } return fileIn; } }
Detected license expression

      
    
Detected license expression (SPDX)

      
    
Percentage of license text
1.25
Copyrights
- end_line: 2
  copyright: Copyright 2004-2023 H2 Group. Multiple-Licensed
  start_line: 2
Holders
- holder: H2 Group. Multiple-Licensed
  end_line: 2
  start_line: 2
Authors

      
    
License expression License clue details
(mpl-2.0 OR epl-1.0) AND proprietary-license {'score': 20.37, 'matcher': '3-seq', 'end_line': 3, 'rule_url': 'https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/rules/mpl-2.0_or_epl-1.0_and_proprietary-license_2.RULE', 'from_file': None, 'start_line': 2, 'matched_text': ' * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,\n * and the EPL 1.0 (https://h2database.com/html/license.html).', 'match_coverage': 20.37, 'matched_length': 11, 'rule_relevance': 100, 'rule_identifier': 'mpl-2.0_or_epl-1.0_and_proprietary-license_2.RULE', 'license_expression': '(mpl-2.0 OR epl-1.0) AND proprietary-license', 'license_expression_spdx': '(MPL-2.0 OR EPL-1.0) AND LicenseRef-scancode-proprietary-license'}
URL Start line End line
https://h2database.com/html/license.html 3 3
Package URL License Primary language
pkg:osgi/com.h2database.source@2.2.220