Java Key Create getKeySize(final Key key)

Here you can find the source of getKeySize(final Key key)

Description

Get the key size for the given Key .

License

Open Source License

Parameter

Parameter Description
key the key

Return

the key size or -1 if the key size cannot be determined

Declaration

public static int getKeySize(final Key key) 

Method Source Code


//package com.java2s;
/*/*  w w w  .jav  a  2s.c  o  m*/
 * JBoss, Home of Professional Open Source.
 * Copyright 2018 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.security.Key;

import java.security.interfaces.DSAKey;
import java.security.interfaces.DSAParams;
import java.security.interfaces.ECKey;
import java.security.interfaces.RSAKey;
import java.security.spec.ECParameterSpec;

public class Main {
    /**
     * Get the key size for the given {@code Key}.
     *
     * @param key the key
     * @return the key size or -1 if the key size cannot be determined
     */
    public static int getKeySize(final Key key) {
        if (key instanceof ECKey) {
            ECParameterSpec params = ((ECKey) key).getParams();
            if (params != null) {
                return params.getOrder().bitLength();
            }
        } else if (key instanceof DSAKey) {
            DSAParams params = ((DSAKey) key).getParams();
            if (params != null) {
                return params.getP().bitLength();
            }
        } else if (key instanceof RSAKey) {
            return ((RSAKey) key).getModulus().bitLength();
        }
        return -1;
    }
}

Related

  1. getKeyPair()
  2. getKeyPair()
  3. getKeyPair(KeyStore keyStore, String alias, String password)
  4. getKeyPairGenerator(final String algorithm)
  5. getKeys()
  6. getKeySize(PrivateKey key)
  7. getKeyStore()
  8. getKeyStore()
  9. getKeyStore(byte[] bytes, char[] password)