import org.nutz.resource.Scans;
public class SafeSetup implements Setup {
public void init(NutConfig config) {
Dao dao = config.getIoc().get(Dao.class);
for (Class<?> klass : Scans.me().scanPackage("org.nutz.safe.bean")) {
if (klass.getAnnotation(Table.class) != null)
dao.create(klass, false);
}
//初始化Enc
//--------------------------------------------------------------
//获取md5key
SystemConfig md5key = dao.fetch(SystemConfig.class, "enc.md5key");
if (md5key == null) {
md5key = new SystemConfig();
md5key.setName("enc.md5key");
md5key.setData(R.sg(16).next());
}
//获取系统的公钥
SystemConfig sysPubKey = dao.fetch(SystemConfig.class, "enc.sys.pubkey");
SystemConfig sysPriKey = dao.fetch(SystemConfig.class, "enc.sys.prikey");
PublicKey publicKey = null;
PrivateKey privateKey = null;
if (sysPubKey == null) {
try {
KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = new SecureRandom();
keygen.initialize(1024, random);
KeyPair kp = keygen.generateKeyPair();
publicKey = kp.getPublic();
privateKey = kp.getPrivate();
sysPubKey = new SystemConfig();
sysPubKey.setName("enc.sys.pubkey");
sysPubKey.setData(Base64.encodeToString(obj2bytes(publicKey), false));
dao.insert(sysPubKey);
sysPriKey = new SystemConfig();
sysPriKey.setName("enc.sys.prikey");
sysPriKey.setData(Base64.encodeToString(obj2bytes(privateKey), false));
dao.insert(sysPriKey);
} catch (Exception e) {
throw Lang.wrapThrow(e);
}
} else {
try {