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