001package com.nimbusds.jose.crypto;
002
003
004import net.jcip.annotations.ThreadSafe;
005
006import com.nimbusds.jose.JOSEException;
007import com.nimbusds.jose.JWSSigner;
008import com.nimbusds.jose.ReadOnlyJWSHeader;
009import com.nimbusds.jose.util.Base64URL;
010
011
012
013/**
014 * Message Authentication Code (MAC) signer of 
015 * {@link com.nimbusds.jose.JWSObject JWS objects}. This class is thread-safe.
016 *
017 * <p>Supports the following JSON Web Algorithms (JWAs):
018 *
019 * <ul>
020 *     <li>{@link com.nimbusds.jose.JWSAlgorithm#HS256}
021 *     <li>{@link com.nimbusds.jose.JWSAlgorithm#HS384}
022 *     <li>{@link com.nimbusds.jose.JWSAlgorithm#HS512}
023 * </ul>
024 * 
025 * @author Vladimir Dzhuvinov
026 * @version $version$ (2013-03-22)
027 */
028@ThreadSafe
029public class MACSigner extends MACProvider implements JWSSigner {
030
031
032        /**
033         * Creates a new Message Authentication (MAC) signer.
034         *
035         * @param sharedSecret The shared secret. Must not be {@code null}.
036         */
037        public MACSigner(final byte[] sharedSecret) {
038
039                super(sharedSecret);
040        }
041
042
043        @Override
044        public Base64URL sign(final ReadOnlyJWSHeader header, final byte[] signableContent)
045                throws JOSEException {
046
047                String jcaAlg = getJCAAlgorithmName(header.getAlgorithm());
048
049                byte[] hmac = HMAC.compute(jcaAlg, getSharedSecret(), signableContent);
050
051                return Base64URL.encode(hmac);
052        }
053}