Java Code Examples for java.security.Key#getClass()

The following examples show how to use java.security.Key#getClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HmacUsingShaAlgorithm.java    From Jose4j with Apache License 2.0 5 votes vote down vote up
public boolean verifySignature(byte[] signatureBytes, Key key, byte[] securedInputBytes, ProviderContext providerContext) throws JoseException
{
    if (!(key instanceof SecretKey))
    {
        throw new InvalidKeyException(key.getClass() + " cannot be used for HMAC verification.");
    }

    Mac mac = getMacInstance(key, providerContext);
    byte[] calculatedSigature = mac.doFinal(securedInputBytes);

    return ByteUtil.secureEquals(signatureBytes, calculatedSigature);
}
 
Example 2
Source File: KeyFactoryTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Override
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
    if (TestPrivateKey.class == key.getClass()) {
        return new TestPublicKey();
    } else if (TestPublicKey.class == key.getClass()) {
        return new TestPrivateKey();
    }
    throw new InvalidKeyException();
}
 
Example 3
Source File: RainbowKeyFactorySpi.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
/**
 * Converts a given key into a key specification, if possible. Currently the
 * following specs are supported:
 * <ul>
 * <li>for RainbowPublicKey: X509EncodedKeySpec, RainbowPublicKeySpec
 * <li>for RainbowPrivateKey: PKCS8EncodedKeySpec, RainbowPrivateKeySpec
 * </ul>
 *
 * @param key     the key
 * @param keySpec the key specification
 * @return the specification of the CMSS key
 * @throws InvalidKeySpecException if the key type or key specification is not supported.
 */
public final KeySpec engineGetKeySpec(Key key, Class keySpec)
    throws InvalidKeySpecException
{
    if (key instanceof BCRainbowPrivateKey)
    {
        if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec))
        {
            return new PKCS8EncodedKeySpec(key.getEncoded());
        }
        else if (RainbowPrivateKeySpec.class.isAssignableFrom(keySpec))
        {
            BCRainbowPrivateKey privKey = (BCRainbowPrivateKey)key;
            return new RainbowPrivateKeySpec(privKey.getInvA1(), privKey
                .getB1(), privKey.getInvA2(), privKey.getB2(), privKey
                .getVi(), privKey.getLayers());
        }
    }
    else if (key instanceof BCRainbowPublicKey)
    {
        if (X509EncodedKeySpec.class.isAssignableFrom(keySpec))
        {
            return new X509EncodedKeySpec(key.getEncoded());
        }
        else if (RainbowPublicKeySpec.class.isAssignableFrom(keySpec))
        {
            BCRainbowPublicKey pubKey = (BCRainbowPublicKey)key;
            return new RainbowPublicKeySpec(pubKey.getDocLength(), pubKey
                .getCoeffQuadratic(), pubKey.getCoeffSingular(), pubKey
                .getCoeffScalar());
        }
    }
    else
    {
        throw new InvalidKeySpecException("Unsupported key type: "
            + key.getClass() + ".");
    }

    throw new InvalidKeySpecException("Unknown key specification: "
        + keySpec + ".");
}
 
Example 4
Source File: McElieceCCA2KeyFactorySpi.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
/**
 * Converts, if possible, a given key into a key specification. Currently,
 * the following key specifications are supported:
 * <ul>
 * <li>for McElieceCCA2PublicKey: {@link X509EncodedKeySpec},
 * {@link McElieceCCA2PublicKeySpec}</li>
 * <li>for McElieceCCA2PrivateKey: {@link PKCS8EncodedKeySpec},
 * {@link McElieceCCA2PrivateKeySpec}</li>.
 * </ul>
 *
 * @param key     the key
 * @param keySpec the key specification
 * @return the specification of the McEliece CCA2 key
 * @throws InvalidKeySpecException if the key type or the key specification is not
 * supported.
 * @see BCMcElieceCCA2PrivateKey
 * @see McElieceCCA2PrivateKeySpec
 * @see BCMcElieceCCA2PublicKey
 * @see McElieceCCA2PublicKeySpec
 */
public KeySpec getKeySpec(Key key, Class keySpec)
    throws InvalidKeySpecException
{
    if (key instanceof BCMcElieceCCA2PrivateKey)
    {
        if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec))
        {
            return new PKCS8EncodedKeySpec(key.getEncoded());
        }
        else if (McElieceCCA2PrivateKeySpec.class
            .isAssignableFrom(keySpec))
        {
            BCMcElieceCCA2PrivateKey privKey = (BCMcElieceCCA2PrivateKey)key;
            return new McElieceCCA2PrivateKeySpec(OID, privKey.getN(), privKey
                .getK(), privKey.getField(), privKey.getGoppaPoly(),
                privKey.getP(), privKey.getH(), privKey.getQInv());
        }
    }
    else if (key instanceof BCMcElieceCCA2PublicKey)
    {
        if (X509EncodedKeySpec.class.isAssignableFrom(keySpec))
        {
            return new X509EncodedKeySpec(key.getEncoded());
        }
        else if (McElieceCCA2PublicKeySpec.class
            .isAssignableFrom(keySpec))
        {
            BCMcElieceCCA2PublicKey pubKey = (BCMcElieceCCA2PublicKey)key;
            return new McElieceCCA2PublicKeySpec(OID, pubKey.getN(), pubKey
                .getT(), pubKey.getG());
        }
    }
    else
    {
        throw new InvalidKeySpecException("Unsupported key type: "
            + key.getClass() + ".");
    }

    throw new InvalidKeySpecException("Unknown key specification: "
        + keySpec + ".");
}
 
Example 5
Source File: McElieceKeyFactorySpi.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
/**
 * Converts, if possible, a given key into a key specification. Currently,
 * the following key specifications are supported:
 * <ul>
 * <li>for McEliecePublicKey: {@link X509EncodedKeySpec},
 * {@link McEliecePublicKeySpec}</li>
 * <li>for McEliecePrivateKey: {@link PKCS8EncodedKeySpec},
 * {@link McEliecePrivateKeySpec}</li>.
 * </ul>
 *
 * @param key     the key
 * @param keySpec the key specification
 * @return the specification of the McEliece key
 * @throws InvalidKeySpecException if the key type or the key specification is not
 * supported.
 * @see BCMcEliecePrivateKey
 * @see McEliecePrivateKeySpec
 * @see BCMcEliecePublicKey
 * @see McEliecePublicKeySpec
 */
public KeySpec getKeySpec(Key key, Class keySpec)
    throws InvalidKeySpecException
{
    if (key instanceof BCMcEliecePrivateKey)
    {
        if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec))
        {
            return new PKCS8EncodedKeySpec(key.getEncoded());
        }
        else if (McEliecePrivateKeySpec.class.isAssignableFrom(keySpec))
        {
            BCMcEliecePrivateKey privKey = (BCMcEliecePrivateKey)key;
            return new McEliecePrivateKeySpec(OID, privKey.getN(), privKey
                .getK(), privKey.getField(), privKey.getGoppaPoly(),
                privKey.getSInv(), privKey.getP1(), privKey.getP2(),
                privKey.getH(), privKey.getQInv());
        }
    }
    else if (key instanceof BCMcEliecePublicKey)
    {
        if (X509EncodedKeySpec.class.isAssignableFrom(keySpec))
        {
            return new X509EncodedKeySpec(key.getEncoded());
        }
        else if (McEliecePublicKeySpec.class.isAssignableFrom(keySpec))
        {
            BCMcEliecePublicKey pubKey = (BCMcEliecePublicKey)key;
            return new McEliecePublicKeySpec(OID, pubKey.getN(), pubKey.getT(),
                pubKey.getG());
        }
    }
    else
    {
        throw new InvalidKeySpecException("Unsupported key type: "
            + key.getClass() + ".");
    }

    throw new InvalidKeySpecException("Unknown key specification: "
        + keySpec + ".");
}
 
Example 6
Source File: RainbowKeyFactorySpi.java    From ripple-lib-java with ISC License 4 votes vote down vote up
/**
 * Converts a given key into a key specification, if possible. Currently the
 * following specs are supported:
 * <ul>
 * <li>for RainbowPublicKey: X509EncodedKeySpec, RainbowPublicKeySpec
 * <li>for RainbowPrivateKey: PKCS8EncodedKeySpec, RainbowPrivateKeySpec
 * </ul>
 *
 * @param key     the key
 * @param keySpec the key specification
 * @return the specification of the CMSS key
 * @throws InvalidKeySpecException if the key type or key specification is not supported.
 */
public final KeySpec engineGetKeySpec(Key key, Class keySpec)
    throws InvalidKeySpecException
{
    if (key instanceof BCRainbowPrivateKey)
    {
        if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec))
        {
            return new PKCS8EncodedKeySpec(key.getEncoded());
        }
        else if (RainbowPrivateKeySpec.class.isAssignableFrom(keySpec))
        {
            BCRainbowPrivateKey privKey = (BCRainbowPrivateKey)key;
            return new RainbowPrivateKeySpec(privKey.getInvA1(), privKey
                .getB1(), privKey.getInvA2(), privKey.getB2(), privKey
                .getVi(), privKey.getLayers());
        }
    }
    else if (key instanceof BCRainbowPublicKey)
    {
        if (X509EncodedKeySpec.class.isAssignableFrom(keySpec))
        {
            return new X509EncodedKeySpec(key.getEncoded());
        }
        else if (RainbowPublicKeySpec.class.isAssignableFrom(keySpec))
        {
            BCRainbowPublicKey pubKey = (BCRainbowPublicKey)key;
            return new RainbowPublicKeySpec(pubKey.getDocLength(), pubKey
                .getCoeffQuadratic(), pubKey.getCoeffSingular(), pubKey
                .getCoeffScalar());
        }
    }
    else
    {
        throw new InvalidKeySpecException("Unsupported key type: "
            + key.getClass() + ".");
    }

    throw new InvalidKeySpecException("Unknown key specification: "
        + keySpec + ".");
}
 
Example 7
Source File: McElieceCCA2KeyFactorySpi.java    From ripple-lib-java with ISC License 4 votes vote down vote up
/**
 * Converts, if possible, a given key into a key specification. Currently,
 * the following key specifications are supported:
 * <ul>
 * <li>for McElieceCCA2PublicKey: {@link X509EncodedKeySpec},
 * {@link McElieceCCA2PublicKeySpec}</li>
 * <li>for McElieceCCA2PrivateKey: {@link PKCS8EncodedKeySpec},
 * {@link McElieceCCA2PrivateKeySpec}</li>.
 * </ul>
 *
 * @param key     the key
 * @param keySpec the key specification
 * @return the specification of the McEliece CCA2 key
 * @throws InvalidKeySpecException if the key type or the key specification is not
 * supported.
 * @see BCMcElieceCCA2PrivateKey
 * @see McElieceCCA2PrivateKeySpec
 * @see BCMcElieceCCA2PublicKey
 * @see McElieceCCA2PublicKeySpec
 */
public KeySpec getKeySpec(Key key, Class keySpec)
    throws InvalidKeySpecException
{
    if (key instanceof BCMcElieceCCA2PrivateKey)
    {
        if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec))
        {
            return new PKCS8EncodedKeySpec(key.getEncoded());
        }
        else if (McElieceCCA2PrivateKeySpec.class
            .isAssignableFrom(keySpec))
        {
            BCMcElieceCCA2PrivateKey privKey = (BCMcElieceCCA2PrivateKey)key;
            return new McElieceCCA2PrivateKeySpec(OID, privKey.getN(), privKey
                .getK(), privKey.getField(), privKey.getGoppaPoly(),
                privKey.getP(), privKey.getH(), privKey.getQInv());
        }
    }
    else if (key instanceof BCMcElieceCCA2PublicKey)
    {
        if (X509EncodedKeySpec.class.isAssignableFrom(keySpec))
        {
            return new X509EncodedKeySpec(key.getEncoded());
        }
        else if (McElieceCCA2PublicKeySpec.class
            .isAssignableFrom(keySpec))
        {
            BCMcElieceCCA2PublicKey pubKey = (BCMcElieceCCA2PublicKey)key;
            return new McElieceCCA2PublicKeySpec(OID, pubKey.getN(), pubKey
                .getT(), pubKey.getG());
        }
    }
    else
    {
        throw new InvalidKeySpecException("Unsupported key type: "
            + key.getClass() + ".");
    }

    throw new InvalidKeySpecException("Unknown key specification: "
        + keySpec + ".");
}
 
Example 8
Source File: McElieceKeyFactorySpi.java    From ripple-lib-java with ISC License 4 votes vote down vote up
/**
 * Converts, if possible, a given key into a key specification. Currently,
 * the following key specifications are supported:
 * <ul>
 * <li>for McEliecePublicKey: {@link X509EncodedKeySpec},
 * {@link McEliecePublicKeySpec}</li>
 * <li>for McEliecePrivateKey: {@link PKCS8EncodedKeySpec},
 * {@link McEliecePrivateKeySpec}</li>.
 * </ul>
 *
 * @param key     the key
 * @param keySpec the key specification
 * @return the specification of the McEliece key
 * @throws InvalidKeySpecException if the key type or the key specification is not
 * supported.
 * @see BCMcEliecePrivateKey
 * @see McEliecePrivateKeySpec
 * @see BCMcEliecePublicKey
 * @see McEliecePublicKeySpec
 */
public KeySpec getKeySpec(Key key, Class keySpec)
    throws InvalidKeySpecException
{
    if (key instanceof BCMcEliecePrivateKey)
    {
        if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec))
        {
            return new PKCS8EncodedKeySpec(key.getEncoded());
        }
        else if (McEliecePrivateKeySpec.class.isAssignableFrom(keySpec))
        {
            BCMcEliecePrivateKey privKey = (BCMcEliecePrivateKey)key;
            return new McEliecePrivateKeySpec(OID, privKey.getN(), privKey
                .getK(), privKey.getField(), privKey.getGoppaPoly(),
                privKey.getSInv(), privKey.getP1(), privKey.getP2(),
                privKey.getH(), privKey.getQInv());
        }
    }
    else if (key instanceof BCMcEliecePublicKey)
    {
        if (X509EncodedKeySpec.class.isAssignableFrom(keySpec))
        {
            return new X509EncodedKeySpec(key.getEncoded());
        }
        else if (McEliecePublicKeySpec.class.isAssignableFrom(keySpec))
        {
            BCMcEliecePublicKey pubKey = (BCMcEliecePublicKey)key;
            return new McEliecePublicKeySpec(OID, pubKey.getN(), pubKey.getT(),
                pubKey.getG());
        }
    }
    else
    {
        throw new InvalidKeySpecException("Unsupported key type: "
            + key.getClass() + ".");
    }

    throw new InvalidKeySpecException("Unknown key specification: "
        + keySpec + ".");
}