001package com.nimbusds.jose; 002 003 004/** 005 * Key length exception. 006 * 007 * @author Vladimir Dzhuvinov 008 * @version 205-06-29 009 */ 010public class KeyLengthException extends KeyException { 011 012 013 /** 014 * The expected key length. 015 */ 016 private final int expectedLength; 017 018 019 /** 020 * The algorithm. 021 */ 022 private final Algorithm alg; 023 024 025 /** 026 * Creates a new key length exception. 027 * 028 * @param message The exception message. 029 */ 030 public KeyLengthException(final String message) { 031 032 super(message); 033 expectedLength = 0; 034 alg = null; 035 } 036 037 038 /** 039 * Creates a new key length exception. 040 * 041 * @param alg The JOSE algorithm, {@code null} if not specified. 042 */ 043 public KeyLengthException(final Algorithm alg) { 044 045 this(0, alg); 046 } 047 048 049 /** 050 * Creates a new key length exception. 051 * 052 * @param expectedLength The expected key length in bits, zero if not 053 * specified. 054 * @param alg The JOSE algorithm, {@code null} if not 055 * specified. 056 */ 057 public KeyLengthException(final int expectedLength, final Algorithm alg) { 058 059 super(( 060 (expectedLength > 0) ? "The expected key length is " + expectedLength + " bits" : "Unexpected key length") + 061 ((alg != null) ? " (for " + alg + " algorithm)" : "") 062 ); 063 064 this.expectedLength = expectedLength; 065 this.alg = alg; 066 } 067 068 069 /** 070 * Returns the expected key length. 071 * 072 * @return The expected key length in bits, zero if not specified. 073 */ 074 public int getExpectedKeyLength() { 075 076 return expectedLength; 077 } 078 079 080 /** 081 * Returns the algorithm. 082 * 083 * @return The JOSE algorithm, {@code null} if not specified. 084 */ 085 public Algorithm getAlgorithm() { 086 087 return alg; 088 } 089}