001package com.nimbusds.jose.jca;
002
003
004import java.security.Provider;
005import java.security.SecureRandom;
006
007
008/**
009 * Java Cryptography Architecture (JCA) context, consisting of a JCA
010 * {@link java.security.Provider provider} and
011 * {@link java.security.SecureRandom secure random generator}.
012 *
013 * @author Vladimir Dzhuvinov
014 * @version 2015-06-08
015 */
016public class JCAContext {
017
018
019        /**
020         * The JCA provider.
021         */
022        private Provider provider;
023
024
025        /**
026         * The secure random generator.
027         */
028        private SecureRandom randomGen;
029
030
031        /**
032         * Creates a new default JCA context.
033         */
034        public JCAContext() {
035
036                this(null, null);
037        }
038
039
040        /**
041         * Creates a new JCA context.
042         *
043         * @param provider  The JCA provider, {@code null} to use the default
044         *                  system one.
045         * @param randomGen The specific secure random generator, {@code null}
046         *                  to use the default system one.
047         */
048        public JCAContext(final Provider provider, final SecureRandom randomGen) {
049
050                this.provider = provider;
051                this.randomGen = randomGen;
052        }
053
054
055        /**
056         * Gets the JCA provider to be used for all operations.
057         *
058         * @return The JCA provider to be used for all operations where a more
059         *         specific one is absent, {@code null} implies the default
060         *         system provider.
061         */
062        public Provider getProvider() {
063
064                return provider;
065        }
066
067
068        /**
069         * Sets the JCA provider to be used for all operations.
070         *
071         * @param provider The JCA provider to be used for all operations where
072         *                 a more specific one is absent, {@code null} to use
073         *                 the default system provider.
074         */
075        public void setProvider(final Provider provider) {
076
077                this.provider = provider;
078        }
079
080
081        /**
082         * Gets the secure random generator. Intended for generation of
083         * initialisation vectors and other purposes that require a secure
084         * random generator.
085         *
086         * @return The specific secure random generator (if available), else
087         *         the default system one.
088         */
089        public SecureRandom getSecureRandom() {
090
091                return randomGen != null ? randomGen : new SecureRandom();
092        }
093
094
095        /**
096         * Sets a specific secure random generator for the initialisation
097         * vector and other purposes requiring a random number.
098         *
099         * @param randomGen The secure random generator, {@code null} to use
100         *                  the default system one.
101         */
102        public void setSecureRandom(final SecureRandom randomGen) {
103
104                this.randomGen = randomGen;
105        }
106}