Пример #1
0
  static KeyPairGenerator getKeyPairGenerator(final String algorithm, final Provider provider)
      throws NoSuchAlgorithmException {
    final Object spi = getImplEngine("KeyPairGenerator", algorithm);
    if (spi == null) {
      throw new NoSuchAlgorithmException(algorithm + " KeyPairGenerator not available");
    }

    final KeyPairGenerator keyPairGenerator;
    if (spi instanceof KeyPairGenerator) {
      keyPairGenerator = (KeyPairGenerator) spi;
    } else {
      final Class<? extends KeyPairGenerator> delegate;
      try {
        delegate =
            (Class<? extends KeyPairGenerator>)
                Class.forName(KeyPairGenerator.class.getName() + "$Delegate");
      } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
      }

      keyPairGenerator =
          newInstance(
              delegate, new Class[] {KeyPairGeneratorSpi.class, String.class}, spi, algorithm);
    }
    setField(keyPairGenerator, KeyPairGenerator.class, "provider", provider);
    return keyPairGenerator;
  }
Пример #2
0
  static Signature getSignature(final String algorithm, final Provider provider)
      throws NoSuchAlgorithmException {
    final Object spi = getImplEngine("Signature", algorithm);
    if (spi == null) throw new NoSuchAlgorithmException(algorithm + " Signature not available");

    final Signature signature;
    if (spi instanceof Signature) {
      signature = (Signature) spi;
    } else {
      final Class<? extends Signature> delegate;
      try {
        delegate =
            (Class<? extends Signature>) Class.forName(Signature.class.getName() + "$Delegate");
      } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
      }

      signature =
          newInstance(delegate, new Class[] {SignatureSpi.class, String.class}, spi, algorithm);
    }
    setField(signature, Signature.class, "provider", provider);
    return signature;
  }
Пример #3
0
  static MessageDigest getMessageDigest(final String algorithm, final Provider provider)
      throws NoSuchAlgorithmException {
    final Object spi = getImplEngine("MessageDigest", algorithm);
    if (spi == null) throw new NoSuchAlgorithmException(algorithm + " not found");

    final MessageDigest messageDigest;
    if (spi instanceof MessageDigest) {
      messageDigest = (MessageDigest) spi;
    } else {
      final Class<? extends MessageDigest> delegate;
      try {
        delegate =
            (Class<? extends MessageDigest>)
                Class.forName(MessageDigest.class.getName() + "$Delegate");
      } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
      }

      messageDigest =
          newInstance(delegate, new Class[] {MessageDigestSpi.class, String.class}, spi, algorithm);
    }
    setField(messageDigest, MessageDigest.class, "provider", provider);
    return messageDigest;
  }