Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mishiranu.dashchan.chan.dvach;
- import android.content.SharedPreferences;
- import android.security.keystore.KeyGenParameterSpec;
- import android.util.Base64;
- import chan.util.StringUtils;
- import com.mishiranu.dashchan.content.MainApplication;
- import java.io.IOException;
- import java.nio.charset.StandardCharsets;
- import java.security.GeneralSecurityException;
- import java.security.InvalidAlgorithmParameterException;
- import java.security.KeyStore;
- import java.security.KeyStoreException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.security.NoSuchProviderException;
- import java.security.cert.CertificateException;
- import java.util.Arrays;
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.GCMParameterSpec;
- /* JADX INFO: compiled from: r8-map-id-69b0d98fa0c655d6ba582ff17a9bb2b718caed2d43ea8140768c75d96d37a9a5 */
- /* JADX INFO: loaded from: classes.dex */
- final class DvachNoCaptchaSecretStore {
- private static final String KEYSTORE_PROVIDER = "AndroidKeyStore";
- private static final String KEY_ALIAS = "slooop_dvach_appid_demo_key_v1";
- private static final String KEY_CIPHERTEXT = "ciphertext";
- private static final String KEY_IV = "iv";
- static final String PREFERENCES_NAME = "dvach_no_captcha_secret";
- private static final String TRANSFORMATION = "AES/GCM/NoPadding";
- private DvachNoCaptchaSecretStore() {
- }
- public static void clear() {
- getPreferences().edit().clear().commit();
- try {
- KeyStore keyStore = KeyStore.getInstance(KEYSTORE_PROVIDER);
- keyStore.load(null);
- if (keyStore.containsAlias(KEY_ALIAS)) {
- keyStore.deleteEntry(KEY_ALIAS);
- }
- } catch (IOException | RuntimeException | GeneralSecurityException unused) {
- }
- }
- private static byte[] decrypt() throws Throwable {
- byte[] bArrDecode;
- Throwable th;
- byte[] bArrDecode2;
- SharedPreferences preferences = getPreferences();
- String string = preferences.getString(KEY_IV, null);
- String string2 = preferences.getString(KEY_CIPHERTEXT, null);
- if (string != null && string2 != null) {
- try {
- bArrDecode = Base64.decode(string, 2);
- try {
- bArrDecode2 = Base64.decode(string2, 2);
- try {
- try {
- SecretKey key = getKey(false);
- if (key == null) {
- if (bArrDecode != null) {
- Arrays.fill(bArrDecode, (byte) 0);
- }
- if (bArrDecode2 != null) {
- Arrays.fill(bArrDecode2, (byte) 0);
- }
- return null;
- }
- Cipher cipher = Cipher.getInstance(TRANSFORMATION);
- cipher.init(2, key, new GCMParameterSpec(128, bArrDecode));
- byte[] bArrDoFinal = cipher.doFinal(bArrDecode2);
- if (bArrDecode != null) {
- Arrays.fill(bArrDecode, (byte) 0);
- }
- if (bArrDecode2 != null) {
- Arrays.fill(bArrDecode2, (byte) 0);
- }
- return bArrDoFinal;
- } catch (IOException | RuntimeException | GeneralSecurityException unused) {
- if (bArrDecode != null) {
- Arrays.fill(bArrDecode, (byte) 0);
- }
- if (bArrDecode2 != null) {
- Arrays.fill(bArrDecode2, (byte) 0);
- }
- return null;
- }
- } catch (Throwable th2) {
- th = th2;
- if (bArrDecode != null) {
- Arrays.fill(bArrDecode, (byte) 0);
- }
- if (bArrDecode2 != null) {
- Arrays.fill(bArrDecode2, (byte) 0);
- }
- throw th;
- }
- } catch (IOException | RuntimeException | GeneralSecurityException unused2) {
- bArrDecode2 = null;
- } catch (Throwable th3) {
- th = th3;
- bArrDecode2 = null;
- }
- } catch (IOException | RuntimeException | GeneralSecurityException unused3) {
- bArrDecode2 = null;
- bArrDecode = null;
- } catch (Throwable th4) {
- bArrDecode = null;
- th = th4;
- bArrDecode2 = null;
- }
- }
- return null;
- }
- private static SecretKey getKey(boolean z) throws NoSuchAlgorithmException, IOException, KeyStoreException, CertificateException, NoSuchProviderException, InvalidAlgorithmParameterException {
- KeyStore keyStore = KeyStore.getInstance(KEYSTORE_PROVIDER);
- keyStore.load(null);
- SecretKey secretKey = (SecretKey) keyStore.getKey(KEY_ALIAS, null);
- if (secretKey != null || !z) {
- return secretKey;
- }
- KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", KEYSTORE_PROVIDER);
- keyGenerator.init(new KeyGenParameterSpec.Builder(KEY_ALIAS, 3).setKeySize(256).setBlockModes("GCM").setEncryptionPaddings("NoPadding").setRandomizedEncryptionRequired(true).build());
- return keyGenerator.generateKey();
- }
- private static SharedPreferences getPreferences() {
- return MainApplication.getInstance().getSharedPreferences(PREFERENCES_NAME, 0);
- }
- public static boolean hasPrivateKey() throws Throwable {
- byte[] bArrDecrypt = decrypt();
- if (bArrDecrypt == null) {
- return false;
- }
- try {
- return bArrDecrypt.length > 0;
- } finally {
- Arrays.fill(bArrDecrypt, (byte) 0);
- }
- }
- public static boolean put(String str) {
- byte[] bytes = str != null ? str.getBytes(StandardCharsets.UTF_8) : null;
- if (bytes != null && bytes.length != 0) {
- try {
- try {
- Cipher cipher = Cipher.getInstance(TRANSFORMATION);
- cipher.init(1, getKey(true));
- byte[] bArrDoFinal = cipher.doFinal(bytes);
- try {
- boolean zCommit = getPreferences().edit().putString(KEY_IV, Base64.encodeToString(cipher.getIV(), 2)).putString(KEY_CIPHERTEXT, Base64.encodeToString(bArrDoFinal, 2)).commit();
- Arrays.fill(bArrDoFinal, (byte) 0);
- Arrays.fill(bytes, (byte) 0);
- return zCommit;
- } catch (Throwable th) {
- Arrays.fill(bArrDoFinal, (byte) 0);
- throw th;
- }
- } catch (Throwable th2) {
- Arrays.fill(bytes, (byte) 0);
- throw th2;
- }
- } catch (IOException | RuntimeException | GeneralSecurityException unused) {
- Arrays.fill(bytes, (byte) 0);
- }
- }
- return false;
- }
- public static String sign(String str) throws Throwable {
- Throwable th;
- byte[] bArrDigest;
- byte[] bArrDecrypt = decrypt();
- if (bArrDecrypt != null && bArrDecrypt.length != 0) {
- byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
- byte[] bArr = new byte[bytes.length + 1 + bArrDecrypt.length];
- System.arraycopy(bytes, 0, bArr, 0, bytes.length);
- bArr[bytes.length] = 124;
- System.arraycopy(bArrDecrypt, 0, bArr, bytes.length + 1, bArrDecrypt.length);
- try {
- bArrDigest = MessageDigest.getInstance("SHA-256").digest(bArr);
- try {
- String hex = StringUtils.formatHex(bArrDigest);
- Arrays.fill(bArrDecrypt, (byte) 0);
- Arrays.fill(bArr, (byte) 0);
- if (bArrDigest != null) {
- Arrays.fill(bArrDigest, (byte) 0);
- }
- return hex;
- } catch (RuntimeException | GeneralSecurityException unused) {
- Arrays.fill(bArrDecrypt, (byte) 0);
- Arrays.fill(bArr, (byte) 0);
- if (bArrDigest != null) {
- Arrays.fill(bArrDigest, (byte) 0);
- }
- return null;
- } catch (Throwable th2) {
- th = th2;
- Arrays.fill(bArrDecrypt, (byte) 0);
- Arrays.fill(bArr, (byte) 0);
- if (bArrDigest != null) {
- Arrays.fill(bArrDigest, (byte) 0);
- }
- throw th;
- }
- } catch (RuntimeException | GeneralSecurityException unused2) {
- bArrDigest = null;
- } catch (Throwable th3) {
- th = th3;
- bArrDigest = null;
- }
- }
- return null;
- }
- }
Add Comment
Please, Sign In to add comment