001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.commons.crypto.utils;
020
021import java.security.NoSuchAlgorithmException;
022
023import javax.crypto.NoSuchPaddingException;
024
025/**
026 * Transformation algorithm, mode and padding, in the format "Algorithm/Mode/Padding", for example "AES/CBC/NoPadding".
027 *
028 * @since 1.2.0
029 */
030public class Transformation {
031
032    private static final int T_DELIM_PARTS = 3;
033    private static final String T_DELIM_REGEX = "/";
034
035    /**
036     * Parses a transformation.
037     *
038     * @param transformation current transformation
039     * @return the Transformation
040     * @throws NoSuchAlgorithmException if the algorithm is not supported
041     * @throws NoSuchPaddingException Thrown when the padding is unsupported.
042     */
043    public static Transformation parse(final String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException {
044        if (transformation == null) {
045            throw new NoSuchAlgorithmException("No transformation given.");
046        }
047
048        //
049        // Array containing the components of a Cipher transformation: index 0:
050        // algorithm (e.g., AES) index 1: mode (e.g., CTR) index 2: padding (e.g.,
051        // NoPadding)
052        //
053        final String[] parts = transformation.split(T_DELIM_REGEX, T_DELIM_PARTS + 1);
054        if (parts.length != T_DELIM_PARTS) {
055            throw new NoSuchAlgorithmException("Invalid transformation format: " + transformation);
056        }
057        return new Transformation(parts[0], parts[1], parts[2]);
058    }
059
060    private final String algorithm;
061    private final String mode;
062    private final Padding padding;
063
064    /**
065     * Constructs a new instance.
066     *
067     * @param algorithm the algorithm name
068     * @param mode the mode name
069     * @param padding the padding name
070     */
071    private Transformation(final String algorithm, final String mode, final Padding padding) {
072        this.algorithm = algorithm;
073        this.mode = mode;
074        this.padding = padding;
075    }
076
077    /**
078     * Constructs a new instance.
079     *
080     * @param algorithm the algorithm name
081     * @param mode the mode name
082     * @param padding the padding name
083     * @throws NoSuchPaddingException Thrown when the padding is unsupported.
084     */
085    private Transformation(final String algorithm, final String mode, final String padding) throws NoSuchPaddingException {
086        this(algorithm, mode, Padding.get(padding));
087    }
088
089    /**
090     * Gets the algorithm.
091     *
092     * @return the algorithm.
093     */
094    public String getAlgorithm() {
095        return algorithm;
096    }
097
098    /**
099     * Gets the mode.
100     *
101     * @return the mode.
102     */
103    public String getMode() {
104        return mode;
105    }
106
107    /**
108     * Gets the padding.
109     *
110     * @return the padding.
111     */
112    public Padding getPadding() {
113        return padding;
114    }
115}