[Minor] looping in PasswordCreator
This commit is contained in:
parent
b8452dc6e4
commit
3905a38a32
|
@ -43,90 +43,94 @@ public class PasswordCreator {
|
||||||
@SuppressWarnings("nls")
|
@SuppressWarnings("nls")
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
|
while (true) {
|
||||||
|
|
||||||
String hashAlgorithm = null;
|
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
|
||||||
while (hashAlgorithm == null) {
|
|
||||||
System.out.print("Hash Algorithm [PBKDF2WithHmacSHA512]: ");
|
|
||||||
String readLine = r.readLine().trim();
|
|
||||||
|
|
||||||
if (readLine.isEmpty()) {
|
String hashAlgorithm = null;
|
||||||
hashAlgorithm = "PBKDF2WithHmacSHA512";
|
while (hashAlgorithm == null) {
|
||||||
} else {
|
System.out.print("Hash Algorithm [PBKDF2WithHmacSHA512]: ");
|
||||||
|
String readLine = r.readLine().trim();
|
||||||
|
|
||||||
try {
|
if (readLine.isEmpty()) {
|
||||||
SecretKeyFactory.getInstance(readLine);
|
hashAlgorithm = "PBKDF2WithHmacSHA512";
|
||||||
hashAlgorithm = readLine;
|
} else {
|
||||||
} catch (Exception e) {
|
|
||||||
System.err.println(e.getLocalizedMessage());
|
try {
|
||||||
hashAlgorithm = null;
|
SecretKeyFactory.getInstance(readLine);
|
||||||
|
hashAlgorithm = readLine;
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println(e.getLocalizedMessage());
|
||||||
|
hashAlgorithm = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int iterations = -1;
|
int iterations = -1;
|
||||||
while (iterations == -1) {
|
while (iterations == -1) {
|
||||||
System.out.print("Hash iterations [200000]: ");
|
System.out.print("Hash iterations [10000]: ");
|
||||||
String readLine = r.readLine().trim();
|
String readLine = r.readLine().trim();
|
||||||
|
|
||||||
if (readLine.isEmpty()) {
|
if (readLine.isEmpty()) {
|
||||||
iterations = 200000;
|
iterations = 10000;
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
iterations = Integer.parseInt(readLine);
|
iterations = Integer.parseInt(readLine);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println(e.getLocalizedMessage());
|
System.err.println(e.getLocalizedMessage());
|
||||||
iterations = -1;
|
iterations = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int keyLength = -1;
|
int keyLength = -1;
|
||||||
while (keyLength == -1) {
|
while (keyLength == -1) {
|
||||||
System.out.print("Hash keyLength [256]: ");
|
System.out.print("Hash keyLength [256]: ");
|
||||||
String readLine = r.readLine().trim();
|
String readLine = r.readLine().trim();
|
||||||
|
|
||||||
if (readLine.isEmpty()) {
|
if (readLine.isEmpty()) {
|
||||||
keyLength = 256;
|
keyLength = 256;
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
keyLength = Integer.parseInt(readLine);
|
keyLength = Integer.parseInt(readLine);
|
||||||
if (keyLength <= 0)
|
if (keyLength <= 0)
|
||||||
throw new IllegalArgumentException("KeyLength must be > 0");
|
throw new IllegalArgumentException("KeyLength must be > 0");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println(e.getLocalizedMessage());
|
System.err.println(e.getLocalizedMessage());
|
||||||
keyLength = -1;
|
keyLength = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Map<String, String> parameterMap = new HashMap<>();
|
||||||
|
parameterMap.put(XmlConstants.XML_PARAM_HASH_ALGORITHM, hashAlgorithm);
|
||||||
|
parameterMap.put(XmlConstants.XML_PARAM_HASH_ITERATIONS, "" + iterations);
|
||||||
|
parameterMap.put(XmlConstants.XML_PARAM_HASH_KEY_LENGTH, "" + keyLength);
|
||||||
|
|
||||||
|
DefaultEncryptionHandler encryptionHandler = new DefaultEncryptionHandler();
|
||||||
|
encryptionHandler.initialize(parameterMap);
|
||||||
|
|
||||||
|
System.out.print("Password: ");
|
||||||
|
char[] password = r.readLine().trim().toCharArray();
|
||||||
|
System.out.print("Salt [random]: ");
|
||||||
|
String saltTemp = r.readLine().trim();
|
||||||
|
if (saltTemp.isEmpty()) {
|
||||||
|
saltTemp = encryptionHandler.nextToken();
|
||||||
|
}
|
||||||
|
String saltS = StringHelper.getHexString(saltTemp.getBytes());
|
||||||
|
byte[] salt = StringHelper.fromHexString(saltS);
|
||||||
|
|
||||||
|
byte[] passwordHash = encryptionHandler.hashPassword(password, salt);
|
||||||
|
String passwordHashS = StringHelper.getHexString(passwordHash);
|
||||||
|
System.out.println("Hash is: " + passwordHashS);
|
||||||
|
System.out.println("Salt is: " + saltS);
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
System.out.println(XmlConstants.XML_ATTR_PASSWORD + "=\"" + passwordHashS + "\" "
|
||||||
|
+ XmlConstants.XML_ATTR_SALT + "=\"" + saltS + "\"");
|
||||||
|
System.out.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, String> parameterMap = new HashMap<>();
|
|
||||||
parameterMap.put(XmlConstants.XML_PARAM_HASH_ALGORITHM, hashAlgorithm);
|
|
||||||
parameterMap.put(XmlConstants.XML_PARAM_HASH_ITERATIONS, "" + iterations);
|
|
||||||
parameterMap.put(XmlConstants.XML_PARAM_HASH_KEY_LENGTH, "" + keyLength);
|
|
||||||
|
|
||||||
DefaultEncryptionHandler encryptionHandler = new DefaultEncryptionHandler();
|
|
||||||
encryptionHandler.initialize(parameterMap);
|
|
||||||
|
|
||||||
System.out.print("Password: ");
|
|
||||||
char[] password = r.readLine().trim().toCharArray();
|
|
||||||
System.out.print("Salt [random]: ");
|
|
||||||
String saltTemp = r.readLine().trim();
|
|
||||||
if (saltTemp.isEmpty()) {
|
|
||||||
saltTemp = encryptionHandler.nextToken();
|
|
||||||
}
|
|
||||||
String saltS = StringHelper.getHexString(saltTemp.getBytes());
|
|
||||||
byte[] salt = StringHelper.fromHexString(saltS);
|
|
||||||
|
|
||||||
byte[] passwordHash = encryptionHandler.hashPassword(password, salt);
|
|
||||||
String passwordHashS = StringHelper.getHexString(passwordHash);
|
|
||||||
System.out.println("Hash is: " + passwordHashS);
|
|
||||||
System.out.println("Salt is: " + saltS);
|
|
||||||
System.out.println();
|
|
||||||
|
|
||||||
System.out.println(XmlConstants.XML_ATTR_PASSWORD + "=\"" + passwordHashS + "\" " + XmlConstants.XML_ATTR_SALT
|
|
||||||
+ "=\"" + saltS + "\"");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue