From cb127a0ca309849c95cddc7d0e54e54e3857302a Mon Sep 17 00:00:00 2001 From: Bert Massop Date: Sat, 5 Oct 2024 21:12:53 +0200 Subject: [PATCH] CryptoKey: assorted cleanup --- src/freenet/crypt/DSAGroup.java | 2 -- src/freenet/crypt/DSAPrivateKey.java | 8 ++++---- src/freenet/crypt/DSAPublicKey.java | 23 ++++++++--------------- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/freenet/crypt/DSAGroup.java b/src/freenet/crypt/DSAGroup.java index 92b6e8571c..cb3a0487a9 100644 --- a/src/freenet/crypt/DSAGroup.java +++ b/src/freenet/crypt/DSAGroup.java @@ -20,8 +20,6 @@ */ public class DSAGroup extends CryptoKey { private static final long serialVersionUID = -1; - - protected static final int Q_BIT_LENGTH = 256; private final BigInteger p, q, g; diff --git a/src/freenet/crypt/DSAPrivateKey.java b/src/freenet/crypt/DSAPrivateKey.java index 3fdf84ef96..e139d0dc73 100644 --- a/src/freenet/crypt/DSAPrivateKey.java +++ b/src/freenet/crypt/DSAPrivateKey.java @@ -67,10 +67,10 @@ public SimpleFieldSet asFieldSet() { } public static DSAPrivateKey create(SimpleFieldSet fs, DSAGroup group) throws IllegalBase64Exception { - BigInteger y = new BigInteger(1, Base64.decode(fs.get("x"))); - if(y.bitLength() > 512) + BigInteger x = new BigInteger(1, Base64.decode(fs.get("x"))); + if (x.bitLength() > 512) { throw new IllegalBase64Exception("Probably a pubkey"); - return new DSAPrivateKey(y, group); + } + return new DSAPrivateKey(x, group); } } - diff --git a/src/freenet/crypt/DSAPublicKey.java b/src/freenet/crypt/DSAPublicKey.java index 2e40a998fe..a9dcd87e63 100644 --- a/src/freenet/crypt/DSAPublicKey.java +++ b/src/freenet/crypt/DSAPublicKey.java @@ -33,10 +33,10 @@ public DSAPublicKey(DSAGroup g, BigInteger y) { if(y.signum() != 1) throw new IllegalArgumentException(); this.y = y; - if(g == Global.DSAgroupBigA) g = null; - this.group = g; - if(y.compareTo(getGroup().getP()) > 0) - throw new IllegalArgumentException("y must be < p but y=" + y + " p=" + g.getP()); + this.group = g == Global.DSAgroupBigA ? null : g; + if (y.compareTo(getGroup().getP()) > 0) { + throw new IllegalArgumentException("y must be < p but y=" + y + " p=" + getGroup().getP()); + } } /** @@ -88,10 +88,8 @@ public String keyType() { return "DSA.p"; } - // Nope, this is fine public final DSAGroup getGroup() { - if(group == null) return Global.DSAgroupBigA; - else return group; + return group == null ? Global.DSAgroupBigA : group; } public static CryptoKey read(InputStream i) throws IOException, CryptFormatException { @@ -158,15 +156,10 @@ public SimpleFieldSet asFieldSet() { } public static DSAPublicKey create(SimpleFieldSet set, DSAGroup group) throws FSParseException { - BigInteger x; - try { - x = new BigInteger(1, Base64.decode(set.get("y"))); - } catch (IllegalBase64Exception e) { - throw new FSParseException(e); - } try { - return new DSAPublicKey(group, x); - } catch (IllegalArgumentException e) { + BigInteger y = new BigInteger(1, Base64.decode(set.get("y"))); + return new DSAPublicKey(group, y); + } catch (IllegalBase64Exception | IllegalArgumentException e) { throw new FSParseException(e); } }