blob: 0265e6aef67b4675a49e203d27d70e1feca87bad [file] [log] [blame]
/* GENERATED SOURCE. DO NOT MODIFY. */
package com.android.internal.org.bouncycastle.jcajce.provider.symmetric.util;
import java.lang.reflect.Constructor;
import java.security.InvalidKeyException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactorySpi;
import javax.crypto.spec.SecretKeySpec;
import com.android.internal.org.bouncycastle.asn1.ASN1ObjectIdentifier;
/**
* @hide This class is not part of the Android public SDK API
*/
public class BaseSecretKeyFactory
extends SecretKeyFactorySpi
implements PBE
{
protected String algName;
protected ASN1ObjectIdentifier algOid;
protected BaseSecretKeyFactory(
String algName,
ASN1ObjectIdentifier algOid)
{
this.algName = algName;
this.algOid = algOid;
}
protected SecretKey engineGenerateSecret(
KeySpec keySpec)
throws InvalidKeySpecException
{
if (keySpec instanceof SecretKeySpec)
{
return new SecretKeySpec(((SecretKeySpec)keySpec).getEncoded(), algName);
}
throw new InvalidKeySpecException("Invalid KeySpec");
}
protected KeySpec engineGetKeySpec(
SecretKey key,
Class keySpec)
throws InvalidKeySpecException
{
if (keySpec == null)
{
throw new InvalidKeySpecException("keySpec parameter is null");
}
if (key == null)
{
throw new InvalidKeySpecException("key parameter is null");
}
if (SecretKeySpec.class.isAssignableFrom(keySpec))
{
return new SecretKeySpec(key.getEncoded(), algName);
}
try
{
Class[] parameters = { byte[].class };
Constructor c = keySpec.getConstructor(parameters);
Object[] p = new Object[1];
p[0] = key.getEncoded();
return (KeySpec)c.newInstance(p);
}
catch (Exception e)
{
throw new InvalidKeySpecException(e.toString());
}
}
protected SecretKey engineTranslateKey(
SecretKey key)
throws InvalidKeyException
{
if (key == null)
{
throw new InvalidKeyException("key parameter is null");
}
if (!key.getAlgorithm().equalsIgnoreCase(algName))
{
throw new InvalidKeyException("Key not of type " + algName + ".");
}
return new SecretKeySpec(key.getEncoded(), algName);
}
}