Author: nextgens
Date: 2008-01-27 13:32:37 +0000 (Sun, 27 Jan 2008)
New Revision: 17317
Modified:
trunk/freenet/src/freenet/crypt/DSAPublicKey.java
Log:
indent
Modified: trunk/freenet/src/freenet/crypt/DSAPublicKey.java
===================================================================
--- trunk/freenet/src/freenet/crypt/DSAPublicKey.java 2008-01-27 12:30:24 UTC
(rev 17316)
+++ trunk/freenet/src/freenet/crypt/DSAPublicKey.java 2008-01-27 13:32:37 UTC
(rev 17317)
@@ -14,85 +14,85 @@
import freenet.support.SimpleFieldSet;
public class DSAPublicKey extends CryptoKey implements StorableBlock {
- private static final long serialVersionUID = -1;
-
- private final BigInteger y;
-
- public static final int PADDED_SIZE = 1024;
+ private static final long serialVersionUID = -1;
+ private final BigInteger y;
+ public static final int PADDED_SIZE = 1024;
public static final int HASH_LENGTH = 32;
-
- private final DSAGroup group;
-
+ private final DSAGroup group;
private byte[] fingerprint = null;
-
- public DSAPublicKey(DSAGroup g, BigInteger y) {
- if(y.signum() != 1) throw new IllegalArgumentException();
- this.y=y;
- this.group=g;
+
+ public DSAPublicKey(DSAGroup g, BigInteger y) {
+ if(y.signum() != 1)
+ throw new IllegalArgumentException();
+ this.y = y;
+ this.group = g;
if(y.compareTo(g.getP()) > 0)
- throw new IllegalArgumentException("y must be < p but
y="+y+" p="+g.getP());
- if(g == null) throw new NullPointerException();
- }
-
- /**
+ throw new IllegalArgumentException("y must be < p but
y=" + y + " p=" + g.getP());
+ if(g == null)
+ throw new NullPointerException();
+ }
+
+ /**
* Use this constructor if you have a Hex:ed version of y already
* available, will save some conversions and string allocations.
*/
public DSAPublicKey(DSAGroup g, String yAsHexString) throws
NumberFormatException {
- this.y=new NativeBigInteger(yAsHexString,16);
- if(y.signum() != 1) throw new IllegalArgumentException();
- this.group=g;
- if(g == null) throw new NullPointerException();
+ this.y = new NativeBigInteger(yAsHexString, 16);
+ if(y.signum() != 1)
+ throw new IllegalArgumentException();
+ this.group = g;
+ if(g == null)
+ throw new NullPointerException();
}
- public DSAPublicKey(DSAGroup g, DSAPrivateKey p) {
- this(g,g.getG().modPow(p.getX(), g.getP()));
- }
+ public DSAPublicKey(DSAGroup g, DSAPrivateKey p) {
+ this(g, g.getG().modPow(p.getX(), g.getP()));
+ }
- public DSAPublicKey(InputStream is) throws IOException,
CryptFormatException {
- group=(DSAGroup) DSAGroup.read(is);
- y=Util.readMPI(is);
+ public DSAPublicKey(InputStream is) throws IOException,
CryptFormatException {
+ group = (DSAGroup) DSAGroup.read(is);
+ y = Util.readMPI(is);
if(y.compareTo(group.getP()) > 0)
- throw new IllegalArgumentException("y must be < p but
y="+y+" p="+group.getP());
- }
-
- public DSAPublicKey(byte[] pubkeyBytes) throws IOException,
CryptFormatException {
- this(new ByteArrayInputStream(pubkeyBytes));
+ throw new IllegalArgumentException("y must be < p but
y=" + y + " p=" + group.getP());
}
+ public DSAPublicKey(byte[] pubkeyBytes) throws IOException,
CryptFormatException {
+ this(new ByteArrayInputStream(pubkeyBytes));
+ }
+
public static DSAPublicKey create(byte[] pubkeyAsBytes) throws
CryptFormatException {
- try {
+ try {
return new DSAPublicKey(new
ByteArrayInputStream(pubkeyAsBytes));
- } catch (IOException e) {
+ } catch(IOException e) {
throw new CryptFormatException(e);
}
- }
-
+ }
+
public BigInteger getY() {
return y;
- }
-
- public BigInteger getP() {
+ }
+
+ public BigInteger getP() {
return group.getP();
- }
-
- public BigInteger getQ() {
+ }
+
+ public BigInteger getQ() {
return group.getQ();
- }
-
- public BigInteger getG() {
+ }
+
+ public BigInteger getG() {
return group.getG();
- }
-
- public String keyType() {
+ }
+
+ public String keyType() {
return "DSA.p";
- }
+ }
- // Nope, this is fine
- public DSAGroup getGroup() {
+ // Nope, this is fine
+ public DSAGroup getGroup() {
return group;
- }
+ }
// public void writeForWireWithoutGroup(OutputStream out) throws
IOException {
// Util.writeMPI(y, out);
@@ -109,77 +109,75 @@
// Util.writeMPI(y, out);
// }
//
- public static CryptoKey read(InputStream i) throws IOException,
CryptFormatException {
+ public static CryptoKey read(InputStream i) throws IOException,
CryptFormatException {
return new DSAPublicKey(i);
- }
+ }
- public int keyId() {
+ public int keyId() {
return y.intValue();
- }
+ }
- public String toLongString() {
- return "y="+HexUtil.biToHex(y);
- }
+ public String toLongString() {
+ return "y=" + HexUtil.biToHex(y);
+ }
- // this won't correctly read the output from writeAsField
- //public static CryptoKey readFromField(DSAGroup group, String field) {
- // BigInteger y=Util.byteArrayToMPI(Util.hexToBytes(field));
- // return new DSAPublicKey(group, y);
- //}
-
- public byte[] asBytes() {
- byte[] groupBytes=group.asBytes();
- byte[] ybytes=Util.MPIbytes(y);
- byte[] bytes=new byte[groupBytes.length + ybytes.length];
+ // this won't correctly read the output from writeAsField
+ //public static CryptoKey readFromField(DSAGroup group, String field) {
+ // BigInteger y=Util.byteArrayToMPI(Util.hexToBytes(field));
+ // return new DSAPublicKey(group, y);
+ //}
+ public byte[] asBytes() {
+ byte[] groupBytes = group.asBytes();
+ byte[] ybytes = Util.MPIbytes(y);
+ byte[] bytes = new byte[groupBytes.length + ybytes.length];
System.arraycopy(groupBytes, 0, bytes, 0, groupBytes.length);
System.arraycopy(ybytes, 0, bytes, groupBytes.length,
ybytes.length);
return bytes;
- }
+ }
- public byte[] asBytesHash() {
- byte[] hash = SHA256.digest(asBytes());
- return hash;
- }
-
- public byte[] asPaddedBytes() {
- byte[] asBytes = asBytes();
- if(asBytes.length == PADDED_SIZE)
- return asBytes;
- if(asBytes.length > PADDED_SIZE)
- throw new Error("Cannot fit key in "+PADDED_SIZE+" - real size
is "+asBytes.length);
- byte[] padded = new byte[PADDED_SIZE];
- System.arraycopy(asBytes, 0, padded, 0, asBytes.length);
- return padded;
- }
-
- public byte[] fingerprint() {
+ public byte[] asBytesHash() {
+ byte[] hash = SHA256.digest(asBytes());
+ return hash;
+ }
+
+ public byte[] asPaddedBytes() {
+ byte[] asBytes = asBytes();
+ if(asBytes.length == PADDED_SIZE)
+ return asBytes;
+ if(asBytes.length > PADDED_SIZE)
+ throw new Error("Cannot fit key in " + PADDED_SIZE + "
- real size is " + asBytes.length);
+ byte[] padded = new byte[PADDED_SIZE];
+ System.arraycopy(asBytes, 0, padded, 0, asBytes.length);
+ return padded;
+ }
+
+ public byte[] fingerprint() {
synchronized(this) {
if(fingerprint == null)
- fingerprint = fingerprint(new BigInteger[] {y});
+ fingerprint = fingerprint(new BigInteger[]{y});
return fingerprint;
}
- }
-
- public boolean equals(DSAPublicKey o) {
- if(this == o) // Not necessary, but a very cheap optimization
- return true;
+ }
+
+ public boolean equals(DSAPublicKey o) {
+ if(this == o) // Not necessary, but a very cheap optimization
+ return true;
return y.equals(o.y) && group.equals(o.group);
- }
+ }
- public boolean equals(Object o) {
- if(this == o) // Not necessary, but a very cheap optimization
- return true;
- return (o instanceof DSAPublicKey)
- && y.equals(((DSAPublicKey) o).y)
- && group.equals(((DSAPublicKey) o).group);
- }
-
- public int compareTo(Object other) {
- if (other instanceof DSAPublicKey) {
- return getY().compareTo(((DSAPublicKey)other).getY());
- } else return -1;
- }
+ public boolean equals(Object o) {
+ if(this == o) // Not necessary, but a very cheap optimization
+ return true;
+ return (o instanceof DSAPublicKey) && y.equals(((DSAPublicKey)
o).y) && group.equals(((DSAPublicKey) o).group);
+ }
+ public int compareTo(Object other) {
+ if(other instanceof DSAPublicKey)
+ return getY().compareTo(((DSAPublicKey) other).getY());
+ else
+ return -1;
+ }
+
public SimpleFieldSet asFieldSet() {
SimpleFieldSet fs = new SimpleFieldSet(true);
fs.putSingle("y", Base64.encode(y.toByteArray()));
@@ -187,7 +185,7 @@
}
public static DSAPublicKey create(SimpleFieldSet set, DSAGroup group)
throws IllegalBase64Exception {
- NativeBigInteger x =
+ NativeBigInteger x =
new NativeBigInteger(1, Base64.decode(set.get("y")));
return new DSAPublicKey(group, x);
}