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
021import com.nimbusds.jose.util.JSONStringUtils;
022import net.jcip.annotations.Immutable;
023
024import java.io.Serializable;
025import java.util.Objects;
026
027
028/**
029 * The base class for algorithm names, with optional implementation 
030 * requirement. This class is immutable.
031 *
032 * <p>Includes constants for the following standard algorithm names:
033 *
034 * <ul>
035 *     <li>{@link #NONE none}
036 * </ul>
037 *
038 * @author Vladimir Dzhuvinov 
039 * @version 2024-04-20
040 */
041@Immutable
042public class Algorithm implements Serializable {
043
044
045        private static final long serialVersionUID = 1L;
046
047
048        /**
049         * No algorithm (unsecured JOSE object without signature / encryption).
050         */
051        public static final Algorithm NONE = new Algorithm("none", Requirement.REQUIRED);
052
053
054        /**
055         * The algorithm name.
056         */
057        private final String name;
058
059
060        /**
061         * The implementation requirement, {@code null} if not known.
062         */
063        private final Requirement requirement;
064
065
066        /**
067         * Creates a new JOSE algorithm name.
068         *
069         * @param name The algorithm name. Must not be {@code null}.
070         * @param req  The implementation requirement, {@code null} if not 
071         *             known.
072         */
073        public Algorithm(final String name, final Requirement req) {
074                this.name = Objects.requireNonNull(name);
075                requirement = req;
076        }
077
078
079        /**
080         * Creates a new JOSE algorithm name.
081         *
082         * @param name The algorithm name. Must not be {@code null}.
083         */
084        public Algorithm(final String name) {
085
086                this(name, null);
087        }
088
089
090        /**
091         * Gets the name of this algorithm.
092         *
093         * @return The algorithm name.
094         */
095        public final String getName() {
096
097                return name;
098        }
099
100
101        /**
102         * Gets the implementation requirement of this algorithm.
103         *
104         * @return The implementation requirement, {@code null} if not known.
105         */
106        public final Requirement getRequirement() {
107
108                return requirement;
109        }
110
111
112        /**
113         * Overrides {@code Object.hashCode()}.
114         *
115         * @return The object hash code.
116         */
117        @Override
118        public final int hashCode() {
119
120                return name.hashCode();
121        }
122
123
124        /**
125         * Overrides {@code Object.equals()}.
126         *
127         * @param object The object to compare to.
128         *
129         * @return {@code true} if the objects have the same value, otherwise
130         *         {@code false}.
131         */
132        @Override
133        public boolean equals(final Object object) {
134
135                return object instanceof Algorithm &&
136                        this.toString().equals(object.toString());
137        }
138
139
140        /**
141         * Returns the string representation of this algorithm.
142         *
143         * @see #getName
144         *
145         * @return The string representation.
146         */
147        @Override
148        public final String toString() {
149
150                return name;
151        }
152
153
154        /**
155         * Returns the JSON string representation of this algorithm.
156         * 
157         * @return The JSON string representation.
158         */
159        public final String toJSONString() {
160                return  JSONStringUtils.toJSONString(name);
161        }
162        
163        /**
164         * Parses an optional algorithm.
165         *
166         * @param s The string to parse. May be {@code null}.
167         *
168         * @return  The JOSE algorithm, {@code null} if not specified.
169         */
170        public static Algorithm parse(final String s) {
171            
172                if(s == null) {
173                        return null;
174                } else {
175                        return new Algorithm(s);
176                }
177        }
178}