package k1;

import com.seewo.sdk.model.SDKKeyboardCode;
import e1.AbstractC0453a;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.Principal;
import java.util.Arrays;
import java.util.Random;
import l1.AbstractC0619b;
import l1.C0618a;
import l1.C0620c;
import l1.C0622e;
import l1.C0623f;
import okhttp3.HttpUrl;

/* JADX INFO: renamed from: k1.q, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0599q implements Principal, Serializable {

    /* JADX INFO: renamed from: b0, reason: collision with root package name */
    static String f4983b0;

    /* JADX INFO: renamed from: c0, reason: collision with root package name */
    static String f4984c0;

    /* JADX INFO: renamed from: d0, reason: collision with root package name */
    static String f4985d0;

    /* JADX INFO: renamed from: H, reason: collision with root package name */
    boolean f4990H = false;

    /* JADX INFO: renamed from: I, reason: collision with root package name */
    byte[] f4991I = null;

    /* JADX INFO: renamed from: L, reason: collision with root package name */
    byte[] f4992L = null;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    String f4993b;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    String f4994g;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    String f4995r;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    byte[] f4996x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    byte[] f4997y;

    /* JADX INFO: renamed from: M, reason: collision with root package name */
    private static final int f4979M = AbstractC0453a.d("jcifs.smb.lmCompatibility", 3);

    /* JADX INFO: renamed from: Q, reason: collision with root package name */
    private static final Random f4980Q = new Random();

    /* JADX INFO: renamed from: Z, reason: collision with root package name */
    private static C0622e f4981Z = C0622e.a();

    /* JADX INFO: renamed from: a0, reason: collision with root package name */
    private static final byte[] f4982a0 = {SDKKeyboardCode.FUNCTION_KEY_BOARD_PAGE_UP, SDKKeyboardCode.FUNCTION_KEY_BOARD_SCROLL_LOCK, 83, SDKKeyboardCode.FUNCTION_KEY_BOARD_4, SDKKeyboardCode.FUNCTION_KEY_BOARD_F7, SDKKeyboardCode.FUNCTION_KEY_BOARD_6, SDKKeyboardCode.FUNCTION_KEY_BOARD_7, SDKKeyboardCode.FUNCTION_KEY_BOARD_8};

    /* JADX INFO: renamed from: e0, reason: collision with root package name */
    public static final C0599q f4986e0 = new C0599q(HttpUrl.FRAGMENT_ENCODE_SET, HttpUrl.FRAGMENT_ENCODE_SET, HttpUrl.FRAGMENT_ENCODE_SET);

    /* JADX INFO: renamed from: f0, reason: collision with root package name */
    static final C0599q f4987f0 = new C0599q(HttpUrl.FRAGMENT_ENCODE_SET, HttpUrl.FRAGMENT_ENCODE_SET, HttpUrl.FRAGMENT_ENCODE_SET);

    /* JADX INFO: renamed from: g0, reason: collision with root package name */
    static final C0599q f4988g0 = new C0599q("?", "GUEST", HttpUrl.FRAGMENT_ENCODE_SET);

    /* JADX INFO: renamed from: h0, reason: collision with root package name */
    static final C0599q f4989h0 = new C0599q(null);

    public C0599q(String str) {
        this.f4995r = null;
        this.f4994g = null;
        this.f4993b = null;
        if (str != null) {
            try {
                str = r(str);
            } catch (UnsupportedEncodingException unused) {
            }
            int length = str.length();
            int i2 = 0;
            int i3 = 0;
            while (true) {
                if (i2 >= length) {
                    break;
                }
                char cCharAt = str.charAt(i2);
                if (cCharAt == ';') {
                    this.f4993b = str.substring(0, i2);
                    i3 = i2 + 1;
                } else if (cCharAt == ':') {
                    this.f4995r = str.substring(i2 + 1);
                    break;
                }
                i2++;
            }
            this.f4994g = str.substring(i3, i2);
        }
        o();
        if (this.f4993b == null) {
            this.f4993b = f4983b0;
        }
        if (this.f4994g == null) {
            this.f4994g = f4984c0;
        }
        if (this.f4995r == null) {
            this.f4995r = f4985d0;
        }
    }

    private static void a(byte[] bArr, byte[] bArr2, byte[] bArr3) {
        byte[] bArr4 = new byte[7];
        byte[] bArr5 = new byte[8];
        for (int i2 = 0; i2 < bArr.length / 7; i2++) {
            System.arraycopy(bArr, i2 * 7, bArr4, 0, 7);
            new C0618a(bArr4).e(bArr2, bArr5);
            System.arraycopy(bArr5, 0, bArr3, i2 * 8, 8);
        }
    }

    static byte[] b(byte[] bArr, byte[] bArr2, byte[] bArr3, int i2, int i3) {
        C0620c c0620c = new C0620c(bArr);
        c0620c.update(bArr2);
        c0620c.update(bArr3, i2, i3);
        byte[] bArrDigest = c0620c.digest();
        byte[] bArr4 = new byte[bArrDigest.length + bArr3.length];
        System.arraycopy(bArrDigest, 0, bArr4, 0, bArrDigest.length);
        System.arraycopy(bArr3, 0, bArr4, bArrDigest.length, bArr3.length);
        return bArr4;
    }

    public static byte[] e(String str, String str2, String str3, byte[] bArr, byte[] bArr2) {
        try {
            byte[] bArr3 = new byte[24];
            C0623f c0623f = new C0623f();
            c0623f.update(str3.getBytes("UTF-16LE"));
            C0620c c0620c = new C0620c(c0623f.digest());
            c0620c.update(str2.toUpperCase().getBytes("UTF-16LE"));
            c0620c.update(str.toUpperCase().getBytes("UTF-16LE"));
            C0620c c0620c2 = new C0620c(c0620c.digest());
            c0620c2.update(bArr);
            c0620c2.update(bArr2);
            c0620c2.digest(bArr3, 0, 16);
            System.arraycopy(bArr2, 0, bArr3, 16, 8);
            return bArr3;
        } catch (Exception e2) {
            if (C0622e.f5384g <= 0) {
                return null;
            }
            e2.printStackTrace(f4981Z);
            return null;
        }
    }

    public static byte[] f(byte[] bArr, byte[] bArr2, byte[] bArr3) {
        byte[] bArr4 = new byte[8];
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(bArr2);
            messageDigest.update(bArr3, 0, 8);
            System.arraycopy(messageDigest.digest(), 0, bArr4, 0, 8);
            byte[] bArr5 = new byte[21];
            System.arraycopy(bArr, 0, bArr5, 0, 16);
            byte[] bArr6 = new byte[24];
            a(bArr5, bArr4, bArr6);
            return bArr6;
        } catch (GeneralSecurityException e2) {
            if (C0622e.f5384g > 0) {
                e2.printStackTrace(f4981Z);
            }
            throw new RuntimeException("MD5", e2);
        }
    }

    public static byte[] g(String str, byte[] bArr) {
        byte[] bytes;
        byte[] bArr2 = new byte[21];
        byte[] bArr3 = new byte[24];
        try {
            bytes = str.getBytes("UTF-16LE");
        } catch (UnsupportedEncodingException e2) {
            if (C0622e.f5384g > 0) {
                e2.printStackTrace(f4981Z);
            }
            bytes = null;
        }
        C0623f c0623f = new C0623f();
        c0623f.update(bytes);
        try {
            c0623f.digest(bArr2, 0, 16);
        } catch (Exception e3) {
            if (C0622e.f5384g > 0) {
                e3.printStackTrace(f4981Z);
            }
        }
        a(bArr2, bArr, bArr3);
        return bArr3;
    }

    public static byte[] h(byte[] bArr, byte[] bArr2, byte[] bArr3, long j2, byte[] bArr4) {
        int length = bArr4 != null ? bArr4.length : 0;
        int i2 = length + 28;
        int i3 = length + 32;
        byte[] bArr5 = new byte[i3];
        AbstractC0619b.f(257, bArr5, 0);
        AbstractC0619b.f(0, bArr5, 4);
        AbstractC0619b.g(j2, bArr5, 8);
        System.arraycopy(bArr3, 0, bArr5, 16, 8);
        AbstractC0619b.f(0, bArr5, 24);
        if (bArr4 != null) {
            System.arraycopy(bArr4, 0, bArr5, 28, length);
        }
        AbstractC0619b.f(0, bArr5, i2);
        return b(bArr, bArr2, bArr5, 0, i3);
    }

    public static byte[] j(String str, byte[] bArr) {
        byte[] bArr2 = new byte[14];
        byte[] bArr3 = new byte[21];
        byte[] bArr4 = new byte[24];
        try {
            byte[] bytes = str.toUpperCase().getBytes(U.f4691C);
            int length = bytes.length;
            System.arraycopy(bytes, 0, bArr2, 0, length <= 14 ? length : 14);
            a(bArr2, f4982a0, bArr3);
            a(bArr3, bArr, bArr4);
            return bArr4;
        } catch (UnsupportedEncodingException e2) {
            throw new RuntimeException("Try setting jcifs.encoding=US-ASCII", e2);
        }
    }

    static void o() {
        if (f4983b0 != null) {
            return;
        }
        f4983b0 = AbstractC0453a.h("jcifs.smb.client.domain", "?");
        f4984c0 = AbstractC0453a.h("jcifs.smb.client.username", "GUEST");
        f4985d0 = AbstractC0453a.h("jcifs.smb.client.password", HttpUrl.FRAGMENT_ENCODE_SET);
    }

    public static byte[] p(String str) {
        if (str == null) {
            throw new RuntimeException("Password parameter is required");
        }
        try {
            C0623f c0623f = new C0623f();
            c0623f.update(str.getBytes("UTF-16LE"));
            return c0623f.digest();
        } catch (UnsupportedEncodingException e2) {
            throw new RuntimeException(e2.getMessage());
        }
    }

    public static byte[] q(String str, String str2, String str3) {
        try {
            C0623f c0623f = new C0623f();
            c0623f.update(str3.getBytes("UTF-16LE"));
            C0620c c0620c = new C0620c(c0623f.digest());
            c0620c.update(str2.toUpperCase().getBytes("UTF-16LE"));
            c0620c.update(str.getBytes("UTF-16LE"));
            return c0620c.digest();
        } catch (UnsupportedEncodingException e2) {
            throw new RuntimeException(e2.getMessage());
        }
    }

    static String r(String str) {
        if (str == null) {
            return null;
        }
        int length = str.length();
        char[] cArr = new char[length];
        int i2 = 0;
        int i3 = 0;
        boolean z2 = false;
        while (i2 < length) {
            if (!z2) {
                char cCharAt = str.charAt(i2);
                if (cCharAt == '%') {
                    z2 = true;
                } else {
                    cArr[i3] = cCharAt;
                    i3++;
                }
            } else if (z2) {
                cArr[i3] = new String(new byte[]{(byte) (Integer.parseInt(str.substring(i2, i2 + 2), 16) & 255)}, 0, 1, "ASCII").charAt(0);
                i2++;
                i3++;
                z2 = false;
            }
            i2++;
        }
        return new String(cArr, 0, i3);
    }

    public byte[] c(byte[] bArr) {
        if (this.f4990H) {
            return this.f4996x;
        }
        int i2 = f4979M;
        if (i2 == 0 || i2 == 1) {
            return j(this.f4995r, bArr);
        }
        if (i2 == 2) {
            return g(this.f4995r, bArr);
        }
        if (i2 != 3 && i2 != 4 && i2 != 5) {
            return j(this.f4995r, bArr);
        }
        if (this.f4991I == null) {
            byte[] bArr2 = new byte[8];
            this.f4991I = bArr2;
            f4980Q.nextBytes(bArr2);
        }
        return e(this.f4993b, this.f4994g, this.f4995r, bArr, this.f4991I);
    }

    public String d() {
        return this.f4993b;
    }

    @Override // java.security.Principal
    public boolean equals(Object obj) {
        if (obj instanceof C0599q) {
            C0599q c0599q = (C0599q) obj;
            if (c0599q.f4993b.toUpperCase().equals(this.f4993b.toUpperCase()) && c0599q.f4994g.toUpperCase().equals(this.f4994g.toUpperCase())) {
                boolean z2 = this.f4990H;
                if (z2 && c0599q.f4990H) {
                    return Arrays.equals(this.f4996x, c0599q.f4996x) && Arrays.equals(this.f4997y, c0599q.f4997y);
                }
                if (!z2 && this.f4995r.equals(c0599q.f4995r)) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override // java.security.Principal
    public String getName() {
        if (this.f4993b.length() <= 0 || this.f4993b.equals("?")) {
            return this.f4994g;
        }
        return this.f4993b + "\\" + this.f4994g;
    }

    @Override // java.security.Principal
    public int hashCode() {
        return getName().toUpperCase().hashCode();
    }

    public String i() {
        return this.f4995r;
    }

    public byte[] k(byte[] bArr) throws V {
        int i2 = f4979M;
        if (i2 == 0 || i2 == 1 || i2 == 2) {
            byte[] bArr2 = new byte[40];
            m(bArr, bArr2, 0);
            System.arraycopy(l(bArr), 0, bArr2, 16, 24);
            return bArr2;
        }
        if (i2 == 3 || i2 == 4 || i2 == 5) {
            throw new V("NTLMv2 requires extended security (jcifs.smb.client.useExtendedSecurity must be true if jcifs.smb.lmCompatibility >= 3)");
        }
        return null;
    }

    public byte[] l(byte[] bArr) {
        if (this.f4990H) {
            return this.f4997y;
        }
        int i2 = f4979M;
        return (i2 == 0 || i2 == 1 || i2 == 2) ? g(this.f4995r, bArr) : (i2 == 3 || i2 == 4 || i2 == 5) ? new byte[0] : g(this.f4995r, bArr);
    }

    void m(byte[] bArr, byte[] bArr2, int i2) throws V {
        if (this.f4990H) {
            return;
        }
        try {
            C0623f c0623f = new C0623f();
            c0623f.update(this.f4995r.getBytes("UTF-16LE"));
            int i3 = f4979M;
            if (i3 == 0 || i3 == 1 || i3 == 2) {
                c0623f.update(c0623f.digest());
                c0623f.digest(bArr2, i2, 16);
                return;
            }
            if (i3 != 3 && i3 != 4 && i3 != 5) {
                c0623f.update(c0623f.digest());
                c0623f.digest(bArr2, i2, 16);
                return;
            }
            if (this.f4991I == null) {
                byte[] bArr3 = new byte[8];
                this.f4991I = bArr3;
                f4980Q.nextBytes(bArr3);
            }
            C0620c c0620c = new C0620c(c0623f.digest());
            c0620c.update(this.f4994g.toUpperCase().getBytes("UTF-16LE"));
            c0620c.update(this.f4993b.toUpperCase().getBytes("UTF-16LE"));
            byte[] bArrDigest = c0620c.digest();
            C0620c c0620c2 = new C0620c(bArrDigest);
            c0620c2.update(bArr);
            c0620c2.update(this.f4991I);
            C0620c c0620c3 = new C0620c(bArrDigest);
            c0620c3.update(c0620c2.digest());
            c0620c3.digest(bArr2, i2, 16);
        } catch (Exception e2) {
            throw new V(HttpUrl.FRAGMENT_ENCODE_SET, e2);
        }
    }

    public String n() {
        return this.f4994g;
    }

    @Override // java.security.Principal
    public String toString() {
        return getName();
    }

    public C0599q(String str, String str2, String str3) {
        if (str2 != null) {
            int iIndexOf = str2.indexOf(64);
            if (iIndexOf > 0) {
                str = str2.substring(iIndexOf + 1);
                str2 = str2.substring(0, iIndexOf);
            } else {
                int iIndexOf2 = str2.indexOf(92);
                if (iIndexOf2 > 0) {
                    str = str2.substring(0, iIndexOf2);
                    str2 = str2.substring(iIndexOf2 + 1);
                }
            }
        }
        this.f4993b = str;
        this.f4994g = str2;
        this.f4995r = str3;
        o();
        if (str == null) {
            this.f4993b = f4983b0;
        }
        if (str2 == null) {
            this.f4994g = f4984c0;
        }
        if (str3 == null) {
            this.f4995r = f4985d0;
        }
    }
}
