001package com.nimbusds.jose.jwk.source;
002
003
004import javax.crypto.SecretKey;
005
006import com.nimbusds.jose.jwk.JWKSet;
007import com.nimbusds.jose.jwk.OctetSequenceKey;
008import com.nimbusds.jose.proc.SecurityContext;
009import net.jcip.annotations.Immutable;
010
011
012/**
013 * JSON Web Key (JWK) source backed by an immutable secret.
014 *
015 * @author Vladimir Dzhuvinov
016 * @version 2016-04-10
017 */
018@Immutable
019public class ImmutableSecret<C extends SecurityContext> extends ImmutableJWKSet<C> {
020        
021
022        /**
023         * Creates a new JSON Web Key (JWK) source backed by an immutable
024         * secret.
025         *
026         * @param secret The secret. Must not be empty or {@code null}.
027         */
028        public ImmutableSecret(final byte[] secret) {
029
030                super(new JWKSet(new OctetSequenceKey.Builder(secret).build()));
031        }
032
033
034        /**
035         * Creates a new JSON Web Key (JWK) source backed by an immutable
036         * secret key.
037         *
038         * @param secretKey The secret key. Must not be {@code null}.
039         */
040        public ImmutableSecret(final SecretKey secretKey) {
041
042                super(new JWKSet(new OctetSequenceKey.Builder(secretKey).build()));
043        }
044
045
046        /**
047         * Returns the secret.
048         *
049         * @return The secret.
050         */
051        public byte[] getSecret() {
052
053                return ((OctetSequenceKey) getJWKSet().getKeys().get(0)).toByteArray();
054        }
055
056
057        /**
058         * Returns the secret key.
059         *
060         * @return The secret key.
061         */
062        public SecretKey getSecretKey() {
063
064                return ((OctetSequenceKey) getJWKSet().getKeys().get(0)).toSecretKey();
065        }
066}