Aqara Home v2.4.1版本的 MD5 值为:529779ac2bc0131db52a1fc3098d2b35

以下内容为反编译后的 EncryptionUtils.java 源代码,内容仅作参考


package com.amazonaws.services.s3.internal.crypto;

import com.amazonaws.AmazonClientException;
import com.amazonaws.services.s3.Headers;
import com.amazonaws.services.s3.internal.InputSubstream;
import com.amazonaws.services.s3.internal.RepeatableCipherInputStream;
import com.amazonaws.services.s3.internal.RepeatableFileInputStream;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.EncryptionMaterials;
import com.amazonaws.services.s3.model.EncryptionMaterialsAccessor;
import com.amazonaws.services.s3.model.EncryptionMaterialsProvider;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.amazonaws.services.s3.model.StaticEncryptionMaterialsProvider;
import com.amazonaws.services.s3.model.UploadPartRequest;
import com.amazonaws.services.s3.util.Mimetypes;
import com.amazonaws.util.Base64;
import com.amazonaws.util.LengthCheckInputStream;
import com.amazonaws.util.StringUtils;
import com.amazonaws.util.json.JsonUtils;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
@Deprecated
public class EncryptionUtils {
    public static final String a = ".instruction";

    @Deprecated
    public static PutObjectRequest a(PutObjectRequest putObjectRequest, EncryptionMaterials encryptionMaterials, Provider provider) {
        EncryptionInstruction b = b(encryptionMaterials, provider);
        PutObjectRequest b2 = b(putObjectRequest, b);
        c(putObjectRequest, b);
        return b2;
    }

    @Deprecated
    public static EncryptionInstruction b(EncryptionMaterials encryptionMaterials, Provider provider) {
        return a(new StaticEncryptionMaterialsProvider(encryptionMaterials), provider);
    }

    @Deprecated
    public static S3Object c(S3Object s3Object, EncryptionMaterials encryptionMaterials, Provider provider) {
        return a(s3Object, b(s3Object, encryptionMaterials, provider));
    }

    @Deprecated
    public static EncryptionInstruction b(S3Object s3Object, EncryptionMaterials encryptionMaterials, Provider provider) {
        return b(s3Object, new StaticEncryptionMaterialsProvider(encryptionMaterials), provider);
    }

    public static EncryptionInstruction b(S3Object s3Object, EncryptionMaterialsProvider encryptionMaterialsProvider, Provider provider) {
        ObjectMetadata g2 = s3Object.g();
        byte[] a2 = a(Headers.T, g2);
        byte[] a3 = a(Headers.V, g2);
        Map<String, String> a4 = a(b(Headers.W, g2));
        if (a2 != null && a3 != null) {
            EncryptionMaterials a5 = a(a4, encryptionMaterialsProvider);
            if (a5 != null) {
                SecretKey a6 = a(a2, a5, provider);
                return new EncryptionInstruction(a4, a2, a6, new CipherFactory(a6, 2, a3, provider));
            }
            throw new AmazonClientException(String.format("Unable to retrieve the encryption materials that originally encrypted file '%s' in bucket '%s'.", s3Object.e(), s3Object.a()));
        }
        throw new AmazonClientException(String.format("Necessary encryption info not found in the headers of file '%s' in bucket '%s'", s3Object.e(), s3Object.a()));
    }

    public static void c(PutObjectRequest putObjectRequest, EncryptionInstruction encryptionInstruction) {
        byte[] b = encryptionInstruction.b();
        Cipher d = encryptionInstruction.d();
        Map<String, String> c = encryptionInstruction.c();
        ObjectMetadata o2 = putObjectRequest.o();
        if (o2 == null) {
            o2 = new ObjectMetadata();
        }
        if (putObjectRequest.getFile() != null) {
            o2.l(Mimetypes.a().a(putObjectRequest.getFile()));
        }
        a(o2, b, d, c);
        putObjectRequest.a(o2);
    }

    public static EncryptionInstruction a(EncryptionMaterialsProvider encryptionMaterialsProvider, Provider provider) {
        return a(encryptionMaterialsProvider.a(), provider);
    }

    public static EncryptionInstruction a(EncryptionMaterialsProvider encryptionMaterialsProvider, Map<String, String> map, Provider provider) {
        return a(encryptionMaterialsProvider.a(map), provider);
    }

    public static EncryptionInstruction a(EncryptionMaterials encryptionMaterials, Provider provider) {
        SecretKey a2 = a();
        CipherFactory cipherFactory = new CipherFactory(a2, 1, null, provider);
        return new EncryptionInstruction(encryptionMaterials.d(), a(a2, encryptionMaterials, provider), a2, cipherFactory);
    }

    @Deprecated
    public static EncryptionInstruction a(S3Object s3Object, EncryptionMaterials encryptionMaterials, Provider provider) {
        return a(s3Object, new StaticEncryptionMaterialsProvider(encryptionMaterials), provider);
    }

    public static EncryptionInstruction a(S3Object s3Object, EncryptionMaterialsProvider encryptionMaterialsProvider, Provider provider) {
        Map<String, String> c = c(s3Object);
        Map<String, String> a2 = a(c.get(Headers.W));
        byte[] decode = Base64.decode(c.get(Headers.T));
        byte[] decode2 = Base64.decode(c.get(Headers.V));
        if (decode != null && decode2 != null) {
            EncryptionMaterials a3 = a(a2, encryptionMaterialsProvider);
            if (a3 != null) {
                SecretKey a4 = a(decode, a3, provider);
                return new EncryptionInstruction(a2, decode, a4, new CipherFactory(a4, 2, decode2, provider));
            }
            throw new AmazonClientException(String.format("Unable to retrieve the encryption materials that originally encrypted object corresponding to instruction file '%s' in bucket '%s'.", s3Object.e(), s3Object.a()));
        }
        throw new AmazonClientException(String.format("Necessary encryption info not found in the instruction file '%s' in bucket '%s'", s3Object.e(), s3Object.a()));
    }

    public static Map<String, String> c(S3Object s3Object) {
        try {
            return JsonUtils.b(a(s3Object.f()));
        } catch (Exception e2) {
            throw new AmazonClientException("Error parsing JSON instruction file: " + e2.getMessage());
        }
    }

    public static PutObjectRequest b(PutObjectRequest putObjectRequest, EncryptionInstruction encryptionInstruction) {
        ObjectMetadata o2 = putObjectRequest.o();
        if (o2 == null) {
            o2 = new ObjectMetadata();
        }
        if (o2.n() != null) {
            o2.a(Headers.Z, o2.n());
        }
        o2.k(null);
        long a2 = a(putObjectRequest, o2);
        if (a2 >= 0) {
            o2.a(Headers.Y, Long.toString(a2));
        }
        long a3 = a(encryptionInstruction.d(), putObjectRequest, o2);
        if (a3 >= 0) {
            o2.a(a3);
        }
        putObjectRequest.a(o2);
        putObjectRequest.a(a(putObjectRequest, encryptionInstruction.a(), a2));
        putObjectRequest.a((File) null);
        return putObjectRequest;
    }

    public static S3Object a(S3Object s3Object, EncryptionInstruction encryptionInstruction) {
        S3ObjectInputStream f2 = s3Object.f();
        s3Object.a(new S3ObjectInputStream(new RepeatableCipherInputStream(f2, encryptionInstruction.a()), f2.f()));
        return s3Object;
    }

    public static boolean b(S3Object s3Object) {
        Map<String, String> A = s3Object.g().A();
        return A != null && A.containsKey(Headers.V) && A.containsKey(Headers.T);
    }

    public static PutObjectRequest a(PutObjectRequest putObjectRequest, EncryptionInstruction encryptionInstruction) {
        byte[] bytes = JsonUtils.a(a(encryptionInstruction)).getBytes(StringUtils.b);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        ObjectMetadata o2 = putObjectRequest.o();
        o2.a(bytes.length);
        o2.a(Headers.X, "");
        putObjectRequest.b(putObjectRequest.n() + a);
        putObjectRequest.a(o2);
        putObjectRequest.a(byteArrayInputStream);
        return putObjectRequest;
    }

    public static String b(String str, ObjectMetadata objectMetadata) {
        Map<String, String> A = objectMetadata.A();
        if (A == null || !A.containsKey(str)) {
            return null;
        }
        return A.get(str);
    }

    public static long b(long j2) {
        return j2 + (16 - (j2 % 16)) + 16;
    }

    public static PutObjectRequest a(String str, String str2, EncryptionInstruction encryptionInstruction) {
        byte[] bytes = JsonUtils.a(a(encryptionInstruction)).getBytes(StringUtils.b);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.a(bytes.length);
        objectMetadata.a(Headers.X, "");
        return new PutObjectRequest(str, str2 + a, byteArrayInputStream, objectMetadata);
    }

    public static GetObjectRequest a(GetObjectRequest getObjectRequest) {
        String k2 = getObjectRequest.k();
        return new GetObjectRequest(k2, getObjectRequest.l() + a, getObjectRequest.v());
    }

    public static DeleteObjectRequest a(DeleteObjectRequest deleteObjectRequest) {
        String k2 = deleteObjectRequest.k();
        return new DeleteObjectRequest(k2, deleteObjectRequest.l() + a);
    }

    public static boolean a(S3Object s3Object) {
        Map<String, String> A;
        if (s3Object == null || (A = s3Object.g().A()) == null) {
            return false;
        }
        return A.containsKey(Headers.X);
    }

    public static long[] a(long[] jArr) {
        if (jArr == null || jArr[0] > jArr[1]) {
            return null;
        }
        return new long[]{a(jArr[0]), b(jArr[1])};
    }

    public static S3Object a(S3Object s3Object, long[] jArr) {
        if (jArr == null || jArr[0] > jArr[1]) {
            return s3Object;
        }
        try {
            S3ObjectInputStream f2 = s3Object.f();
            s3Object.a(new S3ObjectInputStream(new AdjustedRangeInputStream(f2, jArr[0], jArr[1]), f2.f()));
            return s3Object;
        } catch (IOException e2) {
            throw new AmazonClientException("Error adjusting output to desired byte range: " + e2.getMessage());
        }
    }

    public static SecretKey a() {
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(256, new SecureRandom());
            return keyGenerator.generateKey();
        } catch (NoSuchAlgorithmException e2) {
            throw new AmazonClientException("Unable to generate envelope symmetric key:" + e2.getMessage(), e2);
        }
    }

    public static Cipher a(SecretKey secretKey, int i2, Provider provider, byte[] bArr) {
        Cipher cipher;
        try {
            if (provider != null) {
                cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", provider);
            } else {
                cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            }
            if (bArr != null) {
                cipher.init(i2, secretKey, new IvParameterSpec(bArr));
            } else {
                cipher.init(i2, secretKey);
            }
            return cipher;
        } catch (Exception e2) {
            throw new AmazonClientException("Unable to build cipher: " + e2.getMessage() + "\nMake sure you have the JCE unlimited strength policy files installed and configured for your JVM: http://www.ngs.ac.uk/tools/jcepolicyfiles", e2);
        }
    }

    public static byte[] a(SecretKey secretKey, EncryptionMaterials encryptionMaterials, Provider provider) {
        Key e2;
        Cipher cipher;
        if (encryptionMaterials.c() != null) {
            e2 = encryptionMaterials.c().getPublic();
        } else {
            e2 = encryptionMaterials.e();
        }
        try {
            byte[] encoded = secretKey.getEncoded();
            if (provider != null) {
                cipher = Cipher.getInstance(e2.getAlgorithm(), provider);
            } else {
                cipher = Cipher.getInstance(e2.getAlgorithm());
            }
            cipher.init(1, e2);
            return cipher.doFinal(encoded);
        } catch (Exception e3) {
            throw new AmazonClientException("Unable to encrypt symmetric key: " + e3.getMessage(), e3);
        }
    }

    public static SecretKey a(byte[] bArr, EncryptionMaterials encryptionMaterials, Provider provider) {
        Key e2;
        Cipher cipher;
        if (encryptionMaterials.c() != null) {
            e2 = encryptionMaterials.c().getPrivate();
        } else {
            e2 = encryptionMaterials.e();
        }
        try {
            if (provider != null) {
                cipher = Cipher.getInstance(e2.getAlgorithm(), provider);
            } else {
                cipher = Cipher.getInstance(e2.getAlgorithm());
            }
            cipher.init(2, e2);
            return new SecretKeySpec(cipher.doFinal(bArr), "AES");
        } catch (Exception e3) {
            throw new AmazonClientException("Unable to decrypt symmetric key from object metadata : " + e3.getMessage(), e3);
        }
    }

    public static InputStream a(PutObjectRequest putObjectRequest, CipherFactory cipherFactory, long j2) {
        try {
            LengthCheckInputStream inputStream = putObjectRequest.getInputStream();
            if (putObjectRequest.getFile() != null) {
                inputStream = new RepeatableFileInputStream(putObjectRequest.getFile());
            }
            if (j2 > -1) {
                inputStream = new LengthCheckInputStream(inputStream, j2, false);
            }
            return new RepeatableCipherInputStream(inputStream, cipherFactory);
        } catch (Exception e2) {
            throw new AmazonClientException("Unable to create cipher input stream: " + e2.getMessage(), e2);
        }
    }

    public static ByteRangeCapturingInputStream a(UploadPartRequest uploadPartRequest, CipherFactory cipherFactory) {
        try {
            InputStream inputStream = uploadPartRequest.getInputStream();
            if (uploadPartRequest.getFile() != null) {
                inputStream = new InputSubstream(new RepeatableFileInputStream(uploadPartRequest.getFile()), uploadPartRequest.l(), uploadPartRequest.s(), uploadPartRequest.v());
            }
            FilterInputStream repeatableCipherInputStream = new RepeatableCipherInputStream(inputStream, cipherFactory);
            FilterInputStream inputSubstream = !uploadPartRequest.v() ? new InputSubstream(repeatableCipherInputStream, 0L, uploadPartRequest.s(), false) : repeatableCipherInputStream;
            long s2 = uploadPartRequest.s();
            return new ByteRangeCapturingInputStream(inputSubstream, s2 - cipherFactory.a().getBlockSize(), s2);
        } catch (Exception e2) {
            throw new AmazonClientException("Unable to create cipher input stream: " + e2.getMessage(), e2);
        }
    }

    public static byte[] a(String str, ObjectMetadata objectMetadata) {
        Map<String, String> A = objectMetadata.A();
        if (A == null || !A.containsKey(str)) {
            return null;
        }
        return Base64.decode(A.get(str));
    }

    public static Map<String, String> a(String str) {
        if (str == null) {
            return null;
        }
        try {
            return JsonUtils.b(str);
        } catch (AmazonClientException e2) {
            throw new AmazonClientException("Unable to parse encryption materials description from metadata :" + e2.getMessage());
        }
    }

    public static void a(ObjectMetadata objectMetadata, byte[] bArr, Cipher cipher, Map<String, String> map) {
        if (bArr != null) {
            objectMetadata.a(Headers.T, Base64.encodeAsString(bArr));
        }
        objectMetadata.a(Headers.V, Base64.encodeAsString(cipher.getIV()));
        objectMetadata.a(Headers.W, JsonUtils.a(map));
    }

    public static ObjectMetadata a(InitiateMultipartUploadRequest initiateMultipartUploadRequest, byte[] bArr, Cipher cipher, Map<String, String> map) {
        ObjectMetadata o2 = initiateMultipartUploadRequest.o();
        if (o2 == null) {
            o2 = new ObjectMetadata();
        }
        a(o2, bArr, cipher, map);
        return o2;
    }

    public static EncryptionMaterials a(Map<String, String> map, EncryptionMaterialsAccessor encryptionMaterialsAccessor) {
        if (encryptionMaterialsAccessor == null) {
            return null;
        }
        return encryptionMaterialsAccessor.a(map);
    }

    public static long a(Cipher cipher, PutObjectRequest putObjectRequest, ObjectMetadata objectMetadata) {
        long a2 = a(putObjectRequest, objectMetadata);
        if (a2 < 0) {
            return -1L;
        }
        long blockSize = cipher.getBlockSize();
        return a2 + (blockSize - (a2 % blockSize));
    }

    public static long a(Cipher cipher, UploadPartRequest uploadPartRequest) {
        long s2;
        if (uploadPartRequest.getFile() != null) {
            if (uploadPartRequest.s() > 0) {
                s2 = uploadPartRequest.s();
            } else {
                s2 = uploadPartRequest.getFile().length();
            }
        } else if (uploadPartRequest.getInputStream() == null) {
            return -1L;
        } else {
            s2 = uploadPartRequest.s();
        }
        long blockSize = cipher.getBlockSize();
        return s2 + (blockSize - (s2 % blockSize));
    }

    public static long a(PutObjectRequest putObjectRequest, ObjectMetadata objectMetadata) {
        if (putObjectRequest.getFile() != null) {
            return putObjectRequest.getFile().length();
        }
        if (putObjectRequest.getInputStream() == null || objectMetadata.a("Content-Length") == null) {
            return -1L;
        }
        return objectMetadata.m();
    }

    public static Map<String, String> a(EncryptionInstruction encryptionInstruction) {
        HashMap hashMap = new HashMap();
        hashMap.put(Headers.W, JsonUtils.a(encryptionInstruction.c()));
        hashMap.put(Headers.T, Base64.encodeAsString(encryptionInstruction.b()));
        hashMap.put(Headers.V, Base64.encodeAsString(encryptionInstruction.d().getIV()));
        return hashMap;
    }

    public static String a(InputStream inputStream) throws IOException {
        if (inputStream == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StringUtils.b));
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine != null) {
                    sb.append(readLine);
                } else {
                    inputStream.close();
                    return sb.toString();
                }
            }
        } catch (Throwable th) {
            inputStream.close();
            throw th;
        }
    }

    public static long a(long j2) {
        long j3 = (j2 - (j2 % 16)) - 16;
        if (j3 < 0) {
            return 0L;
        }
        return j3;
    }
}