it.scoppelletti.programmerpower.security.spi.EncodedKeyFactory.java Source code

Java tutorial

Introduction

Here is the source code for it.scoppelletti.programmerpower.security.spi.EncodedKeyFactory.java

Source

/*
 * Copyright (C) 2010 Dario Scoppelletti, <http://www.scoppelletti.it/>.
 * 
 * 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.
 */

package it.scoppelletti.programmerpower.security.spi;

import java.security.*;
import java.security.spec.*;
import java.util.*;
import org.apache.commons.codec.binary.*;
import it.scoppelletti.programmerpower.*;
import it.scoppelletti.programmerpower.types.*;

/**
 * Classe di factory di una chiave per un algoritmo di crittografia asimmetrico.
 *  
 * <H4>1. Propriet&agrave;</H4>
 * 
 * <P><TABLE WIDTH="100%" BORDER="1" CELLPADDING="5">
 * <THEAD>
 * <TR>
 *     <TH>Propriet&agrave;</TH>
 *     <TH>Descrizione</TH>     
 * </TR>
 * </THEAD>
 * <TBODY>     
 * <TR>
 *      <TD>{@code key.alg}</TD>
 *      <TD>Codice dell&rsquo;algoritmo di crittografia.</TD>
 * </TR>  
 * <TR>
 *      <TD>{@code key.type}</TD>
 *      <TD>Codice del tipo di chiave di crittografia secondo
 *      l&rsquo;enumerato {@code KeyRep.Type}.</TD>
 * </TR> 
 * <TR>
 *      <TD>{@code data}</TD>
 *      <TD>Sequenza di byte che rappresenta il valore della chiave codificata
 *      nel formato Base64 come definito da RFC 2045.</TD>
 * </TR> 
 * </TBODY>
 * </TABLE></P>
 *   
 * @see   it.scoppelletti.programmerpower.security.CryptoUtils#getKey
 * @see   <A HREF="{@docRoot}/it/scoppelletti/programmerpower/security/CryptoUtils.html#idAlg">Algoritmi
 *        di crittografia</A> 
 * @see   <A HREF="http://www.ietf.org/rfc/rfc2045.txt" TARGET="_blank">RFC
 *        2045: Multipurpose Internet Mail Extensions (MIME) Part One: Format of
 *        Internet Message Bodies</A>
 * @since 1.0.0
 */
public final class EncodedKeyFactory extends AbstractCryptoKeyFactory {

    /**
     * Nome della propriet&agrave; sulla quale deve essere impostato il valore
     * della chiave. Il valore della costante &egrave; <CODE>{@value}</CODE>.
     * 
     * @see <A HREF="http://www.ietf.org/rfc/rfc2045.txt" TARGET="_blank">RFC
     *      2045: Multipurpose Internet Mail Extensions (MIME) Part One: Format
     *      of Internet Message Bodies</A>  
     */
    public static final String PROP_DATA = "data";

    /**
     * Costruttore.
     */
    public EncodedKeyFactory() {
    }

    @Override
    protected String getAlgorithm(Properties props, String prefix) {
        String name, alg;

        name = Strings.concat(prefix, EncodedKeyFactory.PROP_ALGORITHM);
        alg = props.getProperty(name);
        if (Strings.isNullOrEmpty(alg)) {
            throw new ArgumentNullException(name);
        }

        return alg;
    }

    @Override
    protected KeyRep.Type getKeyType(Properties props, String prefix) {
        String name, value;
        KeyRep.Type keyType;

        name = Strings.concat(prefix, EncodedKeyFactory.PROP_KEYTYPE);
        value = props.getProperty(name);
        if (Strings.isNullOrEmpty(value)) {
            throw new ArgumentNullException(name);
        }

        keyType = (KeyRep.Type) Enums.valueOf(KeyRep.Type.class, value);

        return keyType;
    }

    @Override
    protected KeySpec getKeySpec(String alg, KeyRep.Type keyType, Properties props, String prefix) {
        String name, value;
        byte[] data;
        KeySpec keySpec;

        name = Strings.concat(prefix, EncodedKeyFactory.PROP_DATA);
        value = props.getProperty(name);
        if (Strings.isNullOrEmpty(value)) {
            throw new ArgumentNullException(name);
        }

        data = Base64.decodeBase64(value);

        switch (keyType) {
        case PUBLIC:
            keySpec = new X509EncodedKeySpec(data);
            break;

        case PRIVATE:
            keySpec = new PKCS8EncodedKeySpec(data);
            break;

        default:
            throw new EnumConstantNotPresentException(KeyRep.Type.class, keyType.toString());
        }

        return keySpec;
    }
}