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 java.io.Serializable; 022 023import com.nimbusds.jose.util.JSONStringUtils; 024 025import net.jcip.annotations.Immutable; 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 2020-06-03 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 075 if (name == null) { 076 077 throw new IllegalArgumentException("The algorithm name must not be null"); 078 } 079 080 this.name = name; 081 082 requirement = req; 083 } 084 085 086 /** 087 * Creates a new JOSE algorithm name. 088 * 089 * @param name The algorithm name. Must not be {@code null}. 090 */ 091 public Algorithm(final String name) { 092 093 this(name, null); 094 } 095 096 097 /** 098 * Gets the name of this algorithm. 099 * 100 * @return The algorithm name. 101 */ 102 public final String getName() { 103 104 return name; 105 } 106 107 108 /** 109 * Gets the implementation requirement of this algorithm. 110 * 111 * @return The implementation requirement, {@code null} if not known. 112 */ 113 public final Requirement getRequirement() { 114 115 return requirement; 116 } 117 118 119 /** 120 * Overrides {@code Object.hashCode()}. 121 * 122 * @return The object hash code. 123 */ 124 @Override 125 public final int hashCode() { 126 127 return name.hashCode(); 128 } 129 130 131 /** 132 * Overrides {@code Object.equals()}. 133 * 134 * @param object The object to compare to. 135 * 136 * @return {@code true} if the objects have the same value, otherwise 137 * {@code false}. 138 */ 139 @Override 140 public boolean equals(final Object object) { 141 142 return object instanceof Algorithm && 143 this.toString().equals(object.toString()); 144 } 145 146 147 /** 148 * Returns the string representation of this algorithm. 149 * 150 * @see #getName 151 * 152 * @return The string representation. 153 */ 154 @Override 155 public final String toString() { 156 157 return name; 158 } 159 160 161 /** 162 * Returns the JSON string representation of this algorithm. 163 * 164 * @return The JSON string representation. 165 */ 166 public final String toJSONString() { 167 return JSONStringUtils.toJSONString(name); 168 } 169 170 /** 171 * Parses an optional algorithm. 172 * 173 * @param s The string to parse. May be {@code null}. 174 * 175 * @return The JOSE algorithm, {@code null} if not specified. 176 */ 177 public static Algorithm parse(final String s) { 178 179 if(s == null) { 180 return null; 181 } else { 182 return new Algorithm(s); 183 } 184 } 185}