瀏覽代碼

Extract getAuthenticationTag

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 年之前
父節點
當前提交
f9e50338ee

+ 6 - 1
app/src/main/java/com/owncloud/android/operations/UploadFileOperation.java

@@ -94,6 +94,8 @@ import java.util.List;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
 
+import javax.crypto.Cipher;
+
 import androidx.annotation.CheckResult;
 import androidx.annotation.Nullable;
 
@@ -565,7 +567,10 @@ public class UploadFileOperation extends SyncOperation {
             // IV, always generate new one
             byte[] iv = EncryptionUtils.randomBytes(EncryptionUtils.ivLength);
 
-            EncryptedFile encryptedFile = EncryptionUtils.encryptFile(mFile, key, iv);
+            Cipher cipher = EncryptionUtils.getCipher(Cipher.ENCRYPT_MODE, key, iv);
+            File file = new File(mFile.getStoragePath());
+
+            EncryptedFile encryptedFile = EncryptionUtils.encryptFile(file, cipher);
 
             // new random file name, check if it exists in metadata
             String encryptedFileName = EncryptionUtils.generateUid();

+ 7 - 19
app/src/main/java/com/owncloud/android/utils/EncryptionUtils.java

@@ -566,31 +566,18 @@ public final class EncryptionUtils {
     /*
     ENCRYPTION
      */
-
-    /**
-     * @param ocFile             file do crypt
-     * @param encryptionKeyBytes key, either from metadata or {@link EncryptionUtils#generateKey()}
-     * @param iv                 initialization vector, either from metadata or
-     *                           {@link EncryptionUtils#randomBytes(int)}
-     * @return encryptedFile with encryptedBytes and authenticationTag
-     */
-    public static EncryptedFile encryptFile(OCFile ocFile, byte[] encryptionKeyBytes, byte[] iv)
-        throws NoSuchAlgorithmException,
-        InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException, IOException, InvalidParameterSpecException {
-        File file = new File(ocFile.getStoragePath());
-
-        Cipher cipher = getCipher(Cipher.ENCRYPT_MODE, encryptionKeyBytes, iv);
-        return encryptFile(file, cipher);
-    }
-
     public static EncryptedFile encryptFile(File file, Cipher cipher) throws IOException, InvalidParameterSpecException {
         File encryptedFile = new File(file.getAbsolutePath() + ".enc");
         encryptFileWithGivenCipher(file, encryptedFile, cipher);
-        byte[] authenticationTag = cipher.getParameters().getParameterSpec(GCMParameterSpec.class).getIV();
-        String authenticationTagString = encodeBytesToBase64String(authenticationTag);
+        String authenticationTagString = getAuthenticationTag(cipher);
         return new EncryptedFile(encryptedFile, authenticationTagString);
     }
 
+    private static String getAuthenticationTag(Cipher cipher) throws InvalidParameterSpecException {
+        byte[] authenticationTag = cipher.getParameters().getParameterSpec(GCMParameterSpec.class).getIV();
+        return encodeBytesToBase64String(authenticationTag);
+    }
+
     public static Cipher getCipher(int mode, byte[] encryptionKeyBytes, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException {
         Cipher cipher = Cipher.getInstance(AES_CIPHER);
         Key key = new SecretKeySpec(encryptionKeyBytes, AES);
@@ -623,6 +610,7 @@ public final class EncryptionUtils {
                                      ArbitraryDataProvider arbitraryDataProvider,
                                      User user)
         throws BadPaddingException, IllegalBlockSizeException, IOException {
+
         RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
         byte[] fileBytes = new byte[(int) randomAccessFile.length()];
         randomAccessFile.readFully(fileBytes);