コード例 #1
0
  @Override
  public ListenableFuture<byte[]> unwrapKeyAsync(final byte[] encryptedKey, final String algorithm)
      throws NoSuchAlgorithmException {

    if (encryptedKey == null) {
      throw new IllegalArgumentException("encryptedKey ");
    }

    // Interpret the requested algorithm
    if (Strings.isNullOrWhiteSpace(algorithm)) {
      throw new IllegalArgumentException("algorithm");
    }

    // Interpret the requested algorithm
    Algorithm baseAlgorithm = AlgorithmResolver.Default.get(algorithm);

    if (baseAlgorithm == null || !(baseAlgorithm instanceof AsymmetricEncryptionAlgorithm)) {
      throw new NoSuchAlgorithmException(algorithm);
    }

    AsymmetricEncryptionAlgorithm algo = (AsymmetricEncryptionAlgorithm) baseAlgorithm;

    ICryptoTransform transform;
    ListenableFuture<byte[]> result;

    try {
      transform = algo.CreateDecryptor(_keyPair, _provider);
      result = Futures.immediateFuture(transform.doFinal(encryptedKey));
    } catch (Exception e) {
      result = Futures.immediateFailedFuture(e);
    }

    return result;
  }
コード例 #2
0
  @Override
  public ListenableFuture<Boolean> verifyAsync(
      final byte[] digest, final byte[] signature, final String algorithm)
      throws NoSuchAlgorithmException {

    if (digest == null) {
      throw new IllegalArgumentException("encryptedKey ");
    }

    // Interpret the requested algorithm
    if (Strings.isNullOrWhiteSpace(algorithm)) {
      throw new IllegalArgumentException("algorithm");
    }

    // Interpret the requested algorithm
    Algorithm baseAlgorithm = AlgorithmResolver.Default.get(algorithm);

    if (baseAlgorithm == null || !(baseAlgorithm instanceof AsymmetricSignatureAlgorithm)) {
      throw new NoSuchAlgorithmException(algorithm);
    }

    Rs256 algo = (Rs256) baseAlgorithm;

    ISignatureTransform signer = algo.createSignatureTransform(_keyPair);

    try {
      return Futures.immediateFuture(signer.verify(digest, signature));
    } catch (Exception e) {
      return Futures.immediateFailedFuture(e);
    }
  }
コード例 #3
0
  public RsaKey(String kid, int keySize, Provider provider) throws NoSuchAlgorithmException {

    if (Strings.isNullOrWhiteSpace(kid)) {
      throw new IllegalArgumentException("kid");
    }

    final KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", provider);

    generator.initialize(keySize);

    _kid = kid;
    _keyPair = generator.generateKeyPair();
    _provider = provider;
  }
コード例 #4
0
  public RsaKey(String kid, KeyPair keyPair, Provider provider) {

    if (Strings.isNullOrWhiteSpace(kid)) {
      throw new IllegalArgumentException("kid");
    }

    if (keyPair == null) {
      throw new IllegalArgumentException("kid");
    }

    if (keyPair.getPublic() == null || !(keyPair.getPublic() instanceof RSAPublicKey)) {
      throw new IllegalArgumentException("keyPair");
    }

    _kid = kid;
    _keyPair = keyPair;
    _provider = provider;
  }
コード例 #5
0
  @Override
  public ListenableFuture<Triple<byte[], byte[], String>> encryptAsync(
      final byte[] plaintext,
      final byte[] iv,
      final byte[] authenticationData,
      final String algorithm)
      throws NoSuchAlgorithmException {

    if (plaintext == null) {
      throw new IllegalArgumentException("plaintext");
    }

    // Interpret the requested algorithm
    String algorithmName =
        (Strings.isNullOrWhiteSpace(algorithm) ? getDefaultEncryptionAlgorithm() : algorithm);
    Algorithm baseAlgorithm = AlgorithmResolver.Default.get(algorithmName);

    if (baseAlgorithm == null || !(baseAlgorithm instanceof AsymmetricEncryptionAlgorithm)) {
      throw new NoSuchAlgorithmException(algorithmName);
    }

    AsymmetricEncryptionAlgorithm algo = (AsymmetricEncryptionAlgorithm) baseAlgorithm;

    ICryptoTransform transform;
    ListenableFuture<Triple<byte[], byte[], String>> result;

    try {
      transform = algo.CreateEncryptor(_keyPair, _provider);
      result =
          Futures.immediateFuture(
              Triple.of(transform.doFinal(plaintext), (byte[]) null, algorithmName));
    } catch (Exception e) {
      result = Futures.immediateFailedFuture(e);
    }

    return result;
  }