001 package com.nimbusds.jose; 002 003 004 import net.jcip.annotations.Immutable; 005 006 007 /** 008 * JSON Web Signature (JWS) algorithm name, represents the {@code alg} header 009 * parameter in JWS objects. Also used to represent integrity algorithm 010 * ({@code ia}) header parameters in JWE objects. This class is immutable. 011 * 012 * <p>Includes constants for the following standard JWS algorithm names: 013 * 014 * <ul> 015 * <li>{@link #HS256} 016 * <li>{@link #HS384} 017 * <li>{@link #HS512} 018 * <li>{@link #RS256} 019 * <li>{@link #RS384} 020 * <li>{@link #RS512} 021 * <li>{@link #ES256} 022 * <li>{@link #ES384} 023 * <li>{@link #ES512} 024 * </ul> 025 * 026 * <p>Additional JWS algorithm names can be defined using the constructors. 027 * 028 * @author Vladimir Dzhuvinov 029 * @version $version$ (2013-01-08) 030 */ 031 @Immutable 032 public final class JWSAlgorithm extends Algorithm { 033 034 035 /** 036 * HMAC using SHA-256 hash algorithm (required). 037 */ 038 public static final JWSAlgorithm HS256 = new JWSAlgorithm("HS256", Requirement.REQUIRED); 039 040 041 /** 042 * HMAC using SHA-384 hash algorithm (optional). 043 */ 044 public static final JWSAlgorithm HS384 = new JWSAlgorithm("HS384", Requirement.OPTIONAL); 045 046 047 /** 048 * HMAC using SHA-512 hash algorithm (optional). 049 */ 050 public static final JWSAlgorithm HS512 = new JWSAlgorithm("HS512", Requirement.OPTIONAL); 051 052 053 /** 054 * RSA using SHA-256 hash algorithm (recommended). 055 */ 056 public static final JWSAlgorithm RS256 = new JWSAlgorithm("RS256", Requirement.RECOMMENDED); 057 058 059 /** 060 * RSA using SHA-384 hash algorithm (optional). 061 */ 062 public static final JWSAlgorithm RS384 = new JWSAlgorithm("RS384", Requirement.OPTIONAL); 063 064 065 /** 066 * RSA using SHA-512 hash algorithm (optional). 067 */ 068 public static final JWSAlgorithm RS512 = new JWSAlgorithm("RS512", Requirement.OPTIONAL); 069 070 071 /** 072 * ECDSA using P-256 curve and SHA-256 hash algorithm (recommended). 073 */ 074 public static final JWSAlgorithm ES256 = new JWSAlgorithm("ES256", Requirement.RECOMMENDED); 075 076 077 /** 078 * ECDSA using P-384 curve and SHA-384 hash algorithm (optional). 079 */ 080 public static final JWSAlgorithm ES384 = new JWSAlgorithm("ES384", Requirement.OPTIONAL); 081 082 083 /** 084 * ECDSA using P-521 curve and SHA-512 hash algorithm (optional). 085 */ 086 public static final JWSAlgorithm ES512 = new JWSAlgorithm("ES512", Requirement.OPTIONAL); 087 088 089 /** 090 * Creates a new JSON Web Signature (JWS) algorithm name. 091 * 092 * @param name The algorithm name. Must not be {@code null}. 093 * @param req The implementation requirement, {@code null} if not 094 * known. 095 */ 096 public JWSAlgorithm(final String name, final Requirement req) { 097 098 super(name, req); 099 } 100 101 102 /** 103 * Creates a new JSON Web Signature (JWS) algorithm name. 104 * 105 * @param name The algorithm name. Must not be {@code null}. 106 */ 107 public JWSAlgorithm(final String name) { 108 109 super(name, null); 110 } 111 112 113 /** 114 * Parses a JWS algorithm from the specified string. 115 * 116 * @param s The string to parse. Must not be {@code null}. 117 * 118 * @return The JWS algorithm (matching standard algorithm constant, else 119 * a newly created algorithm). 120 */ 121 public static JWSAlgorithm parse(final String s) { 122 123 if (s == HS256.getName()) { 124 return HS256; 125 } else if (s == HS384.getName()) { 126 return HS384; 127 } else if (s == HS512.getName()) { 128 return HS512; 129 } else if (s == RS256.getName()) { 130 return RS256; 131 } else if (s == RS384.getName()) { 132 return RS384; 133 } else if (s == RS512.getName()) { 134 return RS512; 135 } else if (s == ES256.getName()) { 136 return ES256; 137 } else if (s == ES384.getName()) { 138 return ES384; 139 } else if (s == ES512.getName()) { 140 return ES512; 141 } else { 142 return new JWSAlgorithm(s); 143 } 144 } 145 }