下面列出了java.security.Key#getClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
@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();
}
/**
* 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 + ".");
}
/**
* 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 + ".");
}
/**
* 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 + ".");
}
/**
* 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 + ".");
}
/**
* 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 + ".");
}
/**
* 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 + ".");
}