package client;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.MessageDigest;

import tools.HexTool;

import java.util.Random;

public class LoginCrypto {
    protected static final int extralength = 6;
    private static final String[] Alphabet = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    private static final String[] Number = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9"};
    private static final Random rand = new Random();

    public static String Generate_13DigitAsiasoftPassport() {
        final StringBuilder sb = new StringBuilder();
        sb.append(LoginCrypto.Alphabet[LoginCrypto.rand.nextInt(LoginCrypto.Alphabet.length)]);
        for (int i = 0; i < 11; ++i) {
            sb.append(LoginCrypto.Number[LoginCrypto.rand.nextInt(LoginCrypto.Number.length)]);
        }
        sb.append(LoginCrypto.Alphabet[LoginCrypto.rand.nextInt(LoginCrypto.Alphabet.length)]);
        return sb.toString();
    }

    private static String toSimpleHexString(final byte[] bytes) {
        return HexTool.toString(bytes).replace((CharSequence) " ", (CharSequence) "").toLowerCase();
    }

    private static String hashWithDigest(final String in, final String digest) {
        try {
            final MessageDigest Digester = MessageDigest.getInstance(digest);
            Digester.update(in.getBytes("UTF-8"), 0, in.length());
            final byte[] sha1Hash = Digester.digest();
            return toSimpleHexString(sha1Hash);
        } catch (NoSuchAlgorithmException ex) {
            throw new RuntimeException("Hashing the password failed", (Throwable) ex);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Encoding the string failed", (Throwable) e);
        }
    }

    public static String hexSha1(final String in) {
        return hashWithDigest(in, "SHA-1");
    }

    private static String hexSha512(final String in) {
        return hashWithDigest(in, "SHA-512");
    }

    public static boolean checkSha1Hash(final String hash, final String password) {
        return hash.equals(makeSaltedSha1Hash(password));
    }

    public static boolean checkSaltedSha512Hash(final String hash, final String password, final String salt) {
        return hash.equals(makeSaltedSha512Hash(password, salt));
    }

    public static String makeSaltedSha512Hash(final String password, final String salt) {
        return hexSha512(password + salt);
    }

    public static String makeSaltedSha1Hash(final String password) {
        return hexSha1(password);
    }

    public static String makeSalt() {
        final byte[] salt = new byte[16];
        LoginCrypto.rand.nextBytes(salt);
        return toSimpleHexString(salt);
    }

    public static String rand_s(final String in) {
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 6; ++i) {
            sb.append(LoginCrypto.rand.nextBoolean() ? LoginCrypto.Alphabet[LoginCrypto.rand.nextInt(LoginCrypto.Alphabet.length)] : LoginCrypto.Number[LoginCrypto.rand.nextInt(LoginCrypto.Number.length)]);
        }
        return sb.toString() + in;
    }

    public static String rand_r(final String in) {
        return in.substring(6, 134);
    }
}
