001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, Connect2id Ltd.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * 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 distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.jose;
019
020
021/**
022 * Key length exception.
023 *
024 * @author Vladimir Dzhuvinov
025 * @version 205-06-29
026 */
027public class KeyLengthException extends KeyException {
028
029
030        /**
031         * The expected key length.
032         */
033        private final int expectedLength;
034
035
036        /**
037         * The algorithm.
038         */
039        private final Algorithm alg;
040
041
042        /**
043         * Creates a new key length exception.
044         *
045         * @param message The exception message.
046         */
047        public KeyLengthException(final String message) {
048
049                super(message);
050                expectedLength = 0;
051                alg = null;
052        }
053
054
055        /**
056         * Creates a new key length exception.
057         *
058         * @param alg The JOSE algorithm, {@code null} if not specified.
059         */
060        public KeyLengthException(final Algorithm alg) {
061
062                this(0, alg);
063        }
064
065
066        /**
067         * Creates a new key length exception.
068         *
069         * @param expectedLength The expected key length in bits, zero if not
070         *                       specified.
071         * @param alg            The JOSE algorithm, {@code null} if not
072         *                       specified.
073         */
074        public KeyLengthException(final int expectedLength, final Algorithm alg) {
075
076                super((
077                        (expectedLength > 0) ? "The expected key length is " + expectedLength + " bits" : "Unexpected key length") +
078                        ((alg != null) ? " (for " + alg + " algorithm)" : "")
079                );
080
081                this.expectedLength = expectedLength;
082                this.alg = alg;
083        }
084
085
086        /**
087         * Returns the expected key length.
088         *
089         * @return The expected key length in bits, zero if not specified.
090         */
091        public int getExpectedKeyLength() {
092
093                return expectedLength;
094        }
095
096
097        /**
098         * Returns the algorithm.
099         *
100         * @return The JOSE algorithm, {@code null} if not specified.
101         */
102        public Algorithm getAlgorithm() {
103
104                return alg;
105        }
106}