Skip to content

Commit

Permalink
cli parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
user9209 committed Jul 25, 2018
1 parent 31ce9a5 commit 11b0524
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
6 changes: 3 additions & 3 deletions src/de/gs_sys/lib/crypto/passwords/Complexity.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void setNumbers() {
this.numbers = true;
}

public void setSpecialcahrs() {
public void setSpecialcahrs() {
this.specialchars = true;
}

Expand All @@ -39,15 +39,15 @@ public int offsetUppercase() {
}

public int offsetLowercase() {
return offsetUppercase() + (uppercase ? 26 : 0) ;
return offsetUppercase() + (uppercase ? 26 : 0);
}

public int offsetNumbers() {
return offsetLowercase() + (lowercase ? 26 : 0);
}

public int offsetSpecialchars() {
return offsetNumbers() + (numbers? 10 : 0); //+ (specialchars ? SPECIALCHARS.length() : 0);
return offsetNumbers() + (numbers ? 10 : 0); //+ (specialchars ? SPECIALCHARS.length() : 0);
}

@Override
Expand Down
68 changes: 34 additions & 34 deletions src/de/gs_sys/lib/crypto/passwords/PasswordStrength.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@
/*
* Copyright (c) 2018 Georg Schmidt
* All rights reserved
*/
*/

public class PasswordStrength {

/**
* Demo Main
*/
/**
* Demo Main
*/
public static void main(String[] args) {

//String password = "AZaz09!#446d4gd56fgd";
String password = "AZaz0946ddf4gd56fgd";

if (args[0] != null)
password = args[0];
else
System.out.println("Using demo mode '" + password + "'!\n" +
"The first parameter will taken as password in the cli.");

Complexity c = complexity(password);

System.out.println(c);
Expand All @@ -30,22 +36,21 @@ public static void main(String[] args) {

System.out.println();

if(c.hasSpecialchars()) {
if (c.hasSpecialchars()) {
System.out.println("Filled " + (int) Math.ceil((pw.length * 8) - password.length() * 6.3f) + " bit.");
System.out.println("As special chars involved, this number can be very high as each char waste 0.7 bit.");
System.out.println("Here ~ " + (int) (c.getLength() * 0.7d) + " bits");
System.out.println("Without special chars only 0.05 bit wasted.");
}
else {
} else {
System.out.println("Filled " + (int) Math.ceil((pw.length * 8) - password.length() * 5.95f) + " bit.");
System.out.println("Without special chars only 0.05 bit wasted for each char.");
}
System.out.println("May be filled up to 7 bits one time.");

// test remove chars

String demo = "§~09";
System.out.println(Arrays.toString(passwordToBytes(demo)));
// String demo = "§~09";
// System.out.println(Arrays.toString(passwordToBytes(demo)));

/*
// ~128 bit
Expand All @@ -63,44 +68,40 @@ public static void main(String[] args) {

/**
* Charset and length of a password
*
* @param password
*/
public static Complexity complexity(String password) {
Complexity complexity = new Complexity();
int chars = 0;
if(password.matches(".*[0-9].*"))
{
if (password.matches(".*[0-9].*")) {
chars = 10;
complexity.setNumbers();
}

if(password.matches(".*[A-Z].*"))
{
if (password.matches(".*[A-Z].*")) {
chars += 26;
complexity.setUppercase();
}

if(password.matches(".*[a-z].*"))
{
if (password.matches(".*[a-z].*")) {
chars += 26;
complexity.setLowercase();
}

if(password.matches(".*[" + SPECIALCHARS_REGEX + "].*"))
{
if (password.matches(".*[" + SPECIALCHARS_REGEX + "].*")) {
chars += 17;
complexity.setSpecialcahrs();
}

if(chars == 0)
{
if (chars == 0) {
System.out.println("Can not analyse " + password);
return new Complexity();
}

complexity.setCharsetSize(chars);
complexity.setLength(password.length());
complexity.setBit(bitStrength(chars,complexity.getLength()));
complexity.setBit(bitStrength(chars, complexity.getLength()));

System.out.print(chars + " chars ^ " + complexity.getLength() + " length \u2248 ");
System.out.println(complexity.getBit() + " bit");
Expand All @@ -114,17 +115,18 @@ public static byte[] passwordToBytes(String password) {

/**
* Not working yet, currently printing the position of pos^x of 79^x
*
* @param password
* @return
*/
public static byte[] passwordToBytes(String password, Complexity complexity) {

if(password == null || password.isEmpty())
if (password == null || password.isEmpty())
return new byte[0];

password = password.replaceAll("[^A-Za-z0-9" + SPECIALCHARS_REGEX + "]" ,"");
password = password.replaceAll("[^A-Za-z0-9" + SPECIALCHARS_REGEX + "]", "");

if(complexity == null) {
if (complexity == null) {
complexity = complexity(password);
}

Expand All @@ -139,25 +141,21 @@ public static byte[] passwordToBytes(String password, Complexity complexity) {
out[i] = (int) pw[i];

// Numbers
if(out[i] > 47 && out[i] < 58)
{
if (out[i] > 47 && out[i] < 58) {
out[i] -= 48 - complexity.offsetNumbers();
}
// A-Z
else if(out[i] > 64 && out[i] < 91)
{
else if (out[i] > 64 && out[i] < 91) {
out[i] -= 65 - complexity.offsetUppercase();
}
// a-z
else if(out[i] > 96 && out[i] < 123)
{
else if (out[i] > 96 && out[i] < 123) {
out[i] -= 97 - complexity.offsetLowercase();
}
// special chars
else {
out[i] = lookup(pw[i]);
if(out[i] == -1)
{
if (out[i] == -1) {
throw new RuntimeException("Unknown " + pw[i]); //Todo: handle
}
out[i] += complexity.offsetSpecialchars();
Expand All @@ -172,7 +170,7 @@ else if(out[i] > 96 && out[i] < 123)

int l = sb.length();

int fill = Math.floorMod(l,8);
int fill = Math.floorMod(l, 8);

// leading one that is trimmed
StringBuilder fillSB = new StringBuilder("00000001");
Expand All @@ -185,17 +183,18 @@ else if(out[i] > 96 && out[i] < 123)
// System.out.println("sb=" + fillSB.toString() + sb.toString());

// would trim all leading zeros so see leading one
byte[] num = new BigInteger(fillSB.toString() + sb.toString(),2).toByteArray();
byte[] num = new BigInteger(fillSB.toString() + sb.toString(), 2).toByteArray();

// trim the one
byte[] outB = new byte[num.length - 1];
System.arraycopy(num,1,outB,0,outB.length);
System.arraycopy(num, 1, outB, 0, outB.length);

return outB;
}

/**
* Look up position of a special char
*
* @param c
* @return
*/
Expand All @@ -207,6 +206,7 @@ private static int lookup(char c) {

/**
* Returns the corresponding bits of a password complexity
*
* @param base
* @param exponent
* @return
Expand Down

0 comments on commit 11b0524

Please sign in to comment.