Example usage for java.security.spec ECPublicKeySpec getParams

List of usage examples for java.security.spec ECPublicKeySpec getParams

Introduction

In this page you can find the example usage for java.security.spec ECPublicKeySpec getParams.

Prototype

public ECParameterSpec getParams() 

Source Link

Document

Returns the associated elliptic curve domain parameters.

Usage

From source file:Main.java

private static String getSignatureAlgorithm(Key key) throws Exception {
    if ("EC".equals(key.getAlgorithm())) {
        int curveSize;
        KeyFactory factory = KeyFactory.getInstance("EC");

        if (key instanceof PublicKey) {
            ECPublicKeySpec spec = factory.getKeySpec(key, ECPublicKeySpec.class);
            curveSize = spec.getParams().getCurve().getField().getFieldSize();
        } else if (key instanceof PrivateKey) {
            ECPrivateKeySpec spec = factory.getKeySpec(key, ECPrivateKeySpec.class);
            curveSize = spec.getParams().getCurve().getField().getFieldSize();
        } else {//ww  w. j  a v a 2s.c  om
            throw new InvalidKeySpecException();
        }

        if (curveSize <= 256) {
            return "SHA256withECDSA";
        } else if (curveSize <= 384) {
            return "SHA384withECDSA";
        } else {
            return "SHA512withECDSA";
        }
    } else if ("RSA".equals(key.getAlgorithm())) {
        return "SHA256withRSA";
    } else {
        throw new IllegalArgumentException("Unsupported key type " + key.getAlgorithm());
    }
}