Skip to content

Commit

Permalink
NEW.
Browse files Browse the repository at this point in the history
Password is now more variative :)
  • Loading branch information
lyubick committed Apr 12, 2015
1 parent da2f338 commit a4753bc
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 186 deletions.
5 changes: 5 additions & 0 deletions src/cryptosystem/CryptoSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,9 @@ public String getPassword(long cycles, String pwdName, Task<Void> passwordCalcul

return Utilities.bytesToHex(tmp);
}

public String getHash(String toHash, String salt)
{
return sha.getStringSHA512((toHash + salt).getBytes());
}
}
12 changes: 0 additions & 12 deletions src/db/PasswordCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import utilities.Utilities;
import cryptosystem.CryptoSystem;
import logger.Logger;
import main.Exceptions;
import main.Terminator;
import main.Exceptions.XC;
Expand Down Expand Up @@ -88,17 +87,6 @@ public ObservableList<iSpecialPassword> getIface()
return pSet;
}

public void dump()
{
Logger.printDebug("Dumping PasswordCollection... START");

Logger.printDebug("Collection: ");
for (SpecialPassword sp : db)
sp.dump();

Logger.printDebug("Dumping PasswordCollection... DONE!");
}

public Task<Void> save()
{
Task<Void> saveTask = new Task<Void>()
Expand Down
232 changes: 99 additions & 133 deletions src/db/SpecialPassword.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package db;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.math.BigInteger;
import java.util.BitSet;

import sha.SHA;
import utilities.Utilities;
import cryptosystem.CryptoSystem;
import logger.Logger;
import main.Exceptions;
Expand All @@ -24,35 +26,15 @@ public enum ParamsMaskBits
TOTAL_COUNT,
}

String asyncronouslyGeneratedPassword = "";
private String name;
private String comment;
private String url;
private int length;
private String specialChars;
private BitSet paramsMask = null;
private long shaCycles; // generated
// in
// PasswordCollection;
private long shaCycles = 0;
private String name = null;
private String comment = null;
private String url = null;
private int length = 0;
private String specialChars = null;
private BitSet paramsMask = null;

private static final long serialVersionUID = 2L;

public SpecialPassword()
{
Logger.printDebug("SpecialPassword DEFAULT constructor... START");

this.name = "EMPTY";
this.comment = "COMMENT";
this.url = "URL";
this.length = 16;
this.paramsMask = new BitSet(ParamsMaskBits.TOTAL_COUNT.ordinal());
this.paramsMask.set(0, ParamsMaskBits.TOTAL_COUNT.ordinal());
this.specialChars = "!@#$%^&*";

this.shaCycles = 1;

Logger.printDebug("SpecialPassword DEFAULT constructor... DONE!");
}
private static final long serialVersionUID = 1L;

public SpecialPassword(String name, String comment, String url, int length, BitSet paramsMask,
String specialChars) throws Exceptions
Expand Down Expand Up @@ -99,33 +81,6 @@ public SpecialPassword(SpecialPassword other) throws Exceptions
Logger.printDebug("SpecialPassword copy-constructor... DONE!");
}

public void dump()
{
Field[] properties = this.getClass().getDeclaredFields();
StringBuilder template = new StringBuilder();

template.append("SpecialPassword: ");

for (Field property : properties)
{
template.append(property.getName());
template.append(": ");

try
{
template.append(property.get(this));
}
catch (IllegalAccessException e)
{
Logger.printError(e.toString());
}

template.append("; ");
}

Logger.printDebug(template.toString());
}

public String getName()
{
return name;
Expand Down Expand Up @@ -186,10 +141,7 @@ public boolean equals(Object other)
@Override
public int hashCode()
{
Logger.printError("Illegal call of hashCode.");
assert false : "Illegal call of hashCode.";
Terminator.terminate(new Exceptions(XC.INIT_FAILURE));
return 0;
return name.hashCode();
}

private byte getSpecialCharactersCount()
Expand All @@ -199,99 +151,113 @@ private byte getSpecialCharactersCount()

public String getPassword(Task<Void> passwordCalculation)
{
int idx = 0;
String alphabeta = "0123456789abcdefghijklmnopqrstuvwxyz";
String specialSalt = "SPECIAL";

String passwordHash = null;
String specialHash = null;

StringBuilder password = new StringBuilder("");

/*
* 1. Stage - 64-byte long hash will be converted to character/number
* set of appropriate length
*/
Logger.printDebug("Password generation. STAGE 1. START");

try
{
String hash =
CryptoSystem.getInstance().getPassword(this.shaCycles, this.getName(),
passwordCalculation);

// take first @a this.length chars from hash
StringBuilder clearPass = new StringBuilder(hash.substring(0, this.length));
passwordHash =
CryptoSystem.getInstance().getPassword(shaCycles, name, passwordCalculation);
specialHash = CryptoSystem.getInstance().getHash(passwordHash, specialSalt);
}
catch (Exceptions e)
{
Terminator.terminate(e);
}

// set special characters
if (paramsMask.get(ParamsMaskBits.HAS_SPECIAL_CHARACTERS.ordinal())
&& specialChars.length() != 0)
{
byte count = getSpecialCharactersCount();
int idx = hash.length() - 2;
byte specialCharacterPosition = (byte) hash.charAt(idx--);
byte insertPosition = (byte) hash.charAt(idx--);
int loopGuard = clearPass.length(); // to avoid infinite loop

// while still need more special characters and not in infinite
// loop
while (count > 0 && loopGuard-- > 0)
{
insertPosition = (byte) (insertPosition % clearPass.length());
specialCharacterPosition =
(byte) (specialCharacterPosition % specialChars.length());
int mapChunkLength = SHA.SHA_BYTES_RESULT / length;

// if can ensure different special characters
if (specialChars.length() >= getSpecialCharactersCount())
{
// find special character not used
do
{
if (clearPass.toString().indexOf(
specialChars.charAt(specialCharacterPosition)) == -1) break;

// get next special character position
specialCharacterPosition =
(byte) (++specialCharacterPosition % specialChars.length());
}
while (true);
}
for (int i = 0; i < length; ++i)
{
int currIdx = i * mapChunkLength;
password.append(alphabeta.charAt(Utilities
.hexToInt(passwordHash.substring(currIdx, currIdx + mapChunkLength))
.mod(new BigInteger(Integer.toString(alphabeta.length()))).intValue()));
}

Logger.printDebug("use special characters count = " + count
+ "; insertPosition = " + insertPosition);
Logger.printDebug("Password generation. STAGE 1. DONE");

if (Character.isDigit(clearPass.charAt(insertPosition)))
{
Logger.printDebug(password.toString());

clearPass.setCharAt(insertPosition,
specialChars.charAt(specialCharacterPosition));
count--;
/*
* 2. Stage - Result of Stage 1 (appropriate length password) will be
* modified with special characters.
*/
if (paramsMask.get(ParamsMaskBits.HAS_SPECIAL_CHARACTERS.ordinal())
&& specialChars.length() != 0)
{
Logger.printDebug("Password generation. STAGE 2. START");

specialCharacterPosition = (byte) hash.charAt(idx--);
insertPosition = (byte) hash.charAt(idx--);
loopGuard = clearPass.length();
}
else
insertPosition++;
}
}
int count = getSpecialCharactersCount();
int specialCharacterPosition = specialHash.charAt(idx++);
int insertPosition = specialHash.charAt(idx++);

// set CAPITALS.
if (paramsMask.get(ParamsMaskBits.HAS_CAPITALS.ordinal()))
while (count > 0)
{
Logger.printDebug("use capitals");
// determine what chars changes case
boolean changeCase = (hash.charAt(hash.length() - 1) & 0x01) != 0;
// get string with capitals for easier processing
String capsString = (new String(clearPass)).toUpperCase();
insertPosition = (insertPosition % password.length());
specialCharacterPosition = (specialCharacterPosition % specialChars.length());

for (int i = 0; i < clearPass.length(); i++)
if (specialChars.length() >= getSpecialCharactersCount())
{
// different chars means different case
if (clearPass.charAt(i) != capsString.charAt(i))
while (password.toString().indexOf(
specialChars.charAt(specialCharacterPosition)) != -1)
{
if (changeCase)
{
clearPass.setCharAt(i, capsString.charAt(i));
}

changeCase = !changeCase;
specialCharacterPosition =
(++specialCharacterPosition % specialChars.length());
}
}

Logger.printDebug("Special characters count left = " + count
+ "; insertPosition = " + insertPosition);

password.setCharAt(insertPosition, specialChars.charAt(specialCharacterPosition));
count--;

specialCharacterPosition = specialHash.charAt(idx++);
insertPosition = specialHash.charAt(idx++);
}

return clearPass.toString();
Logger.printDebug("Password generation. STAGE 2. DONE");
}
catch (Exceptions e)

/*
* 3. Stage - Final stage where random characters will be capitalized.
*/
if (paramsMask.get(ParamsMaskBits.HAS_CAPITALS.ordinal()))
{
Terminator.terminate(e);
Logger.printDebug("Password generation. STAGE 3. START");

Logger.printDebug("use capitals");
boolean changeCase = (specialHash.charAt(idx++) & 0x01) != 0;
String capsString = (new String(password)).toUpperCase();

for (int i = 0; i < password.length(); i++)
{
if (password.charAt(i) != capsString.charAt(i))
{
if (changeCase)
{
password.setCharAt(i, capsString.charAt(i));
}
changeCase = !changeCase;
}
}
}
return "";
Logger.printDebug("Password generation. STAGE 3. DONE");

return password.toString();
}

public String getPassword()
Expand Down
Loading

0 comments on commit a4753bc

Please sign in to comment.