001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
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.openid.connect.sdk.claims;
019
020
021import net.jcip.annotations.Immutable;
022
023import com.nimbusds.jose.JWSAlgorithm;
024import com.nimbusds.jose.jwk.Curve;
025import com.nimbusds.oauth2.sdk.id.State;
026
027
028/**
029 * State hash ({@code s_hash}).
030 *
031 * <p>Related specifications:
032 *
033 * <ul>
034 *     <li>Financial Services – Financial API - Part 2: Read and Write API
035 *         Security Profile, section 5.1.
036 * </ul>
037 */
038@Immutable
039public class StateHash extends HashClaim {
040        
041        
042        private static final long serialVersionUID = 6043322975168115376L;
043        
044        
045        /**
046         * Creates a new state hash with the specified value.
047         *
048         * @param value The state hash value. Must not be {@code null}.
049         */
050        public StateHash(final String value) {
051                
052                super(value);
053        }
054        
055        
056        /**
057         * Computes the hash for the specified state and reference JSON
058         * Web Signature (JWS) algorithm.
059         *
060         * @param state The state. Must not be {@code null}.
061         * @param alg   The reference JWS algorithm. Must not be {@code null}.
062         *
063         * @return The state hash, or {@code null} if the JWS algorithm is not
064         *         supported.
065         *
066         * @deprecated Use {@link #compute(State, JWSAlgorithm, Curve)}
067         * instead.
068         */
069        @Deprecated
070        public static StateHash compute(final State state, final JWSAlgorithm alg) {
071                
072                String value = computeValue(state, alg);
073                
074                if (value == null)
075                        return null;
076                
077                return new StateHash(value);
078        }
079        
080        
081        /**
082         * Computes the hash for the specified state and reference JSON
083         * Web Signature (JWS) algorithm.
084         *
085         * @param state The state. Must not be {@code null}.
086         * @param alg   The reference JWS algorithm. Must not be {@code null}.
087         * @param crv   The JWK curve used with the JWS algorithm, {@code null}
088         *              if not applicable.
089         *
090         * @return The state hash, or {@code null} if the JWS algorithm is not
091         *         supported.
092         */
093        public static StateHash compute(final State state,
094                                        final JWSAlgorithm alg,
095                                        final Curve crv) {
096                
097                String value = computeValue(state, alg, crv);
098                
099                if (value == null)
100                        return null;
101                
102                return new StateHash(value);
103        }
104        
105        
106        @Override
107        public boolean equals(final Object object) {
108                
109                return object instanceof StateHash &&
110                        this.toString().equals(object.toString());
111        }
112}