提交 34e74b80 authored 作者: Thomas Mueller's avatar Thomas Mueller

Tests.

上级 ab889d9a
......@@ -34,7 +34,7 @@ public class TestSecurity extends TestBase {
testConnectWithHash();
testSHA();
testAES();
testXTEA();
testBlockCiphers();
}
private static void testConnectWithHash() throws SQLException {
......@@ -90,12 +90,20 @@ public class TestSecurity extends TestBase {
assertEquals(expected, hash);
}
private static void testXTEA() {
byte[] test = new byte[4096];
BlockCipher xtea = CipherFactory.getBlockCipher("XTEA");
xtea.setKey("abcdefghijklmnop".getBytes());
for (int i = 0; i < 10; i++) {
xtea.decrypt(test, 0, test.length);
private void testBlockCiphers() {
for (String algorithm : new String[] { "AES", "XTEA", "FOG" }) {
byte[] test = new byte[4096];
BlockCipher cipher = CipherFactory.getBlockCipher(algorithm);
cipher.setKey("abcdefghijklmnop".getBytes());
for (int i = 0; i < 10; i++) {
cipher.encrypt(test, 0, test.length);
}
assertFalse(isCompressible(test));
for (int i = 0; i < 10; i++) {
cipher.decrypt(test, 0, test.length);
}
assertEquals(new byte[test.length], test);
assertTrue(isCompressible(test));
}
}
......@@ -144,4 +152,19 @@ public class TestSecurity extends TestBase {
}
}
private static boolean isCompressible(byte[] data) {
int len = data.length;
int[] sum = new int[16];
for (int i = 0; i < len; i++) {
int x = (data[i] & 255) >> 4;
sum[x]++;
}
int r = 0;
for (int x : sum) {
long v = ((long) x << 32) / len;
r += 63 - Long.numberOfLeadingZeros(v + 1);
}
return len * r < len * 120;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论