package com.epson.mobilephone.common.wifidirect;

import com.google.common.primitives.UnsignedBytes;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

public class NfcMacAddrUtils extends MacAddrUtils {
    private static final int LENGTH_PASSWORD = 8;
    private static final String PREFIX_SSID = "DIRECT-";
    private static final int SUFIX_SSID_INDEX = 4;
    static final char[] acCharacterTblPassphrase = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    static final char[] acCharacterTblPassphraseCL1 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    private static final byte[] password_seed_salt = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1};

    static String getSSIDFromMacAddress(String str) {
        return "DIRECT-" + str.substring(4).toUpperCase(Locale.US);
    }

    static String getPassFormMacAddress(String str, char[] cArr) {
        StringBuffer stringBuffer = new StringBuffer();
        byte[] macAdressBytes = getMacAdressBytes(str);
        byte[] bArr = new byte[(password_seed_salt.length + 6)];
        for (int i = 0; i < 6; i++) {
            bArr[i] = macAdressBytes[(6 - i) - 1];
        }
        byte[] bArr2 = password_seed_salt;
        System.arraycopy(bArr2, 0, bArr, 6, bArr2.length);
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");
            instance.update(bArr);
            byte[] digest = instance.digest();
            for (int i2 = 0; i2 < 8; i2++) {
                stringBuffer.append(cArr[(digest[i2] & UnsignedBytes.MAX_VALUE) % cArr.length]);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }

    static String getMacAddressP2P(String str) {
        byte[] macAdressBytes = getMacAdressBytes(str);
        macAdressBytes[0] = (byte) (macAdressBytes[0] | 2);
        macAdressBytes[4] = (byte) (macAdressBytes[4] ^ UnsignedBytes.MAX_POWER_OF_TWO);
        return getMacAdressStr(macAdressBytes);
    }
}
