Author: jflesch
Date: 2007-04-21 22:08:04 +0000 (Sat, 21 Apr 2007)
New Revision: 12848
Modified:
trunk/apps/Thaw/src/thaw/plugins/Signatures.java
trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java
trunk/apps/Thaw/src/thaw/plugins/signatures/SigConfigTab.java
Log:
Thaw can now generate and delete identities (but there is a focus problem with
the dialogs)
Modified: trunk/apps/Thaw/src/thaw/plugins/Signatures.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/Signatures.java 2007-04-21 21:09:20 UTC
(rev 12847)
+++ trunk/apps/Thaw/src/thaw/plugins/Signatures.java 2007-04-21 22:08:04 UTC
(rev 12848)
@@ -47,6 +47,9 @@
db =
(Hsqldb)core.getPluginManager().getPlugin("thaw.plugins.Hsqldb");
db.registerChild(this);
+ DatabaseManager.init(db, core.getConfig(),
+ core.getSplashScreen());
+
configTab = new SigConfigTab(core.getConfigWindow(), db);
core.getConfigWindow().addTab(I18n.getMessage("thaw.plugin.signature.signatures"),
Modified: trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java 2007-04-21
21:09:20 UTC (rev 12847)
+++ trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java 2007-04-21
22:08:04 UTC (rev 12848)
@@ -2,13 +2,24 @@
import java.awt.Color;
+import java.security.MessageDigest;
+import java.sql.*;
+
+import java.util.Vector;
+import java.util.Iterator;
+
+import freenet.crypt.SHA256;
+import freenet.support.Base64;
+
import freenet.crypt.DSAPrivateKey;
import freenet.crypt.DSAGroup;
import freenet.crypt.DSAPublicKey;
import freenet.crypt.DSASignature;
import freenet.crypt.Yarrow;
+import freenet.crypt.Global;
+import thaw.core.Logger;
import thaw.plugins.Hsqldb;
@@ -16,11 +27,11 @@
public final static int[] trustLevelInt = {
100,
- 2,
+ 10,
1,
0,
-1,
- -2
+ -10
};
public final static String[] trustLevelStr = {
@@ -55,39 +66,110 @@
/* private key */
private byte[] x;
+ private boolean isDup;
+ private int trustLevel;
+
+ private String hash;
+
+
+ private Identity() {
+ }
+
public Identity(Hsqldb db, int id, String nick,
- byte[] y, byte[] x) {
+ byte[] y, byte[] x,
+ boolean isDup,
+ int trustLevel) {
+ this.db = db;
+ this.id = id;
+ this.nick = nick;
+ this.y = y;
+ this.x = x;
+ this.isDup = isDup;
+ this.trustLevel = trustLevel;
+ MessageDigest md = SHA256.getMessageDigest();
+ md.reset();
+ md.update(y);
+
+ hash = Base64.encode(md.digest());
}
/**
* Generate a new identity
* you have to insert() it
+ * @param db just here to fill in the class
*/
- public static Identity generate(String nick) {
- Yarrow y = new Yarrow();
+ public static Identity generate(Hsqldb db, String nick) {
+ Logger.info(nick, "thaw.plugins.signatures.Identity :
Generating new identity ...");
- return null;
+ Yarrow randomSource = new Yarrow();
+
+ DSAPrivateKey privateKey = new
DSAPrivateKey(Global.DSAgroupBigA, randomSource);
+ DSAPublicKey publicKey = new DSAPublicKey(Global.DSAgroupBigA,
privateKey);
+
+ Identity identity = new Identity(db, -1, nick,
+ publicKey.getY().toByteArray(),
+
privateKey.getX().toByteArray(),
+ false,
+ 10);
+
+
+ Logger.info(identity, "done");
+
+ return identity;
}
+ /**
+ * id won't be set
+ */
public void insert() {
+ try {
+ synchronized(db.dbLock) {
+ PreparedStatement st;
+ st =
db.getConnection().prepareStatement("INSERT INTO signatures (nickName, y, x,
isDup, trustLevel) "+
+
"VALUES (?, ?, ?, ?, ?)");
+ st.setString(1, nick);
+ st.setBytes(2, y);
+ st.setBytes(3, x);
+ st.setBoolean(4, isDup);
+ st.setInt(5, trustLevel);
+
+ st.execute();
+ }
+ } catch(SQLException e) {
+ Logger.error(this, "Exception while adding the identity
to the bdd: "+e.toString());
+ }
}
+ public void delete() {
+ try {
+ synchronized(db.dbLock) {
+ PreparedStatement st;
+ st =
db.getConnection().prepareStatement("DELETE FROM signatures "+
+ "WHERE
id = ?");
+ st.setInt(1, id);
+
+ st.execute();
+ }
+ } catch(SQLException e) {
+ Logger.error(this, "Exception while deleting the
identity from the bdd: "+e.toString());
+ }
+
+ }
+
+
/**
* All the parameters are Base64 encoded, except text.
*/
public static boolean isValid(String text, /* signed text */
String r, /* sig */
String s, /* sig */
- String p, /* publicKey */
- String q, /* publicKey */
- String g, /* publicKey */
String y) /* publicKey */ {
return true;
}
@@ -101,5 +183,49 @@
}
+
+ public String toString() {
+ return nick+"@"+hash;
+ }
+
+
+ public static Vector getIdentities(Hsqldb db, String cond) {
+ try {
+ synchronized(db.dbLock) {
+ Vector v = new Vector();
+
+ PreparedStatement st;
+
+ st =
db.getConnection().prepareStatement("SELECT id, nickName, y, x, isDup,
trustLevel FROM signatures WHERE "+cond + " ORDER BY nickName");
+ ResultSet set = st.executeQuery();
+
+ while(set.next()) {
+ v.add(new Identity(db,
+ set.getInt("id"),
+
set.getString("nickName"),
+ set.getBytes("y"),
+ set.getBytes("x"),
+
set.getBoolean("isDup"),
+
set.getInt("trustLevel")));
+ }
+
+ return v;
+ }
+ } catch(SQLException e) {
+ Logger.error(new Identity(), "Error while getting
identities (1): "+e.toString());
+ }
+
+ return null;
+ }
+
+
+ public static Vector getYourIdentities(Hsqldb db) {
+ return getIdentities(db, "x IS NOT NULL");
+ }
+
+
+ public static Vector getOtherIdentities(Hsqldb db) {
+ return getIdentities(db, "x IS NULL");
+ }
}
Modified: trunk/apps/Thaw/src/thaw/plugins/signatures/SigConfigTab.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/signatures/SigConfigTab.java
2007-04-21 21:09:20 UTC (rev 12847)
+++ trunk/apps/Thaw/src/thaw/plugins/signatures/SigConfigTab.java
2007-04-21 22:08:04 UTC (rev 12848)
@@ -110,26 +110,35 @@
dialog.getContentPane().add(southPanel,
BorderLayout.SOUTH);
- dialog.setSize(500, 500);
+ dialog.setSize(500, 300);
dialog.setVisible(true);
}
public void updateList() {
-
+ list.setListData(Identity.getYourIdentities(db));
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addIdentity) {
String nick =
JOptionPane.showInputDialog(dialog,
-
I18n.getMessage("thaw.plugin.signature.enterNick"));
- Identity id = Identity.generate(nick);
- id.insert();
- updateList();
+
I18n.getMessage("thaw.plugin.signature.enterNick"),
+
I18n.getMessage("thaw.plugin.signature.enterNick"),
+
JOptionPane.QUESTION_MESSAGE);
+
+ if (nick != null) {
+ Identity id = Identity.generate(db,
nick);
+ id.insert();
+ updateList();
+ }
}
if (e.getSource() == removeIdentity) {
-
+ Identity i = (Identity)list.getSelectedValue();
+ if (i != null) {
+ i.delete();
+ updateList();
+ }
}
if (e.getSource() == closeWindow) {