Skip to content

Commit

Permalink
base64 no longer shuffled, added pin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
alexyz committed Nov 2, 2019
1 parent c40326a commit 1e583e4
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 27 deletions.
6 changes: 2 additions & 4 deletions src/pwgen/BitsPasswordJPanel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package pwgen;

import java.util.Collections;
import java.util.prefs.Preferences;

import javax.swing.*;
Expand All @@ -22,15 +21,15 @@ public BitsPasswordJPanel () {
protected void loadPrefs () throws Exception {
System.out.println("load prefs");
Preferences prefs = Preferences.userNodeForPackage(getClass());
bitsSpinner.setValue(Integer.valueOf(prefs.getInt("btbits", 64)));
bitsSpinner.setValue(prefs.getInt("btbits", 64));
b64CheckBox.setSelected(prefs.getBoolean("btb64", true));
}

@Override
protected void savePrefs () throws Exception {
System.out.println("save prefs");
Preferences prefs = Preferences.userNodeForPackage(getClass());
prefs.putInt("btbits", ((Number) bitsSpinner.getValue()).intValue());
prefs.putInt("btbits", intValue(bitsSpinner));
prefs.putBoolean("btb64", b64CheckBox.isSelected());
prefs.flush();
}
Expand All @@ -40,7 +39,6 @@ public void generate () {
StringBuilder sb = new StringBuilder();
int b = intValue(bitsSpinner);
sb.append(bits(b, b64CheckBox.isSelected()));
Collections.shuffle(new StringBuilderList(sb), RANDOM);
setValue(sb.toString(), Math.pow(2, b));
}
}
10 changes: 5 additions & 5 deletions src/pwgen/CharPasswordJPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ protected void loadPrefs () throws Exception {
protected void savePrefs () throws Exception {
System.out.println("save prefs");
Preferences prefs = Preferences.userNodeForPackage(getClass());
prefs.putInt("chupper", (Integer) upperSpinner.getValue());
prefs.putInt("chlower", (Integer) lowerSpinner.getValue());
prefs.putInt("chdigit", (Integer) digitSpinner.getValue());
prefs.putInt("chpunct", (Integer) punctSpinner.getValue());
prefs.putInt("chany", (Integer) anySpinner.getValue());
prefs.putInt("chupper", intValue(upperSpinner));
prefs.putInt("chlower", intValue(lowerSpinner));
prefs.putInt("chdigit", intValue(digitSpinner));
prefs.putInt("chpunct", intValue(punctSpinner));
prefs.putInt("chany", intValue(anySpinner));
prefs.putBoolean("chshuf", shuffleBox.isSelected());
prefs.flush();
}
Expand Down
18 changes: 9 additions & 9 deletions src/pwgen/DictPasswordJPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class DictPasswordJPanel extends PasswordJPanel {
public int compare(List<Integer> l1, List<Integer> l2) {
int c = l1.size() - l2.size();
for (int n = 0; c == 0 && n < l1.size(); n++) {
c = l1.get(Integer.valueOf(n)) - l2.get(Integer.valueOf(n));
c = l1.get(n) - l2.get(n);
}
return c;
}
Expand Down Expand Up @@ -73,7 +73,7 @@ private void loadfile () {
// try to avoid poor quality words
l = l.trim().toLowerCase();
if (p.matcher(l).matches() && l.length() >= MIN && l.length() <= MAX) {
dictset.computeIfAbsent(l.length(), k -> new TreeSet<>()).add(l);
dictset.computeIfAbsent(Integer.valueOf(l.length()), k -> new TreeSet<>()).add(l);
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -105,9 +105,9 @@ protected void loadPrefs () {
@Override
protected void savePrefs () throws Exception {
Preferences prefs = Preferences.userNodeForPackage(getClass());
prefs.putInt("dclen", (Integer) wordSpinner.getValue());
prefs.putInt("dcdigit", (Integer) digitSpinner.getValue());
prefs.putInt("dcpunct", (Integer) punctSpinner.getValue());
prefs.putInt("dclen", intValue(wordSpinner));
prefs.putInt("dcdigit", intValue(digitSpinner));
prefs.putInt("dcpunct", intValue(punctSpinner));
prefs.putBoolean("dctitle", titleBox.isSelected());
prefs.putBoolean("dcshuf", shuffleBox.isSelected());
if (file != null) {
Expand All @@ -134,18 +134,18 @@ private Set<List<Integer>> partitions(int n, int s) {
private List<Integer> join (List<Integer> l, int i) {
List<Integer> l2 = new ArrayList<>();
l2.addAll(l);
l2.add(i);
l2.add(Integer.valueOf(i));
Collections.sort(l2);
return l2;
}

private Long dictproduct(List<Integer> x) {
private long dictproduct(List<Integer> x) {
long v = 1;
for (Integer i : x) {
List<String> s = dict.get(i);
v = v * (s != null ? s.size() : 0);
}
return Long.valueOf(v);
return v;
}

private long sum (List<Long> l) {
Expand Down Expand Up @@ -177,7 +177,7 @@ public void generate () {
//System.out.println("parts(" + n + ")=" + pset);
for (List<Integer> p : pset) {
plist.add(p);
pcounts.add(dictproduct(p));
pcounts.add(Long.valueOf(dictproduct(p)));
//System.out.println("part=" + p + " prod(p)=" + dictproduct(p));
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/pwgen/PasswordGenJFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class PasswordGenJFrame extends JFrame {
private final WordPasswordJPanel wordPassPanel = new WordPasswordJPanel();
private final DictPasswordJPanel dictPassPanel = new DictPasswordJPanel();
private final BitsPasswordJPanel bitsPassPanel = new BitsPasswordJPanel();
private final PinPasswordJPanel pinPassPanel = new PinPasswordJPanel();

public static void main (String args[]) {
PasswordGenJFrame f = new PasswordGenJFrame();
Expand All @@ -37,6 +38,7 @@ public void windowClosing (WindowEvent ae) {
tabs.add("Word", wordPassPanel);
tabs.add("Dictionary", dictPassPanel);
tabs.add("Bits", bitsPassPanel);
tabs.add("PIN", pinPassPanel);

loadPrefs();

Expand All @@ -52,6 +54,7 @@ private void loadPrefs () {
wordPassPanel.loadPrefs();
dictPassPanel.loadPrefs();
bitsPassPanel.loadPrefs();
pinPassPanel.loadPrefs();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.toString());
}
Expand All @@ -66,6 +69,7 @@ private void savePrefs () {
wordPassPanel.savePrefs();
dictPassPanel.savePrefs();
bitsPassPanel.savePrefs();
pinPassPanel.savePrefs();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.toString());
}
Expand Down
73 changes: 73 additions & 0 deletions src/pwgen/PinPasswordJPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package pwgen;

import javax.swing.*;
import java.util.Collections;
import java.util.prefs.Preferences;

import static pwgen.PwUtil.*;

public class PinPasswordJPanel extends PasswordJPanel {

private final JSpinner digitSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 99, 1));
private final JCheckBox shuffleBox = new JCheckBox("Shuffle");
private final JCheckBox uniqueBox = new JCheckBox("Unique");

public PinPasswordJPanel() {
optionPanel.add(new JLabel("Digit"));
optionPanel.add(digitSpinner);
optionPanel.add(shuffleBox);
optionPanel.add(uniqueBox);
}

@Override
protected void loadPrefs () throws Exception {
System.out.println("load prefs");
Preferences prefs = Preferences.userNodeForPackage(getClass());
digitSpinner.setValue(prefs.getInt("pindigit", 6));
shuffleBox.setSelected(prefs.getBoolean("pinshuf", true));
uniqueBox.setSelected(prefs.getBoolean("pinuniq", true));
}

@Override
protected void savePrefs () throws Exception {
System.out.println("save prefs");
Preferences prefs = Preferences.userNodeForPackage(getClass());
prefs.putInt("pindigit", intValue(digitSpinner));
prefs.putBoolean("pinshuf", shuffleBox.isSelected());
prefs.putBoolean("pinuniq", uniqueBox.isSelected());
prefs.flush();
}

@Override
public void generate () {
int d = intValue(digitSpinner);
StringBuilderList sbl = new StringBuilderList(new StringBuilder());

if (uniqueBox.isSelected()) {
if (d > 10) {
JOptionPane.showMessageDialog(this, "too many digits for unique");
return;
}
// add all digits then remove
for (int n = 0; n < 10; n++) {
sbl.sb.append(n);
}
Collections.shuffle(sbl);
while (sbl.size() > d) {
sbl.remove(0);
}
} else {
for (int n = 0; n < d; n++) {
sbl.sb.append(RANDOM.nextInt(10));
}
}

if (shuffleBox.isSelected()) {
Collections.shuffle(sbl);
} else {
Collections.sort(sbl);
}

setValue(sbl.sb.toString(), 0);
}
}
1 change: 1 addition & 0 deletions src/pwgen/PwUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class PwUtil {
protected static final int PUNCT = punctCount();
protected static final int ANY = 126 - 33;

/** int value from spinner */
public static int intValue(JSpinner s) {
return ((Number) s.getValue()).intValue();
}
Expand Down
19 changes: 16 additions & 3 deletions src/pwgen/StringBuilderList.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,32 @@
/** list wrapper for stringbuilder */
class StringBuilderList extends AbstractList<Character> {

private final StringBuilder sb;
public final StringBuilder sb;

public StringBuilderList (StringBuilder sb) {
this.sb = sb;
}


@Override
public boolean add(Character character) {
sb.append(character.charValue());
return true;
}

@Override
public Character set (int index, Character element) {
char c = sb.charAt(index);
sb.setCharAt(index, element);
return c;
}


@Override
public Character remove(int index) {
Character c = sb.charAt(index);
sb.deleteCharAt(index);
return c;
}

@Override
public Character get (int index) {
return sb.charAt(index);
Expand Down
21 changes: 15 additions & 6 deletions src/pwgen/WordPasswordJPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class WordPasswordJPanel extends PasswordJPanel {

private final JSpinner wordSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 99, 1));
private final JSpinner digitSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 99, 1));
private final JSpinner punctSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 99, 1));
private final JCheckBox titleBox = new JCheckBox("Title");
private final JCheckBox shuffleBox = new JCheckBox("Shuffle");

Expand All @@ -21,6 +22,8 @@ public WordPasswordJPanel () {
optionPanel.add(wordSpinner);
optionPanel.add(new JLabel("Digit"));
optionPanel.add(digitSpinner);
optionPanel.add(new JLabel("Punct"));
optionPanel.add(punctSpinner);
optionPanel.add(titleBox);
optionPanel.add(shuffleBox);
}
Expand All @@ -32,29 +35,35 @@ protected void loadPrefs () {
digitSpinner.setValue(prefs.getInt("wddigit", 1));
titleBox.setSelected(prefs.getBoolean("wdtitle", true));
shuffleBox.setSelected(prefs.getBoolean("wdshuf", false));
punctSpinner.setValue(prefs.getInt("wdpunct", 1));
}

@Override
protected void savePrefs () throws Exception {
Preferences prefs = Preferences.userNodeForPackage(getClass());
prefs.putInt("wdwords", (Integer) wordSpinner.getValue());
prefs.putInt("wddigit", (Integer) digitSpinner.getValue());
prefs.putInt("wdwords", intValue(wordSpinner));
prefs.putInt("wddigit", intValue(digitSpinner));
prefs.putBoolean("wdtitle", titleBox.isSelected());
prefs.putBoolean("wdshuf", shuffleBox.isSelected());
prefs.putInt("wdpunct", intValue(punctSpinner));
prefs.sync();
}

@Override
public void generate () {
List<String> list = new ArrayList<>();
int word = ((Integer) wordSpinner.getValue()).intValue();
int digit = ((Integer) digitSpinner.getValue()).intValue();
int w = intValue(wordSpinner);
int d = intValue(digitSpinner);
int p = intValue(punctSpinner);

list.add(word(word, titleBox.isSelected()));
list.add(digit(digit));
list.add(word(w, titleBox.isSelected()));
list.add(digit(d));
list.add(punct(p));

if (shuffleBox.isSelected()) {
Collections.shuffle(list, RANDOM);
}

StringBuilder sb = new StringBuilder();
for (String s : list) {
sb.append(s);
Expand Down

0 comments on commit 1e583e4

Please sign in to comment.