From a4753bc36e46b3cff47dca3f59e8418d38d1aaf8 Mon Sep 17 00:00:00 2001 From: lyubick Date: Sun, 12 Apr 2015 14:27:40 +0300 Subject: [PATCH] NEW. Password is now more variative :) --- src/cryptosystem/CryptoSystem.java | 5 + src/db/PasswordCollection.java | 12 -- src/db/SpecialPassword.java | 232 ++++++++++++----------------- src/sha/SHA.java | 80 +++++----- src/test/Test.java | 16 +- src/utilities/Utilities.java | 27 ++++ 6 files changed, 186 insertions(+), 186 deletions(-) diff --git a/src/cryptosystem/CryptoSystem.java b/src/cryptosystem/CryptoSystem.java index c18fd6e..527fa14 100644 --- a/src/cryptosystem/CryptoSystem.java +++ b/src/cryptosystem/CryptoSystem.java @@ -219,4 +219,9 @@ public String getPassword(long cycles, String pwdName, Task passwordCalcul return Utilities.bytesToHex(tmp); } + + public String getHash(String toHash, String salt) + { + return sha.getStringSHA512((toHash + salt).getBytes()); + } } diff --git a/src/db/PasswordCollection.java b/src/db/PasswordCollection.java index 57b3008..6afd2a2 100644 --- a/src/db/PasswordCollection.java +++ b/src/db/PasswordCollection.java @@ -7,7 +7,6 @@ import utilities.Utilities; import cryptosystem.CryptoSystem; -import logger.Logger; import main.Exceptions; import main.Terminator; import main.Exceptions.XC; @@ -88,17 +87,6 @@ public ObservableList 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 save() { Task saveTask = new Task() diff --git a/src/db/SpecialPassword.java b/src/db/SpecialPassword.java index 8ba2dda..65aadad 100644 --- a/src/db/SpecialPassword.java +++ b/src/db/SpecialPassword.java @@ -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; @@ -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 @@ -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; @@ -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() @@ -199,99 +151,113 @@ private byte getSpecialCharactersCount() public String getPassword(Task 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() diff --git a/src/sha/SHA.java b/src/sha/SHA.java index 7622cd2..c16c7a2 100644 --- a/src/sha/SHA.java +++ b/src/sha/SHA.java @@ -10,46 +10,46 @@ public class SHA { - private final int SHA_BYTES_LENGTH = 128; - private final int SHA_BYTES_RESULT = 64; - private final int SHA_STATES_COUNT = 8; - private final int SHA_WORDS_COUNT = 80; - private final int SHA_INIT_WORDS = 16; - private final long LONG_BITS_COUNT = 64; - final long[] initialHashValue = - { 0x6a09e667f3bcc908L, 0xbb67ae8584caa73bL, - 0x3c6ef372fe94f82bL, 0xa54ff53a5f1d36f1L, 0x510e527fade682d1L, 0x9b05688c2b3e6c1fL, - 0x1f83d9abfb41bd6bL, 0x5be0cd19137e2179L }; - - final long[] roundConstants = - { 0x428a2f98d728ae22L, 0x7137449123ef65cdL, - 0xb5c0fbcfec4d3b2fL, 0xe9b5dba58189dbbcL, 0x3956c25bf348b538L, 0x59f111f1b605d019L, - 0x923f82a4af194f9bL, 0xab1c5ed5da6d8118L, 0xd807aa98a3030242L, 0x12835b0145706fbeL, - 0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L, 0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L, - 0x9bdc06a725c71235L, 0xc19bf174cf692694L, 0xe49b69c19ef14ad2L, 0xefbe4786384f25e3L, - 0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L, 0x2de92c6f592b0275L, 0x4a7484aa6ea6e483L, - 0x5cb0a9dcbd41fbd4L, 0x76f988da831153b5L, 0x983e5152ee66dfabL, 0xa831c66d2db43210L, - 0xb00327c898fb213fL, 0xbf597fc7beef0ee4L, 0xc6e00bf33da88fc2L, 0xd5a79147930aa725L, - 0x06ca6351e003826fL, 0x142929670a0e6e70L, 0x27b70a8546d22ffcL, 0x2e1b21385c26c926L, - 0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL, 0x650a73548baf63deL, 0x766a0abb3c77b2a8L, - 0x81c2c92e47edaee6L, 0x92722c851482353bL, 0xa2bfe8a14cf10364L, 0xa81a664bbc423001L, - 0xc24b8b70d0f89791L, 0xc76c51a30654be30L, 0xd192e819d6ef5218L, 0xd69906245565a910L, - 0xf40e35855771202aL, 0x106aa07032bbd1b8L, 0x19a4c116b8d2d0c8L, 0x1e376c085141ab53L, - 0x2748774cdf8eeb99L, 0x34b0bcb5e19b48a8L, 0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL, - 0x5b9cca4f7763e373L, 0x682e6ff3d6b2b8a3L, 0x748f82ee5defb2fcL, 0x78a5636f43172f60L, - 0x84c87814a1f0ab72L, 0x8cc702081a6439ecL, 0x90befffa23631e28L, 0xa4506cebde82bde9L, - 0xbef9a3f7b2c67915L, 0xc67178f2e372532bL, 0xca273eceea26619cL, 0xd186b8c721c0c207L, - 0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L, 0x06f067aa72176fbaL, 0x0a637dc5a2c898a6L, - 0x113f9804bef90daeL, 0x1b710b35131c471bL, 0x28db77f523047d84L, 0x32caab7b40c72493L, - 0x3c9ebe0a15c9bebcL, 0x431d67c49c100d4cL, 0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL, - 0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L }; - - private final long[] state = new long[SHA_STATES_COUNT]; - private byte[] MessageBlock = null; - private final byte[] hashBlock = new byte[SHA_BYTES_LENGTH]; - private int hashBlocksCount = 0; - - private final long TEN_LESS_SIGNIFICANT_BITS_MASK = 0x3FFFF; + private final int SHA_BYTES_LENGTH = 128; + public static final int SHA_BYTES_RESULT = 64; + private final int SHA_STATES_COUNT = 8; + private final int SHA_WORDS_COUNT = 80; + private final int SHA_INIT_WORDS = 16; + private final long LONG_BITS_COUNT = 64; + final long[] initialHashValue = + { 0x6a09e667f3bcc908L, + 0xbb67ae8584caa73bL, 0x3c6ef372fe94f82bL, 0xa54ff53a5f1d36f1L, 0x510e527fade682d1L, + 0x9b05688c2b3e6c1fL, 0x1f83d9abfb41bd6bL, 0x5be0cd19137e2179L }; + + final long[] roundConstants = + { 0x428a2f98d728ae22L, + 0x7137449123ef65cdL, 0xb5c0fbcfec4d3b2fL, 0xe9b5dba58189dbbcL, 0x3956c25bf348b538L, + 0x59f111f1b605d019L, 0x923f82a4af194f9bL, 0xab1c5ed5da6d8118L, 0xd807aa98a3030242L, + 0x12835b0145706fbeL, 0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L, 0x72be5d74f27b896fL, + 0x80deb1fe3b1696b1L, 0x9bdc06a725c71235L, 0xc19bf174cf692694L, 0xe49b69c19ef14ad2L, + 0xefbe4786384f25e3L, 0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L, 0x2de92c6f592b0275L, + 0x4a7484aa6ea6e483L, 0x5cb0a9dcbd41fbd4L, 0x76f988da831153b5L, 0x983e5152ee66dfabL, + 0xa831c66d2db43210L, 0xb00327c898fb213fL, 0xbf597fc7beef0ee4L, 0xc6e00bf33da88fc2L, + 0xd5a79147930aa725L, 0x06ca6351e003826fL, 0x142929670a0e6e70L, 0x27b70a8546d22ffcL, + 0x2e1b21385c26c926L, 0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL, 0x650a73548baf63deL, + 0x766a0abb3c77b2a8L, 0x81c2c92e47edaee6L, 0x92722c851482353bL, 0xa2bfe8a14cf10364L, + 0xa81a664bbc423001L, 0xc24b8b70d0f89791L, 0xc76c51a30654be30L, 0xd192e819d6ef5218L, + 0xd69906245565a910L, 0xf40e35855771202aL, 0x106aa07032bbd1b8L, 0x19a4c116b8d2d0c8L, + 0x1e376c085141ab53L, 0x2748774cdf8eeb99L, 0x34b0bcb5e19b48a8L, 0x391c0cb3c5c95a63L, + 0x4ed8aa4ae3418acbL, 0x5b9cca4f7763e373L, 0x682e6ff3d6b2b8a3L, 0x748f82ee5defb2fcL, + 0x78a5636f43172f60L, 0x84c87814a1f0ab72L, 0x8cc702081a6439ecL, 0x90befffa23631e28L, + 0xa4506cebde82bde9L, 0xbef9a3f7b2c67915L, 0xc67178f2e372532bL, 0xca273eceea26619cL, + 0xd186b8c721c0c207L, 0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L, 0x06f067aa72176fbaL, + 0x0a637dc5a2c898a6L, 0x113f9804bef90daeL, 0x1b710b35131c471bL, 0x28db77f523047d84L, + 0x32caab7b40c72493L, 0x3c9ebe0a15c9bebcL, 0x431d67c49c100d4cL, 0x4cc5d4becb3e42b6L, + 0x597f299cfc657e2aL, 0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L }; + + private final long[] state = new long[SHA_STATES_COUNT]; + private byte[] MessageBlock = null; + private final byte[] hashBlock = new byte[SHA_BYTES_LENGTH]; + private int hashBlocksCount = 0; + + private final long TEN_LESS_SIGNIFICANT_BITS_MASK = 0x3FFFF; public SHA() throws Exceptions { diff --git a/src/test/Test.java b/src/test/Test.java index 19c2651..b38eb6f 100644 --- a/src/test/Test.java +++ b/src/test/Test.java @@ -7,6 +7,7 @@ // explicit variable initialization missing // functions and file missing headers +import java.util.BitSet; import java.util.Vector; import cryptosystem.CryptoSystem; @@ -18,6 +19,7 @@ import utilities.Utilities; import db.SpecialPassword; import db.UserFileIO; +import db.SpecialPassword.ParamsMaskBits; /** * @author lyubick @@ -80,7 +82,19 @@ public static boolean TestSHA() public static boolean TestSerialization() { - SpecialPassword sp = new SpecialPassword(); + SpecialPassword sp = null; + try + { + BitSet paramsMask = new BitSet(ParamsMaskBits.TOTAL_COUNT.ordinal()); + paramsMask.set(0, ParamsMaskBits.TOTAL_COUNT.ordinal()); + + sp = new SpecialPassword("name", "comment", "url", 16, paramsMask, "@!=-%#"); + } + catch (Exceptions e1) + { + // TODO Auto-generated catch block + e1.printStackTrace(); + } SpecialPassword sp1 = null; SpecialPassword sp2 = null; diff --git a/src/utilities/Utilities.java b/src/utilities/Utilities.java index 4667425..4e103e1 100644 --- a/src/utilities/Utilities.java +++ b/src/utilities/Utilities.java @@ -12,6 +12,7 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.PrintWriter; +import java.math.BigInteger; import java.util.Vector; import logger.Logger; @@ -166,4 +167,30 @@ public static Vector readFromFile(String fileName) throws Exceptions return inLines; } + public static BigInteger hexToInt(String hexValue) + { + BigInteger out = new BigInteger("0"); + BigInteger curr = null; + BigInteger power = new BigInteger("16"); + + int i = hexValue.length() - 1; + int pwr = 0; + + for (; i >= 0; --i) + { + Logger.printDebug("HEX STRING: " + hexValue.substring(i, i + 1)); + + curr = new BigInteger(hexValue.substring(i, i + 1), 16); + + Logger.printDebug("BI CURR: " + curr.toString()); + + Logger.printDebug("BI POWER: " + power.toString() + ", " + pwr); + + out = out.add(curr.multiply(power.pow(pwr++))); + + Logger.printDebug("BI OUT: " + out.toString()); + } + + return out; + } }