jeantil commented on a change in pull request #342:
URL: https://github.com/apache/james-project/pull/342#discussion_r602160493



##########
File path: 
server/blob/blob-aes/src/main/java/org/apache/james/blob/aes/AESBlobStoreDAO.java
##########
@@ -0,0 +1,155 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.blob.aes;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.GeneralSecurityException;
+import java.security.NoSuchAlgorithmException;
+import java.security.spec.InvalidKeySpecException;
+
+import javax.crypto.SecretKey;
+import javax.crypto.SecretKeyFactory;
+import javax.crypto.spec.PBEKeySpec;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.james.blob.api.BlobId;
+import org.apache.james.blob.api.BlobStoreDAO;
+import org.apache.james.blob.api.BucketName;
+import org.apache.james.blob.api.ObjectNotFoundException;
+import org.apache.james.blob.api.ObjectStoreIOException;
+import org.reactivestreams.Publisher;
+
+import com.github.fge.lambdas.Throwing;
+import com.google.common.base.Preconditions;
+import com.google.common.io.ByteSource;
+import com.google.crypto.tink.Aead;
+import com.google.crypto.tink.aead.AeadConfig;
+import com.google.crypto.tink.subtle.AesGcmJce;
+
+import reactor.core.publisher.Mono;
+import reactor.core.scheduler.Schedulers;
+
+public class AESBlobStoreDAO implements BlobStoreDAO {
+    private static final byte[] EMPTY_ASSOCIATED_DATA = new byte[0];
+    private static final int PBKDF2_ITERATIONS = 65536;
+    private static final int KEY_SIZE = 256;
+    private static final String SECRET_KEY_FACTORY_ALGORITHM = 
"PBKDF2WithHmacSHA256";
+
+    private final BlobStoreDAO underlying;
+    private final Aead aead;
+
+    public AESBlobStoreDAO(BlobStoreDAO underlying, CryptoConfig cryptoConfig) 
{
+        this.underlying = underlying;
+
+        try {
+            AeadConfig.register();
+
+            SecretKey secretKey = deriveKey(cryptoConfig);
+            aead = new AesGcmJce(secretKey.getEncoded());
+        } catch (GeneralSecurityException e) {
+            throw new RuntimeException("Error while starting AESPayloadCodec", 
e);
+        }
+    }
+
+    private static SecretKey deriveKey(CryptoConfig cryptoConfig) throws 
NoSuchAlgorithmException, InvalidKeySpecException {
+        byte[] saltBytes = cryptoConfig.salt();
+        SecretKeyFactory skf = 
SecretKeyFactory.getInstance(SECRET_KEY_FACTORY_ALGORITHM);
+        PBEKeySpec spec = new PBEKeySpec(cryptoConfig.password(), saltBytes, 
PBKDF2_ITERATIONS, KEY_SIZE);
+        return skf.generateSecret(spec);
+    }
+
+    public byte[] encrypt(byte[] input) {
+        try {
+            return aead.encrypt(input, EMPTY_ASSOCIATED_DATA);
+        } catch (GeneralSecurityException e) {
+            throw new RuntimeException("Unable to build payload for object 
storage, failed to encrypt", e);
+        }
+    }
+
+    public byte[] decrypt(byte[] ciphertext) throws IOException {

Review comment:
       hmm I don have a sha1 but I have this : 
   https://github.com/google/tink/issues/128#issuecomment-420893021 and 
alternatively https://github.com/google/tink/issues/128#issuecomment-433933047 
but I'm not convinced it is a better solution as the implementations in these 
examples starts an unbounded number of threads ... :(




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to