/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  sun.misc.BASE64Decoder
 *  sun.misc.BASE64Encoder
 */
package com.ifugle.util.export;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AESUtil {
    private static final Charset CHARSET_DEFAULT = Charset.forName("UTF-8");
    private static final String ALGORITHM_MD5 = "MD5";
    private static final String ALGORITHM_AES = "AES";
    private static final String ALGORITHM_PBKDF2_HMACSHA1 = "PBKDF2WithHmacSHA1";
    private static final String CBC_MODE_PADDING = "AES/CBC/PKCS5Padding";
    private static final int DEFAULT_ITERATIONS = 65536;
    private static final int DEFAULT_HASH_WIDTH = 128;
    private static final Map<String, SecretKey> SECRET_KEYS = new HashMap<String, SecretKey>();

    public static String encryptWithZipToBase64(String text, String password) throws Exception {
        return text != null && !text.isEmpty() ? AESUtil.base64Encode(AESUtil.encrypt(AESUtil.zip(AESUtil.getBytes(text)), password)) : text;
    }

    public static String decryptWithUnzipFromBase64(String base64String, String password) throws Exception {
        return base64String != null && !base64String.isEmpty() ? AESUtil.unzip(AESUtil.decrypt(AESUtil.base64Decode(base64String), password)) : base64String;
    }

    public static String encryptToBase64(String text, String password) throws Exception {
        return text != null && !text.isEmpty() ? AESUtil.base64Encode(AESUtil.encrypt(AESUtil.getBytes(text), password)) : text;
    }

    public static String decryptFromBase64(String base64String, String password) throws Exception {
        return base64String != null && !base64String.isEmpty() ? new String(AESUtil.decrypt(AESUtil.base64Decode(base64String), password), CHARSET_DEFAULT) : base64String;
    }

    public static byte[] encrypt(byte[] data, String password) throws Exception {
        return AESUtil.crypt(1, data, password);
    }

    public static byte[] decrypt(byte[] data, String password) throws Exception {
        return AESUtil.crypt(2, data, password);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private static byte[] crypt(int opmode, byte[] data, String password) throws Exception {
        byte[] salt = AESUtil.getMD5(password);
        IvParameterSpec iv = new IvParameterSpec(salt);
        SecretKey secretKey = SECRET_KEYS.get(password);
        if (secretKey == null) {
            Map<String, SecretKey> map = SECRET_KEYS;
            synchronized (map) {
                secretKey = SECRET_KEYS.get(password);
                if (secretKey == null) {
                    SecretKey secretKey1 = AESUtil.getPBESecretKey(password, salt);
                    secretKey = new SecretKeySpec(secretKey1.getEncoded(), ALGORITHM_AES);
                    SECRET_KEYS.put(password, secretKey);
                }
            }
        }
        Cipher cipher1 = Cipher.getInstance(CBC_MODE_PADDING);
        cipher1.init(opmode, (Key)secretKey, iv);
        return cipher1.doFinal(data);
    }

    public static SecretKey getPBESecretKey(String password, byte[] salt) throws Exception {
        SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM_PBKDF2_HMACSHA1);
        PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128);
        return factory.generateSecret(spec);
    }

    private static byte[] getMD5(String text) throws NoSuchAlgorithmException {
        return MessageDigest.getInstance(ALGORITHM_MD5).digest(AESUtil.getBytes(text));
    }

    private static byte[] zip(byte[] bytes) throws IOException {
        byte[] var5;
        ByteArrayOutputStream bos = null;
        DeflaterOutputStream dos = null;
        try {
            Deflater deflater = new Deflater();
            deflater.setLevel(6);
            bos = new ByteArrayOutputStream();
            dos = new DeflaterOutputStream((OutputStream)bos, deflater);
            dos.write(bytes);
            dos.close();
            var5 = bos.toByteArray();
        }
        finally {
            if (bos != null) {
                bos.close();
            }
            if (dos != null) {
                dos.close();
            }
        }
        return var5;
    }

    private static String unzip(byte[] bytes) throws IOException {
        ByteArrayOutputStream bos = null;
        ByteArrayInputStream bis = null;
        InflaterInputStream iis = null;
        try {
            String var7;
            bos = new ByteArrayOutputStream();
            bis = new ByteArrayInputStream(bytes);
            iis = new InflaterInputStream(bis);
            byte[] block = new byte[1024];
            int readLength = iis.read(block);
            while (readLength != -1) {
                bos.write(block, 0, readLength);
                readLength = iis.read(block);
            }
            String string = var7 = new String(bos.toByteArray(), CHARSET_DEFAULT);
            return string;
        }
        finally {
            if (iis != null) {
                iis.close();
            }
            if (bis != null) {
                bis.close();
            }
            if (bos != null) {
                bos.close();
            }
        }
    }

    private static byte[] getBytes(String text) {
        return text == null ? null : text.getBytes(CHARSET_DEFAULT);
    }

    private static String base64Encode(byte[] bytes) {
        return new BASE64Encoder().encode(bytes);
    }

    private static byte[] base64Decode(String base64Code) throws IOException {
        return new BASE64Decoder().decodeBuffer(base64Code);
    }
}
