Home | History | Annotate | Download | only in crypto
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package javax.crypto;
     19 
     20 import java.security.InvalidKeyException;
     21 import java.security.spec.InvalidKeySpecException;
     22 import java.security.spec.KeySpec;
     23 
     24 /**
     25  * The <i>Service Provider Interface</i> (<b>SPI</b>) definition for the {@code
     26  * SecretKeyFactory} class.
     27  */
     28 public abstract class SecretKeyFactorySpi {
     29 
     30     /**
     31      * Creates a new {@code SecretKeyFactorySpi} instance.
     32      */
     33     public SecretKeyFactorySpi() {
     34     }
     35 
     36     /**
     37      * Generate a secret key from the specified key specification.
     38      *
     39      * @param keySpec
     40      *            the key specification.
     41      * @return a secret key.
     42      * @throws InvalidKeySpecException
     43      *             if the specified key specification cannot be used to generate
     44      *             a secret key.
     45      */
     46     protected abstract SecretKey engineGenerateSecret(KeySpec keySpec)
     47             throws InvalidKeySpecException;
     48 
     49     /**
     50      * Returns the key specification of the specified secret key.
     51      *
     52      * @param key
     53      *            the secret key to get the specification from.
     54      * @param keySpec
     55      *            the target key specification class.
     56      * @return an instance of the specified key specification class.
     57      * @throws InvalidKeySpecException
     58      *             if the specified secret key cannot be transformed into the
     59      *             requested key specification.
     60      */
     61     @SuppressWarnings("unchecked")
     62     protected abstract KeySpec engineGetKeySpec(SecretKey key, Class keySpec)
     63             throws InvalidKeySpecException;
     64 
     65     /**
     66      * Translates the specified secret key into an instance of the corresponding
     67      * key from the provider of this key factory.
     68      *
     69      * @param key
     70      *            the secret key to translate.
     71      * @return the corresponding translated key.
     72      * @throws InvalidKeyException
     73      *             if the specified key cannot be translated using this key
     74      *             factory.
     75      */
     76     protected abstract SecretKey engineTranslateKey(SecretKey key)
     77             throws InvalidKeyException;
     78 }