001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, Connect2id Ltd.
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.jose.jwk.source;
019
020
021import javax.crypto.SecretKey;
022
023import com.nimbusds.jose.jwk.JWKSet;
024import com.nimbusds.jose.jwk.OctetSequenceKey;
025import com.nimbusds.jose.proc.SecurityContext;
026import net.jcip.annotations.Immutable;
027
028
029/**
030 * JSON Web Key (JWK) source backed by an immutable secret.
031 *
032 * @author Vladimir Dzhuvinov
033 * @version 2016-04-10
034 */
035@Immutable
036public class ImmutableSecret<C extends SecurityContext> extends ImmutableJWKSet<C> {
037        
038
039        /**
040         * Creates a new JSON Web Key (JWK) source backed by an immutable
041         * secret.
042         *
043         * @param secret The secret. Must not be empty or {@code null}.
044         */
045        public ImmutableSecret(final byte[] secret) {
046
047                super(new JWKSet(new OctetSequenceKey.Builder(secret).build()));
048        }
049
050
051        /**
052         * Creates a new JSON Web Key (JWK) source backed by an immutable
053         * secret key.
054         *
055         * @param secretKey The secret key. Must not be {@code null}.
056         */
057        public ImmutableSecret(final SecretKey secretKey) {
058
059                super(new JWKSet(new OctetSequenceKey.Builder(secretKey).build()));
060        }
061
062
063        /**
064         * Returns the secret.
065         *
066         * @return The secret.
067         */
068        public byte[] getSecret() {
069
070                return ((OctetSequenceKey) getJWKSet().getKeys().get(0)).toByteArray();
071        }
072
073
074        /**
075         * Returns the secret key.
076         *
077         * @return The secret key.
078         */
079        public SecretKey getSecretKey() {
080
081                return ((OctetSequenceKey) getJWKSet().getKeys().get(0)).toSecretKey();
082        }
083}