提交 bbebd0de authored 作者: Thomas Mueller's avatar Thomas Mueller

Opening and closing encrypted databases is now much faster.

上级 40ee48c6
...@@ -122,18 +122,22 @@ public class AES implements BlockCipher { ...@@ -122,18 +122,22 @@ public class AES implements BlockCipher {
} }
public void encrypt(byte[] bytes, int off, int len) { public void encrypt(byte[] bytes, int off, int len) {
if (SysProperties.CHECK && (len % ALIGN != 0)) { if (SysProperties.CHECK) {
if (len % ALIGN != 0) {
DbException.throwInternalError("unaligned len " + len); DbException.throwInternalError("unaligned len " + len);
} }
}
for (int i = off; i < off + len; i += 16) { for (int i = off; i < off + len; i += 16) {
encryptBlock(bytes, bytes, i); encryptBlock(bytes, bytes, i);
} }
} }
public void decrypt(byte[] bytes, int off, int len) { public void decrypt(byte[] bytes, int off, int len) {
if (SysProperties.CHECK && (len % ALIGN != 0)) { if (SysProperties.CHECK) {
if (len % ALIGN != 0) {
DbException.throwInternalError("unaligned len " + len); DbException.throwInternalError("unaligned len " + len);
} }
}
for (int i = off; i < off + len; i += 16) { for (int i = off; i < off + len; i += 16) {
decryptBlock(bytes, bytes, i); decryptBlock(bytes, bytes, i);
} }
......
...@@ -12,8 +12,9 @@ import org.h2.store.FileStore; ...@@ -12,8 +12,9 @@ import org.h2.store.FileStore;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
/** /**
* A file store that encrypts all data before writing, * A file store that encrypts all data before writing, and decrypts all data
* and decrypts all data after reading. * after reading. Areas that were never written to (for example after calling
* setLength to enlarge the file) are not encrypted (contains 0 bytes).
*/ */
public class SecureFileStore extends FileStore { public class SecureFileStore extends FileStore {
...@@ -72,8 +73,13 @@ public class SecureFileStore extends FileStore { ...@@ -72,8 +73,13 @@ public class SecureFileStore extends FileStore {
public void readFully(byte[] b, int off, int len) { public void readFully(byte[] b, int off, int len) {
super.readFully(b, off, len); super.readFully(b, off, len);
for (int i = 0; i < len; i++) {
if (b[i] != 0) {
cipher.decrypt(b, off, len); cipher.decrypt(b, off, len);
xorInitVector(b, off, len, pos); xorInitVector(b, off, len, pos);
break;
}
}
pos += len; pos += len;
} }
...@@ -82,29 +88,6 @@ public class SecureFileStore extends FileStore { ...@@ -82,29 +88,6 @@ public class SecureFileStore extends FileStore {
super.seek(x); super.seek(x);
} }
public void setLength(long newLength) {
long oldPos = pos;
long length = length();
if (newLength > length) {
seek(length);
if (empty == null) {
empty = new byte[16 * 1024];
}
byte[] e = empty;
while (true) {
int p = (int) Math.min(newLength - length, e.length);
if (p <= 0) {
break;
}
write(e, 0, p);
length += p;
}
seek(oldPos);
} else {
super.setLength(newLength);
}
}
private void xorInitVector(byte[] b, int off, int len, long p) { private void xorInitVector(byte[] b, int off, int len, long p) {
byte[] iv = bufferForInitVector; byte[] iv = bufferForInitVector;
while (len > 0) { while (len > 0) {
......
...@@ -42,18 +42,22 @@ public class XTEA implements BlockCipher { ...@@ -42,18 +42,22 @@ public class XTEA implements BlockCipher {
} }
public void encrypt(byte[] bytes, int off, int len) { public void encrypt(byte[] bytes, int off, int len) {
if (SysProperties.CHECK && (len % ALIGN != 0)) { if (SysProperties.CHECK) {
if (len % ALIGN != 0) {
DbException.throwInternalError("unaligned len " + len); DbException.throwInternalError("unaligned len " + len);
} }
}
for (int i = off; i < off + len; i += 8) { for (int i = off; i < off + len; i += 8) {
encryptBlock(bytes, bytes, i); encryptBlock(bytes, bytes, i);
} }
} }
public void decrypt(byte[] bytes, int off, int len) { public void decrypt(byte[] bytes, int off, int len) {
if (SysProperties.CHECK && (len % ALIGN != 0)) { if (SysProperties.CHECK) {
if (len % ALIGN != 0) {
DbException.throwInternalError("unaligned len " + len); DbException.throwInternalError("unaligned len " + len);
} }
}
for (int i = off; i < off + len; i += 8) { for (int i = off; i < off + len; i += 8) {
decryptBlock(bytes, bytes, i); decryptBlock(bytes, bytes, i);
} }
......
...@@ -529,5 +529,4 @@ public class FileStore { ...@@ -529,5 +529,4 @@ public class FileStore {
file.releaseLock(); file.releaseLock();
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论