mariosmeim-db commented on a change in pull request #3440:
URL: https://github.com/apache/hadoop/pull/3440#discussion_r752461416



##########
File path: 
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/security/EncryptionAdapter.java
##########
@@ -0,0 +1,147 @@
+/**
+ * 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.hadoop.fs.azurebfs.security;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
+import java.util.Base64;
+import javax.crypto.SecretKey;
+import javax.security.auth.DestroyFailedException;
+import javax.security.auth.Destroyable;
+
+import org.apache.hadoop.thirdparty.com.google.common.base.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.hadoop.fs.azurebfs.extensions.EncryptionContextProvider;
+
+public class EncryptionAdapter implements Destroyable {
+  private final String path;
+  private SecretKey encryptionContext;
+  private SecretKey encryptionKey;
+  private final EncryptionContextProvider provider;
+  private String encodedKey = null;
+  private String encodedKeySHA = null;
+  private static final Logger LOG =
+      LoggerFactory.getLogger(EncryptionAdapter.class);
+
+  public EncryptionAdapter(EncryptionContextProvider provider, String path,
+      byte[] encryptionContext) throws IOException {
+    this(provider, path);
+    Preconditions.checkNotNull(encryptionContext,
+        "Encryption context should not be null.");
+    this.encryptionContext = new ABFSSecretKey(encryptionContext);
+  }
+
+  public EncryptionAdapter(EncryptionContextProvider provider, String path)
+      throws IOException {
+    this.provider = provider;
+    this.path = path;
+  }
+
+  public SecretKey getEncryptionKey() throws IOException {
+    if (encryptionKey != null) {
+      return encryptionKey;
+    }
+    encryptionKey = provider.getEncryptionKey(path, encryptionContext);
+    return encryptionKey;
+  }
+
+  public SecretKey createEncryptionContext() throws IOException {
+    encryptionContext = provider.getEncryptionContext(path);
+    Preconditions.checkNotNull(encryptionContext,
+        "Encryption context should not be null.");
+    return encryptionContext;
+  }
+
+  public void computeKeys() throws IOException {
+    SecretKey key = getEncryptionKey();
+    Preconditions.checkNotNull(key, "Encryption key should not be null.");
+    encodedKey = getBase64EncodedString(new String(key.getEncoded(),

Review comment:
       Why not just `getBase64EncodedString(key.getEncoded())`? I mean, won't 
`new String(key.getEncoded(),StandardCharsets.UTF_8)` basically give us a a key 
of length !=32?




-- 
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.

To unsubscribe, e-mail: [email protected]

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