1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.commons.crypto.utils;
20
21 import java.security.NoSuchAlgorithmException;
22
23 import javax.crypto.NoSuchPaddingException;
24
25 /**
26 * Transformation algorithm, mode and padding, in the format "Algorithm/Mode/Padding", for example "AES/CBC/NoPadding".
27 *
28 * @since 1.2.0
29 */
30 public class Transformation {
31
32 private static final int T_DELIM_PARTS = 3;
33 private static final String T_DELIM_REGEX = "/";
34
35 /**
36 * Parses a transformation.
37 *
38 * @param transformation current transformation
39 * @return the Transformation
40 * @throws NoSuchAlgorithmException if the algorithm is not supported
41 * @throws NoSuchPaddingException Thrown when the padding is unsupported.
42 */
43 public static Transformation parse(final String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException {
44 if (transformation == null) {
45 throw new NoSuchAlgorithmException("No transformation given.");
46 }
47
48 //
49 // Array containing the components of a Cipher transformation: index 0:
50 // algorithm (e.g., AES) index 1: mode (e.g., CTR) index 2: padding (e.g.,
51 // NoPadding)
52 //
53 final String[] parts = transformation.split(T_DELIM_REGEX, T_DELIM_PARTS + 1);
54 if (parts.length != T_DELIM_PARTS) {
55 throw new NoSuchAlgorithmException("Invalid transformation format: " + transformation);
56 }
57 return new Transformation(parts[0], parts[1], parts[2]);
58 }
59
60 private final String algorithm;
61 private final String mode;
62 private final Padding padding;
63
64 /**
65 * Constructs a new instance.
66 *
67 * @param algorithm the algorithm name
68 * @param mode the mode name
69 * @param padding the padding name
70 */
71 private Transformation(final String algorithm, final String mode, final Padding padding) {
72 this.algorithm = algorithm;
73 this.mode = mode;
74 this.padding = padding;
75 }
76
77 /**
78 * Constructs a new instance.
79 *
80 * @param algorithm the algorithm name
81 * @param mode the mode name
82 * @param padding the padding name
83 * @throws NoSuchPaddingException Thrown when the padding is unsupported.
84 */
85 private Transformation(final String algorithm, final String mode, final String padding) throws NoSuchPaddingException {
86 this(algorithm, mode, Padding.get(padding));
87 }
88
89 /**
90 * Gets the algorithm.
91 *
92 * @return the algorithm.
93 */
94 public String getAlgorithm() {
95 return algorithm;
96 }
97
98 /**
99 * 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 }