/** * Constructs a new instance of {@code SecureRandom}. An implementation for * the highest-priority provider is returned. The constructed instance will * not have been seeded. */ public SecureRandom() : base(0) { Provider.Service service = findService(); if (service == null) { this.provider = null; this.secureRandomSpi = new SHA1PRNG_SecureRandomImpl(); this.algorithm = "SHA1PRNG"; //$NON-NLS-1$ } else { try { this.provider = service.getProvider(); this.secureRandomSpi = (SecureRandomSpi)service.newInstance(null); this.algorithm = service.getAlgorithm(); } catch (java.lang.Exception e) { throw new RuntimeException(e); } } }
/** * Constructs a new instance of {@code Service} with the given * attributes. * * @param provider * the provider to which this service belongs. * @param type * the type of this service (for example {@code * KeyPairGenerator}). * @param algorithm * the algorithm this service implements. * @param className * the name of the class implementing this service. * @param aliases * {@code List} of aliases for the algorithm name, or {@code * null} if the implemented algorithm has no aliases. * @param attributes * {@code Map} of additional attributes, or {@code null} if * this {@code Service} has no attributed. * @throws NullPointerException * if {@code provider, type, algorithm} or {@code className} * is {@code null}. */ public Service(Provider provider, String type, String algorithm, String className, java.util.List<String> aliases, java.util.Map<String, String> attributes) { if (provider == null || type == null || algorithm == null || className == null) { throw new java.lang.NullPointerException(); } this.provider = provider; this.type = type; this.algorithm = algorithm; this.className = className; this.aliases = aliases; this.attributes = attributes; }
// MessageDigestImpl ctor internal MessageDigestImpl(MessageDigestSpi messageDigestSpi, Provider provider, String algorithm) : base(algorithm) { base.provider = provider; spiImpl = messageDigestSpi; }
/** * Returns a new instance of {@code MessageDigest} that utilizes the * specified algorithm from the specified provider. * * @param algorithm * the name of the algorithm to use * @param provider * the provider * @return a new instance of {@code MessageDigest} that utilizes the * specified algorithm from the specified provider * @throws NoSuchAlgorithmException * if the specified algorithm is not available * @throws NullPointerException * if {@code algorithm} is {@code null} */ public static MessageDigest getInstance(String algorithm, Provider provider) { //throws NoSuchAlgorithmException { if (provider == null) { throw new java.lang.IllegalArgumentException("Provider is null"); //$NON-NLS-1$ } if (algorithm == null) { throw new java.lang.NullPointerException("Algorithm is null"); //$NON-NLS-1$ } MessageDigest result; lock (engine) { engine.getInstance(algorithm, provider, null); if (engine.spi is MessageDigest) { result = (MessageDigest)engine.spi; result.algorithm = algorithm; result.provider = provider; return result; } result = new MessageDigestImpl((MessageDigestSpi)engine.spi, provider, algorithm); return result; } }
// Access to Provider.getService() public Provider.Service getService(Provider p, String type) { return p.getService(type); }
// Access to Security.getAliases() public java.util.Iterator<String> getAliases(Provider.Service s) { return s.getAliases(); }
/** * Insert the given {@code Provider} at the specified {@code position}. The * positions define the preference order in which providers are searched for * requested algorithms. * <p/> * If a {@code SecurityManager} is installed, code calling this method needs * the {@code SecurityPermission} {@code insertProvider.NAME} (where NAME is * the provider name) to be granted, otherwise a {@code SecurityException} * will be thrown. * * @param provider * the provider to insert. * @param position * the position (starting from 1). * @return the actual position or {@code -1} if the given {@code provider} * was already in the list. The actual position may be different * from the desired position. * @throws SecurityException * if a {@code SecurityManager} is installed and the caller does * not have permission to invoke this method. */ public static int insertProviderAt(Provider provider, int position) { lock (lockJ) { // check security access; check that provider is not already // installed, else return -1; if (position <1) or (position > max // position) position = max position + 1; insert provider, shift up // one position for next providers; Note: The position is 1-based java.lang.SecurityManager sm = java.lang.SystemJ.getSecurityManager(); if (sm != null) { sm.checkSecurityAccess("insertProvider." + provider.getName()); //$NON-NLS-1$ } if (getProvider(provider.getName()) != null) { return -1; } int result = Services.insertProviderAt(provider, position); renumProviders(); return result; } }
/** * Adds the given {@code provider} to the collection of providers at the * next available position. * <p/> * If a {@code SecurityManager} is installed, code calling this method needs * the {@code SecurityPermission} {@code insertProvider.NAME} (where NAME is * the provider name) to be granted, otherwise a {@code SecurityException} * will be thrown. * * @param provider * the provider to be added. * @return the actual position or {@code -1} if the given {@code provider} * was already in the list. * @throws SecurityException * if a {@code SecurityManager} is installed and the caller does * not have permission to invoke this method. */ public static int addProvider(Provider provider) { return insertProviderAt(provider, 0); }
/** * Returns a new instance of {@code SecureRandom} that utilizes the * specified algorithm from the specified provider. * * @param algorithm * the name of the algorithm to use. * @param provider * the security provider. * @return a new instance of {@code SecureRandom} that utilizes the * specified algorithm from the specified provider. * @throws NoSuchAlgorithmException * if the specified algorithm is not available. * @throws NullPointerException * if {@code algorithm} is {@code null}. */ public static SecureRandom getInstance(String algorithm, Provider provider) { //throws NoSuchAlgorithmException { if (provider == null) { throw new java.lang.IllegalArgumentException(Messages.getString("security.04")); //$NON-NLS-1$ } if (algorithm == null) { throw new java.lang.NullPointerException(Messages.getString("security.01")); //$NON-NLS-1$ } lock (engine) { engine.getInstance(algorithm, provider, null); return new SecureRandom((SecureRandomSpi)engine.spi, provider, algorithm); } }
// Constructor private SecureRandom(SecureRandomSpi secureRandomSpi, Provider provider, String algorithm) : base(0) { this.provider = provider; this.algorithm = algorithm; this.secureRandomSpi = secureRandomSpi; }
/** * Constructs a new instance of {@code SecureRandom} using the given * implementation from the specified provider. * * @param secureRandomSpi * the implementation. * @param provider * the security provider. */ protected SecureRandom(SecureRandomSpi secureRandomSpi, Provider provider) : this(secureRandomSpi, provider, "unknown") { //$NON-NLS-1$ }