001package com.nimbusds.jose;
002
003
004import 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 *     <li>{@link #PS256}
025 *     <li>{@link #PS384}
026 *     <li>{@link #PS512}
027 * </ul>
028 *
029 * <p>Additional JWS algorithm names can be defined using the constructors.
030 *
031 * @author Vladimir Dzhuvinov
032 * @version $version$ (2013-08-20)
033 */
034@Immutable
035public final class JWSAlgorithm extends Algorithm {
036
037
038        /**
039         * HMAC using SHA-256 hash algorithm (required).
040         */
041        public static final JWSAlgorithm HS256 = new JWSAlgorithm("HS256", Requirement.REQUIRED);
042
043
044        /**
045         * HMAC using SHA-384 hash algorithm (optional).
046         */
047        public static final JWSAlgorithm HS384 = new JWSAlgorithm("HS384", Requirement.OPTIONAL);
048
049
050        /**
051         * HMAC using SHA-512 hash algorithm (optional).
052         */
053        public static final JWSAlgorithm HS512 = new JWSAlgorithm("HS512", Requirement.OPTIONAL);
054
055
056        /**
057         * RSASSA-PKCS-v1_5 using SHA-256 hash algorithm (recommended).
058         */
059        public static final JWSAlgorithm RS256 = new JWSAlgorithm("RS256", Requirement.RECOMMENDED);
060
061
062        /**
063         * RSASSA-PKCS-v1_5 using SHA-384 hash algorithm (optional).
064         */
065        public static final JWSAlgorithm RS384 = new JWSAlgorithm("RS384", Requirement.OPTIONAL);
066
067
068        /**
069         * RSASSA-PKCS-v1_5 using SHA-512 hash algorithm (optional).
070         */
071        public static final JWSAlgorithm RS512 = new JWSAlgorithm("RS512", Requirement.OPTIONAL);
072
073
074        /**
075         * ECDSA using P-256 curve and SHA-256 hash algorithm (recommended).
076         */
077        public static final JWSAlgorithm ES256 = new JWSAlgorithm("ES256", Requirement.RECOMMENDED);
078
079
080        /**
081         * ECDSA using P-384 curve and SHA-384 hash algorithm (optional).
082         */
083        public static final JWSAlgorithm ES384 = new JWSAlgorithm("ES384", Requirement.OPTIONAL);
084
085
086        /**
087         * ECDSA using P-521 curve and SHA-512 hash algorithm (optional).
088         */
089        public static final JWSAlgorithm ES512 = new JWSAlgorithm("ES512", Requirement.OPTIONAL);
090
091
092        /**
093         * RSASSA-PSS using SHA-256 hash algorithm and MGF1 mask generation
094         * function with SHA-256 (optional).
095         */
096        public static final JWSAlgorithm PS256 = new JWSAlgorithm("PS256", Requirement.OPTIONAL);
097
098
099        /**
100         * RSASSA-PSS using SHA-384 hash algorithm and MGF1 mask generation
101         * function with SHA-384 (optional).
102         */
103        public static final JWSAlgorithm PS384 = new JWSAlgorithm("PS384", Requirement.OPTIONAL);
104
105
106        /**
107         * RSASSA-PSS using SHA-512 hash algorithm and MGF1 mask generation
108         * function with SHA-512 (optional).
109         */
110        public static final JWSAlgorithm PS512 = new JWSAlgorithm("PS512", Requirement.OPTIONAL);
111
112
113        /**
114         * Creates a new JSON Web Signature (JWS) algorithm name.
115         *
116         * @param name The algorithm name. Must not be {@code null}.
117         * @param req  The implementation requirement, {@code null} if not 
118         *             known.
119         */
120        public JWSAlgorithm(final String name, final Requirement req) {
121
122                super(name, req);
123        }
124
125
126        /**
127         * Creates a new JSON Web Signature (JWS) algorithm name.
128         *
129         * @param name The algorithm name. Must not be {@code null}.
130         */
131        public JWSAlgorithm(final String name) {
132
133                super(name, null);
134        }
135
136
137        /**
138         * Parses a JWS algorithm from the specified string.
139         *
140         * @param s The string to parse. Must not be {@code null}.
141         *
142         * @return The JWS algorithm (matching standard algorithm constant, else
143         *         a newly created algorithm).
144         */
145        public static JWSAlgorithm parse(final String s) {
146
147                if (s.equals(HS256.getName())) {
148                        return HS256;
149                } else if (s.equals(HS384.getName())) {
150                        return HS384;
151                } else if (s.equals(HS512.getName())) {
152                        return HS512;
153                } else if (s.equals(RS256.getName())) {
154                        return RS256;
155                } else if (s.equals(RS384.getName())) {
156                        return RS384;
157                } else if (s.equals(RS512.getName())) {
158                        return RS512;
159                } else if (s.equals(ES256.getName())) {
160                        return ES256;
161                } else if (s.equals(ES384.getName())) {
162                        return ES384;
163                } else if (s.equals(ES512.getName())) {
164                        return ES512;
165                } else if (s.equals(PS256.getName())) {
166                        return PS256;
167                } else if (s.equals(PS384.getName())) {
168                        return PS384;
169                } else if (s.equals(PS512.getName())) {
170                        return PS512;
171                } else {
172                        return new JWSAlgorithm(s);
173                }
174        }
175}