use of com.github.zhenwei.provider.jce.interfaces.ECPrivateKey in project LinLong-Java by zhenwei1108.
the class ECUtil method generatePrivateKeyParameter.
public static AsymmetricKeyParameter generatePrivateKeyParameter(PrivateKey key) throws InvalidKeyException {
if (key instanceof ECPrivateKey) {
ECPrivateKey k = (ECPrivateKey) key;
ECParameterSpec s = k.getParameters();
if (s == null) {
s = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
}
if (k.getParameters() instanceof ECNamedCurveParameterSpec) {
String name = ((ECNamedCurveParameterSpec) k.getParameters()).getName();
return new ECPrivateKeyParameters(k.getD(), new ECNamedDomainParameters(ECNamedCurveTable.getOID(name), s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
} else {
return new ECPrivateKeyParameters(k.getD(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
}
} else if (key instanceof java.security.interfaces.ECPrivateKey) {
java.security.interfaces.ECPrivateKey privKey = (java.security.interfaces.ECPrivateKey) key;
ECParameterSpec s = EC5Util.convertSpec(privKey.getParams());
return new ECPrivateKeyParameters(privKey.getS(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
} else {
// see if we can build a key from key.getEncoded()
try {
byte[] bytes = key.getEncoded();
if (bytes == null) {
throw new InvalidKeyException("no encoding for EC private key");
}
PrivateKey privateKey = WeGooProvider.getPrivateKey(PrivateKeyInfo.getInstance(bytes));
if (privateKey instanceof java.security.interfaces.ECPrivateKey) {
return ECUtil.generatePrivateKeyParameter(privateKey);
}
} catch (Exception e) {
throw new InvalidKeyException("cannot identify EC private key: " + e.toString());
}
}
throw new InvalidKeyException("can't identify EC private key.");
}
Aggregations