Added 'toPublic()' methods to PrivateKey and SigningPrivateKey, such
that these return PublicKey and SigningPublicKey objects, respectively.
This commit is contained in:
@ -76,6 +76,20 @@ public class KeyGenerator {
|
|||||||
return keys;
|
return keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Convert a PrivateKey to its corresponding PublicKey
|
||||||
|
* @param a PrivateKey object
|
||||||
|
* @return the corresponding PublicKey object
|
||||||
|
* @author aum
|
||||||
|
*/
|
||||||
|
public static PublicKey getPublicKey(PrivateKey priv) {
|
||||||
|
BigInteger a = new NativeBigInteger(priv.toByteArray());
|
||||||
|
BigInteger aalpha = CryptoConstants.elgg.modPow(a, CryptoConstants.elgp);
|
||||||
|
PublicKey pub = new PublicKey();
|
||||||
|
byte [] pubBytes = aalpha.toByteArray();
|
||||||
|
pub.setData(padBuffer(pubBytes, PublicKey.KEYSIZE_BYTES));
|
||||||
|
return pub;
|
||||||
|
}
|
||||||
|
|
||||||
/** Generate a pair of DSA keys, where index 0 is a SigningPublicKey, and
|
/** Generate a pair of DSA keys, where index 0 is a SigningPublicKey, and
|
||||||
* index 1 is a SigningPrivateKey
|
* index 1 is a SigningPrivateKey
|
||||||
* @return pair of keys
|
* @return pair of keys
|
||||||
@ -100,6 +114,20 @@ public class KeyGenerator {
|
|||||||
return keys;
|
return keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Convert a SigningPrivateKey to a SigningPublicKey
|
||||||
|
* @param a SigningPrivateKey object
|
||||||
|
* @return a SigningPublicKey object
|
||||||
|
* @author aum
|
||||||
|
*/
|
||||||
|
public static SigningPublicKey getSigningPublicKey(SigningPrivateKey priv) {
|
||||||
|
BigInteger x = new NativeBigInteger(priv.toByteArray());
|
||||||
|
BigInteger y = CryptoConstants.dsag.modPow(x, CryptoConstants.dsap);
|
||||||
|
SigningPublicKey pub = new SigningPublicKey();
|
||||||
|
byte [] pubBytes = padBuffer(y.toByteArray(), SigningPublicKey.KEYSIZE_BYTES);
|
||||||
|
pub.setData(pubBytes);
|
||||||
|
return pub;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pad the buffer w/ leading 0s or trim off leading bits so the result is the
|
* Pad the buffer w/ leading 0s or trim off leading bits so the result is the
|
||||||
* given length.
|
* given length.
|
||||||
|
@ -14,6 +14,7 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import net.i2p.util.Log;
|
import net.i2p.util.Log;
|
||||||
|
import net.i2p.crypto.KeyGenerator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the PrivateKey as defined by the I2P data structure spec.
|
* Defines the PrivateKey as defined by the I2P data structure spec.
|
||||||
@ -77,4 +78,14 @@ public class PrivateKey extends DataStructureImpl {
|
|||||||
buf.append("]");
|
buf.append("]");
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** derives a new PublicKey object derived from the secret contents
|
||||||
|
* of this PrivateKey
|
||||||
|
* @return a PublicKey object
|
||||||
|
* @author aum
|
||||||
|
*/
|
||||||
|
public PublicKey toPublic() {
|
||||||
|
return KeyGenerator.getPublicKey(this);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -76,4 +76,5 @@ public class PublicKey extends DataStructureImpl {
|
|||||||
buf.append("]");
|
buf.append("]");
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -14,6 +14,7 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import net.i2p.util.Log;
|
import net.i2p.util.Log;
|
||||||
|
import net.i2p.crypto.KeyGenerator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the SigningPrivateKey as defined by the I2P data structure spec.
|
* Defines the SigningPrivateKey as defined by the I2P data structure spec.
|
||||||
@ -76,4 +77,12 @@ public class SigningPrivateKey extends DataStructureImpl {
|
|||||||
buf.append("]");
|
buf.append("]");
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** converts this signing private key to its public equivalent
|
||||||
|
* @return a SigningPublicKey object derived from this private key
|
||||||
|
* @author aum
|
||||||
|
*/
|
||||||
|
public SigningPublicKey toPublic() {
|
||||||
|
return KeyGenerator.getSigningPublicKey(this);
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user