@Command(name = "CreateUserSecretKey", usage = ADD_USER_SECRET_KEY) public void addUserSecretKey(String[] args) { final String keyName = StringArrayUtil.getRequiredValue(args, 0); final String keyText = StringArrayUtil.getRequiredValue(args, 1); try { byte[] key = keyText.getBytes("UTF-8"); MessageDigest sha = MessageDigest.getInstance("SHA-1"); key = sha.digest(key); key = Arrays.copyOf(key, 16); // use only first 128 bit mgr.clearKey(keyName); mgr.setKey(keyName, new SecretKeySpec(key, "AES")); } catch (Exception e) { System.err.println("Failed to add new random secret key: " + e.getMessage()); e.printStackTrace(); } }
@Command(name = "RemoveSecretKey", usage = REMOVE_SECRET_KEY) public void removeSecretKey(String[] args) { String keyName = StringArrayUtil.getRequiredValue(args, 0); // remove secret key try { mgr.clearKey(keyName); } catch (Exception e) { System.err.println("Failed to add new random secret key: " + e.getMessage()); } }
@Command(name = "CreateRandomSecretKey", usage = ADD_RANDOM_SECRET_KEY) public void addRandomSecretKey(String[] args) { final String keyName = StringArrayUtil.getRequiredValue(args, 0); // generate a new random secret key try { final KeyGenerator keyGen = KeyGenerator.getInstance("AES"); final SecureRandom random = new SecureRandom(); // cryptograph. secure random keyGen.init(random); final SecretKey key = keyGen.generateKey(); mgr.clearKey(keyName); mgr.setKey(keyName, key); } catch (Exception e) { System.err.println("Failed to add new random secret key: " + e.getMessage()); e.printStackTrace(); } }