package eu.saltwaterc.eet;

import java.io.*;
import java.security.*;
import java.security.spec.*;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.EnvironmentVariableCredentialsProvider;

import com.amazonaws.services.s3.model.EncryptionMaterials;
import com.amazonaws.services.s3.model.CryptoConfiguration;
import com.amazonaws.services.s3.model.CryptoMode;

import com.amazonaws.services.s3.AmazonS3EncryptionClient;
import com.amazonaws.services.s3.model.S3Object;

public class S3 {
  public static PublicKey getPublicKey(String file) throws Exception {
    File f = new File(file);
    FileInputStream fis = new FileInputStream(f);
    DataInputStream dis = new DataInputStream(fis);
    byte[] keyBytes = new byte[(int) f.length()];
    dis.readFully(keyBytes);
    dis.close();
    X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    return kf.generatePublic(spec);
  }

  public static PrivateKey getPrivateKey(String file) throws Exception {
    File f = new File(file);
    FileInputStream fis = new FileInputStream(f);
    DataInputStream dis = new DataInputStream(fis);
    byte[] keyBytes = new byte[(int) f.length()];
    dis.readFully(keyBytes);
    dis.close();
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    return kf.generatePrivate(spec);
  }

  public static void main(String[] args) throws Exception {
    KeyPair kp = new KeyPair(getPublicKey("public.der"), getPrivateKey("private.der"));

    AWSCredentials credentials = new EnvironmentVariableCredentialsProvider().getCredentials();
    EncryptionMaterials encryptionMaterials = new EncryptionMaterials(kp);
    CryptoConfiguration cryptoConf = new CryptoConfiguration(CryptoMode.StrictAuthenticatedEncryption);
    // this is deprecated, but the non-deprecated interface is too bloody complicated for a non-Java dev
    // and the documentation is an absolute pile of rubbish
    AmazonS3EncryptionClient s3 = new AmazonS3EncryptionClient(credentials, encryptionMaterials, cryptoConf);

    // read object created by node.js
    S3Object downloadedObject = s3.getObject(System.getenv("EET_BUCKET"), "hello-node.txt");
    BufferedReader in = new BufferedReader(new InputStreamReader(downloadedObject.getObjectContent()));
    String line;
    while ((line = in.readLine()) != null) {
      System.out.println(line);
    }

    // write object to be read by node.js
    s3.putObject(System.getenv("EET_BUCKET"), "hello-java.txt", "hello world! - from AWS SDK for Java to be decrypted in node.js");
    System.out.println("Written hello-java.txt to S3 bucket " + System.getenv("EET_BUCKET"));
  }
}

@@ -123,90 +123,10 @@
 uth.
-AWSCredentials;%0Aimport com.amazonaws.auth.EnvironmentVariableCredentialsProvider;%0A
+*;
 %0Aimp
@@ -159,33 +159,9 @@
 .s3.
-model.EncryptionMaterials
+*
 ;%0Aim
@@ -192,147 +192,13 @@
 ces.
-s3.model.CryptoConfiguration;%0Aimport com.amazonaws.services.s3.model.CryptoMode;%0A%0Aimport com.amazonaws.services.s3.AmazonS3EncryptionClient
+kms.*
 ;%0Aim
@@ -234,24 +234,17 @@
 3.model.
-S3Object
+*
 ;%0A%0Apubli
@@ -1316,16 +1316,74 @@
 .der%22));
+%0A    EncryptionMaterials em = new EncryptionMaterials(kp);
 %0A%0A    AW
@@ -1394,16 +1394,24 @@
 dentials
+Provider
  credent
@@ -1465,31 +1465,20 @@
 er()
-.getCredentials()
 ;%0A    
+Static
 Encr
@@ -1492,16 +1492,24 @@
 aterials
+Provider
  encrypt
@@ -1523,24 +1523,30 @@
 rials = new 
+Static
 EncryptionMa
@@ -1552,19 +1552,27 @@
 aterials
-(kp
+Provider(em
 );%0A    C
@@ -1674,172 +1674,109 @@
 n);%0A
+%0A
     
-// this is deprecated, but the non-deprecated interface is too bloody complicated for a non-Java dev%0A    // and the documentation is an absolute pile of rubbish
+AWSKMS kms = AWSKMSClientBuilder.standard()%0A      .withCredentials(credentials)%0A      .build();%0A
 %0A   
@@ -1798,23 +1798,13 @@
 tion
-Client
  s3 =
- new
  Ama
@@ -1828,53 +1828,197 @@
 ient
-(credentials, encryptionMaterials, cryptoConf
+Builder.standard()%0A      .withCredentials(credentials)%0A      .withEncryptionMaterials(encryptionMaterials)%0A      .withCryptoConfiguration(cryptoConf)%0A      .withKmsClient(kms)%0A      .build(
 );%0A%0A

Use the builder class to remove the deprecated constructor.