#ifndef SECURE_HPP_
#define SECURE_HPP_
#include <string>
#include <boost/container/detail/singleton.hpp>

typedef unsigned char u_char;
typedef unsigned long u_long;
/************************
*
*  所有的加密与解密都只针对一条指令
*
*************************/
class Secure {
public:
    Secure();
    virtual ~Secure();
    void GetKey(char* pKey, std::string pImeiId); //获取手机KEY
    void GetKey(std::string gwmac);               //获取连接云服务器的KEY

    char* Base64Encode(const unsigned char* bindata, char* base65, int binlength);
    int Base64Decode(const char* base64, unsigned char* bindata);

    int ZigbeeDataEncrypt(char* pData);              // zigbee数据加密
    int ZigbeeDataEncrypt_Hex(char* pData, int len); // zigbee数据加密 HEX格式
    void ZigbeeDataDecrypt(char* pData, int pLen);   // zigbee数据解密

    int NetDataEncrypt(char* pMing, char* pMi, char* pKey); //网络数据加密
    int NetDataDecrypt(char* pMing, char* pMi, char* pKey); //网络数据解密
    int NetDataDecrypt(char* pMing, char* pMi, int pLen, char* pKey);

    char* Md5Sum(const char* filename);
    int Encrypt(u_char* plaintext, u_char* expectText, int c_len, u_char* key);
    int Decrypt(u_char* expectText, u_char* currentText, int c_len, u_char* key);
    void BteaEncrypt(u_char* buf, u_char len);
    void BteaDecrpyt(u_char* buf, u_char len);
    char* FnMD5CACL(u_char* buf, int len);

    enum { KEYLen = 16 };
    char KEY[KEYLen];
    typedef struct {
        int nr;               /*!<  number of rounds  */
        unsigned int* rk;     /*!<  AES round keys    */
        unsigned int buf[68]; /*!<  unaligned data    */
    } aes_context;
    void aes_setkey_enc(aes_context* ctx, const unsigned char* key, int keysize);
    void aes_setkey_dec(aes_context* ctx, const unsigned char* key, int keysize);
    unsigned char* aes_crypt_ecb(aes_context* ctx, int mode, const unsigned char* input, int slen, int* dlen);

private:
    char result[33];
};

typedef boost::container::dtl::singleton_default<Secure> secure;

#endif // SECURE_HPP_