public Result Encrypt()

in TestVectors/runtimes/java/src/main/smithy-generated/software/amazon/cryptography/encryptionsdk/wrapped/TestESDK.java [161:270]


  public Result<EncryptOutput, Error> Encrypt(EncryptInput dafnyInput) {
    try {
      software.amazon.cryptography.encryptionsdk.model.EncryptInput nativeInput =
        ToNative.EncryptInput(dafnyInput);

      final CryptoResult<byte[], ?> encryptResult;
      MasterKeyProvider<?> provider = null;

      // Convert will handle supported keyrings directly
      // Returns null for unsupported MKP to allow encryption/decryption with keyrings instead
      if (_prefer_mkp_over_keyring) {
        if (dafnyInput.dtor_keyring().is_Some()) {
          provider =
            createMasterKeyProvider(dafnyInput.dtor_keyring().dtor_value());
        } else if (dafnyInput.dtor_materialsManager().is_Some()) {
          provider =
            createMasterKeyProvider(
              dafnyInput.dtor_materialsManager().dtor_value()
            );
        }
      }

      // Java ESDK is special and you have to set the algorithm suite both in the keyring which the
      // test vectors do, but also in the client itself.
      CryptoAlgorithm cryptoAlgorithm = _getAlgorithmSuite(
        nativeInput.algorithmSuiteId()
      );
      this._impl.setEncryptionAlgorithm(cryptoAlgorithm);

      if (_prefer_mkp_over_keyring && provider != null) {
        // Call decrypt with MKP
        if (Objects.isNull(nativeInput.encryptionContext())) {
          encryptResult =
            this._impl.encryptData(provider, nativeInput.plaintext().array());
        } else {
          encryptResult =
            this._impl.encryptData(
                provider,
                nativeInput.plaintext().array(),
                nativeInput.encryptionContext()
              );
        }
      } else {
        // If the CMM is null, it MUST be a Keyring
        if (Objects.isNull(nativeInput.materialsManager())) {
          // Call decrypt with keyring
          if (Objects.isNull(nativeInput.encryptionContext())) {
            encryptResult =
              this._impl.encryptData(
                  nativeInput.keyring(),
                  nativeInput.plaintext().array()
                );
          } else {
            encryptResult =
              this._impl.encryptData(
                  nativeInput.keyring(),
                  nativeInput.plaintext().array(),
                  nativeInput.encryptionContext()
                );
          }
        } else { // We are in the CMM case
          if (Objects.isNull(nativeInput.encryptionContext())) {
            encryptResult =
              this._impl.encryptData(
                  nativeInput.materialsManager(),
                  nativeInput.plaintext().array()
                );
          } else {
            encryptResult =
              this._impl.encryptData(
                  nativeInput.materialsManager(),
                  nativeInput.plaintext().array(),
                  nativeInput.encryptionContext()
                );
          }
        }
      }
      dafny.DafnySequence<? extends Byte> ciphertext = Simple.ByteSequence(
        encryptResult.getResult()
      );
      DafnyMap<
        ? extends DafnySequence<? extends Byte>,
        ? extends DafnySequence<? extends Byte>
      > encryptionContext =
        software.amazon.cryptography.materialproviders.ToDafny.EncryptionContext(
          encryptResult.getEncryptionContext()
        );
      ESDKAlgorithmSuiteId algorithmSuiteId =
        software.amazon.cryptography.materialproviders.ToDafny.ESDKAlgorithmSuiteId(
          encryptResult.getCryptoAlgorithm().getAlgorithmSuiteId().ESDK()
        );

      EncryptOutput dafnyOutput = new EncryptOutput(
        ciphertext,
        encryptionContext,
        algorithmSuiteId
      );
      return Result.create_Success(
        EncryptOutput._typeDescriptor(),
        Error._typeDescriptor(),
        dafnyOutput
      );
    } catch (RuntimeException ex) {
      return Result.create_Failure(
        EncryptOutput._typeDescriptor(),
        Error._typeDescriptor(),
        ToDafny.Error(ex)
      );
    }
  }