< prev index next >

src/java.base/share/classes/javax/crypto/Mac.java

Print this page
rev 15967 : [mq]: GetInstance


 149      * MacSpi implementation from the first
 150      * Provider that supports the specified algorithm is returned.
 151      *
 152      * <p> Note that the list of registered providers may be retrieved via
 153      * the {@link Security#getProviders() Security.getProviders()} method.
 154      *
 155      * @implNote
 156      * The JDK Reference Implementation additionally uses the
 157      * {@code jdk.security.provider.preferred}
 158      * {@link Security#getProperty(String) Security} property to determine
 159      * the preferred provider order for the specified algorithm. This
 160      * may be different than the order of providers returned by
 161      * {@link Security#getProviders() Security.getProviders()}.
 162      *
 163      * @param algorithm the standard name of the requested MAC algorithm.
 164      * See the Mac section in the <a href=
 165      *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Mac">
 166      * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 167      * for information about standard algorithm names.
 168      *
 169      * @return the new {@code Mac} object.
 170      *
 171      * @exception NoSuchAlgorithmException if no Provider supports a
 172      *          MacSpi implementation for the
 173      *          specified algorithm.

 174      *
 175      * @see java.security.Provider
 176      */
 177     public static final Mac getInstance(String algorithm)
 178             throws NoSuchAlgorithmException {

 179         List<Service> services = GetInstance.getServices("Mac", algorithm);
 180         // make sure there is at least one service from a signed provider
 181         Iterator<Service> t = services.iterator();
 182         while (t.hasNext()) {
 183             Service s = t.next();
 184             if (JceSecurity.canUseProvider(s.getProvider()) == false) {
 185                 continue;
 186             }
 187             return new Mac(s, t, algorithm);
 188         }
 189         throw new NoSuchAlgorithmException
 190                                 ("Algorithm " + algorithm + " not available");
 191     }
 192 
 193     /**
 194      * Returns a {@code Mac} object that implements the
 195      * specified MAC algorithm.
 196      *
 197      * <p> A new Mac object encapsulating the
 198      * MacSpi implementation from the specified provider
 199      * is returned.  The specified provider must be registered
 200      * in the security provider list.
 201      *
 202      * <p> Note that the list of registered providers may be retrieved via
 203      * the {@link Security#getProviders() Security.getProviders()} method.
 204      *
 205      * @param algorithm the standard name of the requested MAC algorithm.
 206      * See the Mac section in the <a href=
 207      *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Mac">
 208      * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 209      * for information about standard algorithm names.
 210      *
 211      * @param provider the name of the provider.
 212      *
 213      * @return the new {@code Mac} object.



 214      *
 215      * @exception NoSuchAlgorithmException if a MacSpi
 216      *          implementation for the specified algorithm is not
 217      *          available from the specified provider.
 218      *
 219      * @exception NoSuchProviderException if the specified provider is not
 220      *          registered in the security provider list.
 221      *
 222      * @exception IllegalArgumentException if the {@code provider}
 223      *          is null or empty.
 224      *
 225      * @see java.security.Provider
 226      */
 227     public static final Mac getInstance(String algorithm, String provider)
 228             throws NoSuchAlgorithmException, NoSuchProviderException {

 229         Instance instance = JceSecurity.getInstance
 230                 ("Mac", MacSpi.class, algorithm, provider);
 231         return new Mac((MacSpi)instance.impl, instance.provider, algorithm);
 232     }
 233 
 234     /**
 235      * Returns a {@code Mac} object that implements the
 236      * specified MAC algorithm.
 237      *
 238      * <p> A new Mac object encapsulating the
 239      * MacSpi implementation from the specified Provider
 240      * object is returned.  Note that the specified Provider object
 241      * does not have to be registered in the provider list.
 242      *
 243      * @param algorithm the standard name of the requested MAC algorithm.
 244      * See the Mac section in the <a href=
 245      *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Mac">
 246      * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 247      * for information about standard algorithm names.
 248      *
 249      * @param provider the provider.
 250      *
 251      * @return the new {@code Mac} object.



 252      *
 253      * @exception NoSuchAlgorithmException if a MacSpi
 254      *          implementation for the specified algorithm is not available
 255      *          from the specified Provider object.
 256      *
 257      * @exception IllegalArgumentException if the {@code provider}
 258      *          is null.
 259      *
 260      * @see java.security.Provider
 261      */
 262     public static final Mac getInstance(String algorithm, Provider provider)
 263             throws NoSuchAlgorithmException {

 264         Instance instance = JceSecurity.getInstance
 265                 ("Mac", MacSpi.class, algorithm, provider);
 266         return new Mac((MacSpi)instance.impl, instance.provider, algorithm);
 267     }
 268 
 269     // max number of debug warnings to print from chooseFirstProvider()
 270     private static int warnCount = 10;
 271 
 272     /**
 273      * Choose the Spi from the first provider available. Used if
 274      * delayed provider selection is not possible because init()
 275      * is not the first method called.
 276      */
 277     void chooseFirstProvider() {
 278         if ((spi != null) || (serviceIterator == null)) {
 279             return;
 280         }
 281         synchronized (lock) {
 282             if (spi != null) {
 283                 return;




 149      * MacSpi implementation from the first
 150      * Provider that supports the specified algorithm is returned.
 151      *
 152      * <p> Note that the list of registered providers may be retrieved via
 153      * the {@link Security#getProviders() Security.getProviders()} method.
 154      *
 155      * @implNote
 156      * The JDK Reference Implementation additionally uses the
 157      * {@code jdk.security.provider.preferred}
 158      * {@link Security#getProperty(String) Security} property to determine
 159      * the preferred provider order for the specified algorithm. This
 160      * may be different than the order of providers returned by
 161      * {@link Security#getProviders() Security.getProviders()}.
 162      *
 163      * @param algorithm the standard name of the requested MAC algorithm.
 164      * See the Mac section in the <a href=
 165      *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Mac">
 166      * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 167      * for information about standard algorithm names.
 168      *
 169      * @return the new {@code Mac} object
 170      *
 171      * @throws NoSuchAlgorithmException if no {@code Provider} supports a
 172      *         {@code MacSpi} implementation for the specified algorithm
 173      *
 174      * @throws NullPointerException if {@code algorithm} is {@code null}
 175      *
 176      * @see java.security.Provider
 177      */
 178     public static final Mac getInstance(String algorithm)
 179             throws NoSuchAlgorithmException {
 180         Objects.requireNonNull(algorithm, "null algorithm name");
 181         List<Service> services = GetInstance.getServices("Mac", algorithm);
 182         // make sure there is at least one service from a signed provider
 183         Iterator<Service> t = services.iterator();
 184         while (t.hasNext()) {
 185             Service s = t.next();
 186             if (JceSecurity.canUseProvider(s.getProvider()) == false) {
 187                 continue;
 188             }
 189             return new Mac(s, t, algorithm);
 190         }
 191         throw new NoSuchAlgorithmException
 192                                 ("Algorithm " + algorithm + " not available");
 193     }
 194 
 195     /**
 196      * Returns a {@code Mac} object that implements the
 197      * specified MAC algorithm.
 198      *
 199      * <p> A new Mac object encapsulating the
 200      * MacSpi implementation from the specified provider
 201      * is returned.  The specified provider must be registered
 202      * in the security provider list.
 203      *
 204      * <p> Note that the list of registered providers may be retrieved via
 205      * the {@link Security#getProviders() Security.getProviders()} method.
 206      *
 207      * @param algorithm the standard name of the requested MAC algorithm.
 208      * See the Mac section in the <a href=
 209      *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Mac">
 210      * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 211      * for information about standard algorithm names.
 212      *
 213      * @param provider the name of the provider.
 214      *
 215      * @return the new {@code Mac} object
 216      *
 217      * @throws IllegalArgumentException if the {@code provider}
 218      *         is {@code null} or empty
 219      *
 220      * @throws NoSuchAlgorithmException if a {@code MacSpi}
 221      *         implementation for the specified algorithm is not
 222      *         available from the specified provider
 223      *
 224      * @throws NoSuchProviderException if the specified provider is not
 225      *         registered in the security provider list
 226      *
 227      * @throws NullPointerException if {@code algorithm} is {@code null}

 228      *
 229      * @see java.security.Provider
 230      */
 231     public static final Mac getInstance(String algorithm, String provider)
 232             throws NoSuchAlgorithmException, NoSuchProviderException {
 233         Objects.requireNonNull(algorithm, "null algorithm name");
 234         Instance instance = JceSecurity.getInstance
 235                 ("Mac", MacSpi.class, algorithm, provider);
 236         return new Mac((MacSpi)instance.impl, instance.provider, algorithm);
 237     }
 238 
 239     /**
 240      * Returns a {@code Mac} object that implements the
 241      * specified MAC algorithm.
 242      *
 243      * <p> A new Mac object encapsulating the
 244      * MacSpi implementation from the specified Provider
 245      * object is returned.  Note that the specified Provider object
 246      * does not have to be registered in the provider list.
 247      *
 248      * @param algorithm the standard name of the requested MAC algorithm.
 249      * See the Mac section in the <a href=
 250      *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Mac">
 251      * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 252      * for information about standard algorithm names.
 253      *
 254      * @param provider the provider.
 255      *
 256      * @return the new {@code Mac} object
 257      *
 258      * @throws IllegalArgumentException if the {@code provider} is
 259      *         {@code null}
 260      *
 261      * @throws NoSuchAlgorithmException if a {@code MacSpi}
 262      *         implementation for the specified algorithm is not available
 263      *         from the specified {@code Provider} object
 264      *
 265      * @throws NullPointerException if {@code algorithm} is {@code null}

 266      *
 267      * @see java.security.Provider
 268      */
 269     public static final Mac getInstance(String algorithm, Provider provider)
 270             throws NoSuchAlgorithmException {
 271         Objects.requireNonNull(algorithm, "null algorithm name");
 272         Instance instance = JceSecurity.getInstance
 273                 ("Mac", MacSpi.class, algorithm, provider);
 274         return new Mac((MacSpi)instance.impl, instance.provider, algorithm);
 275     }
 276 
 277     // max number of debug warnings to print from chooseFirstProvider()
 278     private static int warnCount = 10;
 279 
 280     /**
 281      * Choose the Spi from the first provider available. Used if
 282      * delayed provider selection is not possible because init()
 283      * is not the first method called.
 284      */
 285     void chooseFirstProvider() {
 286         if ((spi != null) || (serviceIterator == null)) {
 287             return;
 288         }
 289         synchronized (lock) {
 290             if (spi != null) {
 291                 return;


< prev index next >