Encrypt tool and Decrypter

This commit is contained in:
xiaoyu830411
2012-08-27 11:18:46 +08:00
parent f172f0a756
commit 13fbf8b483
23 changed files with 1034 additions and 0 deletions
@@ -0,0 +1,44 @@
package com.alibaba.druid.bvt.pool;
import com.alibaba.druid.pool.DecryptException;
import com.alibaba.druid.pool.Decrypter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.SensitiveParameters;
import com.alibaba.druid.util.JdbcUtils;
import org.junit.Assert;
import org.junit.Test;
import java.sql.SQLException;
/**
* @author Jonas Yang
*/
public class DruidDataSourceDecryptTest {
@Test
public void testDecrypt() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:oracle:thin:@");
dataSource.setUsername("xiaoyu");
dataSource.setPassword("druid");
dataSource.setDecrypter(new Decrypter() {
@Override
public SensitiveParameters decrypt(SensitiveParameters parameters) throws DecryptException {
Assert.assertEquals("xiaoyu", parameters.getUsername());
throw new DecryptException("Test");
}
});
try {
dataSource.init();
dataSource.createPhysicalConnection();
Assert.assertTrue("Got Here.", false);
} catch (SQLException e) {
Assert.assertEquals("The cause clas is " + e.getCause().getClass(), DecryptException.class, e.getCause().getClass());
Assert.assertEquals("The excepiton message is " + e.getCause().getMessage(), "Test", e.getCause().getMessage());
} finally {
JdbcUtils.close(dataSource);
}
}
}
@@ -0,0 +1,11 @@
package com.alibaba.druid.bvt.support.security;
/**
* Created with IntelliJ IDEA.
* User: yangkingsel
* Date: 12-8-27
* Time: 上午9:55
* To change this template use File | Settings | File Templates.
*/
public class DecrypterTestSuite {
}
@@ -0,0 +1,21 @@
package com.alibaba.druid.support.security.decryptor;
import com.alibaba.druid.pool.DecryptException;
import com.alibaba.druid.pool.SensitiveParameters;
import junit.framework.Assert;
import org.junit.Test;
/**
* @author Jonas Yang
*/
public class AesDecrypterTest {
@Test
public void testDecrypt() throws DecryptException {
AesDecrypter decrypter = new AesDecrypter();
decrypter.setKey("123456");
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", "0UNXTyp/r/wdm+b5FAY1Jw=="));
Assert.assertEquals("It is not same.", "xiaoyu", parameters.getPassword());
}
}
@@ -0,0 +1,20 @@
package com.alibaba.druid.support.security.decryptor;
import com.alibaba.druid.pool.DecryptException;
import com.alibaba.druid.pool.SensitiveParameters;
import junit.framework.Assert;
import org.junit.Test;
/**
* @author Jonas Yang
*/
public class BlowfishDecrypterTest {
@Test
public void testDecrypt() throws DecryptException {
BlowfishDecrypter decrypter = new BlowfishDecrypter();
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", "64c5fd2979a86168"));
Assert.assertEquals("It is not same.", "123456", parameters.getPassword());
}
}
@@ -0,0 +1,21 @@
package com.alibaba.druid.support.security.decryptor;
import com.alibaba.druid.pool.DecryptException;
import com.alibaba.druid.pool.SensitiveParameters;
import junit.framework.Assert;
import org.junit.Test;
/**
* @author Jonas Yang
*/
public class DesDecrypterTest {
@Test
public void testDecrypt() throws DecryptException {
DesDecrypter decrypter = new DesDecrypter();
decrypter.setKey("123456");
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", "rIsr38vJDyQ="));
Assert.assertEquals("It is not same", parameters.getPassword(), "xiaoyu");
}
}
@@ -0,0 +1,24 @@
package com.alibaba.druid.support.security.decryptor;
import com.alibaba.druid.pool.DecryptException;
import com.alibaba.druid.pool.SensitiveParameters;
import junit.framework.Assert;
import org.junit.Test;
/**
* @author Jonas Yang
*/
public class RsaDecrypterTest {
String encryptedString = "Zaa9qpyB9snnBUz7UBvcaHcNfWMKEWvhnx0dbWmwxBOJ9vkeZNXjV9MEJezSzL+6yQh7aO29vD67" +
"qedtD9ublVwwf+jTPuO1NjzAKy0+yWVQ1KdR2KaQuvCxFWiM1n6nbpkUGB6OVpoQgFnFzdKlyC9D" +
"KU3Q6evGa1hlZ8jnHcA=";
@Test
public void testDecrypt() throws DecryptException {
RsaDecrypter decrypter = new RsaDecrypter();
decrypter.setPublicKeyFile("/Users/yangkingsel/IdeaProjects/HelloWorld/out/production/HelloWorld/a.public.key");
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", encryptedString));
Assert.assertEquals("It is not same.", "xiaoyu", parameters.getPassword());
}
}