use of java.security.interfaces.DSAPrivateKey in project robovm by robovm.
the class DSAKeyFactoryImpl method engineGetKeySpec.
/**
* This method returns a specification for the supplied key.
*
* The specification will be returned in the form of an object of the type
* specified by keySpec.
*
* @param key -
* either DSAPrivateKey or DSAPublicKey
* @param keySpec -
* either DSAPrivateKeySpec.class or DSAPublicKeySpec.class
*
* @return either a DSAPrivateKeySpec or a DSAPublicKeySpec
*
* @throws InvalidKeySpecException
* if "keySpec" is not a specification for DSAPublicKey or
* DSAPrivateKey
*/
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException {
BigInteger p, q, g, x, y;
if (key != null) {
if (keySpec == null) {
throw new NullPointerException("keySpec == null");
}
if (key instanceof DSAPrivateKey) {
DSAPrivateKey privateKey = (DSAPrivateKey) key;
if (keySpec.equals(DSAPrivateKeySpec.class)) {
x = privateKey.getX();
DSAParams params = privateKey.getParams();
p = params.getP();
q = params.getQ();
g = params.getG();
return (T) (new DSAPrivateKeySpec(x, p, q, g));
}
if (keySpec.equals(PKCS8EncodedKeySpec.class)) {
return (T) (new PKCS8EncodedKeySpec(key.getEncoded()));
}
throw new InvalidKeySpecException("'keySpec' is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec");
}
if (key instanceof DSAPublicKey) {
DSAPublicKey publicKey = (DSAPublicKey) key;
if (keySpec.equals(DSAPublicKeySpec.class)) {
y = publicKey.getY();
DSAParams params = publicKey.getParams();
p = params.getP();
q = params.getQ();
g = params.getG();
return (T) (new DSAPublicKeySpec(y, p, q, g));
}
if (keySpec.equals(X509EncodedKeySpec.class)) {
return (T) (new X509EncodedKeySpec(key.getEncoded()));
}
throw new InvalidKeySpecException("'keySpec' is neither DSAPublicKeySpec nor X509EncodedKeySpec");
}
}
throw new InvalidKeySpecException("'key' is neither DSAPublicKey nor DSAPrivateKey");
}
use of java.security.interfaces.DSAPrivateKey in project robovm by robovm.
the class DSAKeyFactoryImpl method engineTranslateKey.
/**
* The method generates a DSAPublicKey object from the provided key.
*
* @param
* key - a DSAPublicKey object or DSAPrivateKey object.
*
* @return
* object of the same type as the "key" argument
*
* @throws InvalidKeyException
* if "key" is neither DSAPublicKey nor DSAPrivateKey
*/
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
if (key != null) {
if (key instanceof DSAPrivateKey) {
DSAPrivateKey privateKey = (DSAPrivateKey) key;
DSAParams params = privateKey.getParams();
try {
return engineGeneratePrivate(new DSAPrivateKeySpec(privateKey.getX(), params.getP(), params.getQ(), params.getG()));
} catch (InvalidKeySpecException e) {
// Actually this exception shouldn't be thrown
throw new InvalidKeyException("ATTENTION: InvalidKeySpecException: " + e);
}
}
if (key instanceof DSAPublicKey) {
DSAPublicKey publicKey = (DSAPublicKey) key;
DSAParams params = publicKey.getParams();
try {
return engineGeneratePublic(new DSAPublicKeySpec(publicKey.getY(), params.getP(), params.getQ(), params.getG()));
} catch (InvalidKeySpecException e) {
// Actually this exception shouldn't be thrown
throw new InvalidKeyException("ATTENTION: InvalidKeySpecException: " + e);
}
}
}
throw new InvalidKeyException("'key' is neither DSAPublicKey nor DSAPrivateKey");
}
use of java.security.interfaces.DSAPrivateKey in project robovm by robovm.
the class SHA1withDSA_SignatureImpl method engineInitSign.
/**
* Initializes this signature object with PrivateKey object
* passed as argument to the method.
*
* @params
* privateKey DSAPrivateKey object
* @throws
* InvalidKeyException if privateKey is not DSAPrivateKey object
*/
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
DSAParams params;
// parameters and private key
BigInteger p, q, x;
int n;
if (privateKey == null || !(privateKey instanceof DSAPrivateKey)) {
throw new InvalidKeyException();
}
params = ((DSAPrivateKey) privateKey).getParams();
p = params.getP();
q = params.getQ();
x = ((DSAPrivateKey) privateKey).getX();
// checks described in DSA standard
n = p.bitLength();
if (p.compareTo(BigInteger.valueOf(1)) != 1 || n < 512 || n > 1024 || (n & 077) != 0) {
throw new InvalidKeyException("bad p");
}
if (q.signum() != 1 && q.bitLength() != 160) {
throw new InvalidKeyException("bad q");
}
if (x.signum() != 1 || x.compareTo(q) != -1) {
throw new InvalidKeyException("x <= 0 || x >= q");
}
dsaKey = (DSAKey) privateKey;
msgDigest.reset();
}
use of java.security.interfaces.DSAPrivateKey in project robovm by robovm.
the class EncodedKeySpec2Test method isEqual.
private boolean isEqual(Key key1, Key key2) {
if (key1 instanceof DSAPublicKey && key2 instanceof DSAPublicKey) {
DSAPublicKey dsa1 = ((DSAPublicKey) key1);
DSAPublicKey dsa2 = ((DSAPublicKey) key2);
return dsa1.getY().equals(dsa2.getY()) && dsa1.getParams().getG().equals(dsa2.getParams().getG()) && dsa1.getParams().getP().equals(dsa2.getParams().getP()) && dsa1.getParams().getQ().equals(dsa2.getParams().getQ());
} else if (key1 instanceof DSAPrivateKey && key2 instanceof DSAPrivateKey) {
DSAPrivateKey dsa1 = ((DSAPrivateKey) key1);
DSAPrivateKey dsa2 = ((DSAPrivateKey) key2);
return dsa1.getX().equals(dsa2.getX()) && dsa1.getParams().getG().equals(dsa2.getParams().getG()) && dsa1.getParams().getP().equals(dsa2.getParams().getP()) && dsa1.getParams().getQ().equals(dsa2.getParams().getQ());
} else {
return false;
}
}
use of java.security.interfaces.DSAPrivateKey in project XobotOS by xamarin.
the class SHA1withDSA_SignatureImpl method engineInitSign.
/**
* Initializes this signature object with PrivateKey object
* passed as argument to the method.
*
* @params
* privateKey DSAPrivateKey object
* @throws
* InvalidKeyException if privateKey is not DSAPrivateKey object
*/
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
DSAParams params;
// parameters and private key
BigInteger p, q, x;
int n;
if (privateKey == null || !(privateKey instanceof DSAPrivateKey)) {
throw new InvalidKeyException();
}
params = ((DSAPrivateKey) privateKey).getParams();
p = params.getP();
q = params.getQ();
x = ((DSAPrivateKey) privateKey).getX();
// checks described in DSA standard
n = p.bitLength();
if (p.compareTo(BigInteger.valueOf(1)) != 1 || n < 512 || n > 1024 || (n & 077) != 0) {
throw new InvalidKeyException("bad p");
}
if (q.signum() != 1 && q.bitLength() != 160) {
throw new InvalidKeyException("bad q");
}
if (x.signum() != 1 || x.compareTo(q) != -1) {
throw new InvalidKeyException("x <= 0 || x >= q");
}
dsaKey = (DSAKey) privateKey;
msgDigest.reset();
}
Aggregations