Debian Codegraph @

src/main/org/h2/store/fs/encrypt/FilePathEncrypt.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.store.fs.encrypt;
7
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.OutputStream;
11 import java.nio.channels.Channels;
12 import java.nio.channels.FileChannel;
13 import java.nio.charset.StandardCharsets;
14 import org.h2.store.fs.FilePath;
15 import org.h2.store.fs.FilePathWrapper;
16 import org.h2.store.fs.FileUtils;
17
18 /**
19 * An encrypted file.
20 */
21 public class FilePathEncrypt extends FilePathWrapper {
22
23 private static final String SCHEME = "encrypt";
24
25 /**
26 * Register this file system.
27 */
28 public static void register() {
29 FilePath.register(new FilePathEncrypt());
30 }
31
32 @Override
33 public FileChannel open(String mode) throws IOException {
34 String[] parsed = parse(name);
35 FileChannel file = FileUtils.open(parsed[1], mode);
36 byte[] passwordBytes = parsed[0].getBytes(StandardCharsets.UTF_8);
37 return new FileEncrypt(name, passwordBytes, file);
38 }
39
40 @Override
41 public String getScheme() {
42 return SCHEME;
43 }
44
45 @Override
46 protected String getPrefix() {
47 String[] parsed = parse(name);
48 return getScheme() + ":" + parsed[0] + ":";
49 }
50
51 @Override
52 public FilePath unwrap(String fileName) {
53 return FilePath.get(parse(fileName)[1]);
54 }
55
56 @Override
57 public long size() {
58 long size = getBase().size() - FileEncrypt.HEADER_LENGTH;
59 size = Math.max(0, size);
60 if ((size & FileEncrypt.BLOCK_SIZE_MASK) != 0) {
61 size -= FileEncrypt.BLOCK_SIZE;
62 }
63 return size;
64 }
65
66 @Override
67 public OutputStream newOutputStream(boolean append) throws IOException {
68 return newFileChannelOutputStream(open("rw"), append);
69 }
70
71 @Override
72 public InputStream newInputStream() throws IOException {
73 return Channels.newInputStream(open("r"));
74 }
75
76 /**
77 * Split the file name into algorithm, password, and base file name.
78 *
79 * @param fileName the file name
80 * @return an array with algorithm, password, and base file name
81 */
82 private String[] parse(String fileName) {
83 if (!fileName.startsWith(getScheme())) {
84 throw new IllegalArgumentException(fileName +
85 " doesn't start with " + getScheme());
86 }
87 fileName = fileName.substring(getScheme().length() + 1);
88 int idx = fileName.indexOf(':');
89 String password;
90 if (idx < 0) {
91 throw new IllegalArgumentException(fileName +
92 " doesn't contain encryption algorithm and password");
93 }
94 password = fileName.substring(0, idx);
95 fileName = fileName.substring(idx + 1);
96 return new String[] { password, fileName };
97 }
98
99 /**
100 * Convert a char array to a byte array, in UTF-16 format. The char array is
101 * not cleared after use (this must be done by the caller).
102 *
103 * @param passwordChars the password characters
104 * @return the byte array
105 */
106 public static byte[] getPasswordBytes(char[] passwordChars) {
107 // using UTF-16
108 int len = passwordChars.length;
109 byte[] password = new byte[len * 2];
110 for (int i = 0; i < len; i++) {
111 char c = passwordChars[i];
112 password[i + i] = (byte) (c >>> 8);
113 password[i + i + 1] = (byte) c;
114 }
115 return password;
116 }
117
118 }