├── .gitignore ├── .qmake.stash ├── Example ├── .gitignore ├── Example.pro └── main.cpp ├── LICENSE ├── NAuth.pro ├── NAuth ├── NAuth.pro ├── NAuth_inc.pri ├── NAuth_resource.rc ├── NAuth_src.pri ├── inc │ ├── liscenseobject.h │ ├── nauth.h │ ├── nauth_global.h │ └── nauthprotocol.h └── src │ ├── liscenseobject.cpp │ ├── nauth.cpp │ └── nauthprotocol.cpp ├── NComputerInfo ├── .gitignore ├── Example │ ├── Example.pro │ └── main.cpp ├── NComputerInfo.pro ├── NComputerInfo │ ├── 3rdparty │ │ ├── README.md │ │ ├── includepath │ │ │ └── getdiskinfo.h │ │ └── library │ │ │ └── getdiskinfo.dll │ ├── NComputerInfo.pro │ ├── NComputerInfo_inc.pri │ ├── NComputerInfo_resource.rc │ ├── NComputerInfo_src.pri │ ├── inc │ │ ├── ncomputerinfo.h │ │ └── ncomputerinfo_global.h │ └── src │ │ └── ncomputerinfo.cpp ├── README.md └── Scripts │ └── clear.bat ├── NConfig ├── .gitignore ├── AppCustomConfig │ ├── .gitignore │ ├── 3rdparty │ │ └── singleton │ │ │ ├── call_once.h │ │ │ ├── singleton.h │ │ │ └── singleton.pri │ ├── AppCustomConfig.pro │ ├── AppCustomConfig_inc.pri │ ├── inc │ │ └── appcustomconfig.h │ └── src │ │ ├── appcustomconfig.cpp │ │ └── main.cpp ├── NConfig.pro ├── NConfig │ ├── 3rdparty │ │ ├── wxsqlite3 │ │ │ ├── codec.c │ │ │ ├── codec.h │ │ │ ├── codecext.c │ │ │ ├── rijndael.c │ │ │ ├── rijndael.h │ │ │ ├── sha2.c │ │ │ ├── sha2.h │ │ │ ├── sqlite3.c │ │ │ ├── sqlite3.h │ │ │ ├── sqlite3secure.c │ │ │ └── wxsqlite3.pri │ │ └── zsqlitecipherdriver │ │ │ ├── zsqliteciphercachedresult.cpp │ │ │ ├── zsqliteciphercachedresult.h │ │ │ ├── zsqlitecipherdriver.cpp │ │ │ ├── zsqlitecipherdriver.h │ │ │ └── zsqlitecipherdriver.pri │ ├── NConfig.pro │ ├── NConfig_inc.pri │ ├── NConfig_resource.rc │ ├── NConfig_src.pri │ ├── inc │ │ ├── nconfig.h │ │ └── nconfig_global.h │ └── src │ │ └── nconfig.cpp ├── README.md ├── example │ ├── .gitignore │ ├── example.pro │ └── main.cpp └── scripts │ ├── clear.bat │ └── sqlite3shell.exe ├── NEncryptionKit ├── .gitignore ├── Example │ ├── .gitignore │ ├── Example.pro │ └── main.cpp ├── NEncryptionKit.pro ├── NEncryptionKit │ ├── 3rdparty │ │ └── botan │ │ │ ├── botan.cpp │ │ │ ├── botan.h │ │ │ ├── botan.pri │ │ │ ├── configure.py │ │ │ ├── doc │ │ │ └── license.txt │ │ │ └── readme.txt │ ├── NEncryptionKit.pro │ ├── NEncryptionKit_inc.pri │ ├── NEncryptionKit_resource.rc │ ├── NEncryptionKit_src.pri │ ├── inc │ │ ├── nencryptionkit.h │ │ └── nencryptionkit_global.h │ └── src │ │ └── nencryptionkit.cpp ├── README.md └── Scripts │ └── clear.bat ├── README.md ├── Scripts ├── arch.png ├── clear.bat ├── pro_arch.bmpr └── 鉴权流程.png └── _config.yml /.gitignore: -------------------------------------------------------------------------------- 1 | /NAuth.pro.user 2 | /NAuth/NAuth.pro.user 3 | -------------------------------------------------------------------------------- /.qmake.stash: -------------------------------------------------------------------------------- 1 | QMAKE_DEFAULT_INCDIRS = \ 2 | C:/Qt/Qt5.7.0/Tools/mingw530_32/lib/gcc/i686-w64-mingw32/5.3.0/include \ 3 | C:/Qt/Qt5.7.0/Tools/mingw530_32/lib/gcc/i686-w64-mingw32/5.3.0/include-fixed \ 4 | C:/Qt/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include \ 5 | C:/Qt/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++ \ 6 | C:/Qt/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/i686-w64-mingw32 \ 7 | C:/Qt/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/backward 8 | QMAKE_DEFAULT_LIBDIRS = \ 9 | C:/Qt/Qt5.7.0/Tools/mingw530_32/lib/gcc/i686-w64-mingw32/5.3.0 \ 10 | C:/Qt/Qt5.7.0/Tools/mingw530_32/lib/gcc \ 11 | C:/Qt/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/lib \ 12 | C:/Qt/Qt5.7.0/Tools/mingw530_32/lib 13 | -------------------------------------------------------------------------------- /Example/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Example/Example.pro: -------------------------------------------------------------------------------- 1 | QT += core 2 | QT -= gui 3 | 4 | CONFIG += c++11 5 | 6 | TARGET = NAuth_Example 7 | CONFIG += console 8 | CONFIG -= app_bundle 9 | 10 | TEMPLATE = app 11 | 12 | SOURCES += main.cpp 13 | 14 | win32{ 15 | CONFIG += debug_and_release 16 | CONFIG(release, debug|release) { 17 | target_path = ./build_/dist 18 | } else { 19 | target_path = ./build_/debug 20 | } 21 | DESTDIR = ../bin 22 | MOC_DIR = $$target_path/moc 23 | RCC_DIR = $$target_path/rcc 24 | OBJECTS_DIR = $$target_path/obj 25 | } 26 | 27 | include($$PWD/../NAuth/NAuth_inc.pri) 28 | 29 | CONFIG += c++11 30 | message(Qt version: $$[QT_VERSION]) 31 | message(Qt is installed in $$[QT_INSTALL_PREFIX]) 32 | message(the NAuth_Example will create in folder: $$target_path) 33 | -------------------------------------------------------------------------------- /Example/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "nauth.h" 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QCoreApplication a(argc, argv); 8 | 9 | qDebug()<<"*****************************************pc.key导出测试开始**********************************************"; 10 | NAuth testINstance001; 11 | bool ret = testINstance001.exportKeyFile(); 12 | qDebug() << "文件是否导出成功:"< 11 | #include "nauth_global.h" 12 | 13 | /** 14 | * @brief The LiscenseObject class 权限对象实体 15 | */ 16 | class NAUTHSHARED_EXPORT LiscenseObject : public QObject 17 | { 18 | Q_OBJECT 19 | public: 20 | explicit LiscenseObject(QObject *parent = 0); 21 | 22 | bool unPackLiscense(const QString ¶m_key); 23 | 24 | public: 25 | /** 26 | * @brief getLiscenseRaw 获取许可原始数据 27 | * @return 28 | */ 29 | QString getLiscenseRaw() const; 30 | 31 | /** 32 | * @brief setLiscenseRaw 设置许可原始数据 33 | * @param value 34 | */ 35 | void setLiscenseRaw(const QString &value); 36 | 37 | /** 38 | * @brief getLastError 获取错误描述信息 39 | * @return 40 | */ 41 | QString getLastError() const; 42 | 43 | /** 44 | * @brief setLastError 设置错误描述 45 | * @param value 46 | */ 47 | void setLastError(const QString &value); 48 | 49 | private: 50 | /** 51 | * @brief LiscenseRaw 许可原始数据 52 | */ 53 | QString liscenseRaw; 54 | 55 | /** 56 | * @brief lastError 最近的错误描述 57 | */ 58 | QString lastError; 59 | }; 60 | 61 | #endif // LISCENSEOBJECT_H 62 | -------------------------------------------------------------------------------- /NAuth/inc/nauth.h: -------------------------------------------------------------------------------- 1 | #ifndef NAUTH_H 2 | #define NAUTH_H 3 | 4 | /** 5 | * 作者: daodaoliang 6 | * 时间: 2016年8月18日 7 | * 邮箱: daodaoliang@yeah.net 8 | */ 9 | 10 | #include 11 | #include 12 | #include "nauth_global.h" 13 | #include "nauthprotocol.h" 14 | 15 | /** 16 | * @brief The NAuth class 认证鉴权组件 17 | */ 18 | class NAUTHSHARED_EXPORT NAuth : public QObject 19 | { 20 | Q_OBJECT 21 | public: 22 | explicit NAuth(QObject* parent=NULL); 23 | 24 | public: 25 | /** 26 | * @brief exportKeyFile 导出 key 文件到指定目录 27 | * @param paramPath 文件路径 28 | * @return 29 | */ 30 | bool exportKeyFile(const QString ¶mPath = QCoreApplication::applicationDirPath() + "/"); 31 | 32 | /** 33 | * @brief importKeyFile 导入 key 文件并进行解析 34 | * @param paramPath 文件路径 35 | * @return 36 | */ 37 | bool importKeyFile(const QString ¶mPath = QCoreApplication::applicationDirPath() + "/"); 38 | 39 | /** 40 | * @brief authWorkFlow 认证工作流程 41 | * @return 42 | */ 43 | bool authWorkFlow(); 44 | 45 | /** 46 | * @brief getLastError 获取最后一次错误信息 47 | * @return 48 | */ 49 | QString getLastError() const; 50 | 51 | /** 52 | * @brief setLastError 设置最后一次错误信息 53 | * @param value 54 | */ 55 | void setLastError(const QString &value); 56 | 57 | private: 58 | /** 59 | * @brief protocolInstance 协议操作实例 60 | */ 61 | NAuthProtocol protocolInstance; 62 | 63 | /** 64 | * @brief lastError 最后一次错误信息 65 | */ 66 | QString lastError; 67 | }; 68 | 69 | #endif // NAUTH_H 70 | -------------------------------------------------------------------------------- /NAuth/inc/nauth_global.h: -------------------------------------------------------------------------------- 1 | #ifndef NAUTH_GLOBAL_H 2 | #define NAUTH_GLOBAL_H 3 | 4 | #include 5 | 6 | #if defined(NAUTH_LIBRARY) 7 | # define NAUTHSHARED_EXPORT Q_DECL_EXPORT 8 | #else 9 | # define NAUTHSHARED_EXPORT Q_DECL_IMPORT 10 | #endif 11 | 12 | #endif // NAUTH_GLOBAL_H 13 | -------------------------------------------------------------------------------- /NAuth/inc/nauthprotocol.h: -------------------------------------------------------------------------------- 1 | #ifndef NAUTHPROTOCOL_H 2 | #define NAUTHPROTOCOL_H 3 | 4 | /** 5 | * 作者: daodaoliang 6 | * 时间: 2016年8月18日 7 | * 邮箱: daodaoliang@yeah.net 8 | */ 9 | 10 | #include 11 | #include "nauth_global.h" 12 | #include "liscenseobject.h" 13 | 14 | /** 15 | * @brief The NAuthProtocol class 鉴权协议封装和解析 16 | */ 17 | class NAUTHSHARED_EXPORT NAuthProtocol : public QObject 18 | { 19 | Q_OBJECT 20 | public: 21 | explicit NAuthProtocol(QObject *parent = 0); 22 | virtual ~NAuthProtocol(){} 23 | 24 | public: 25 | /** 26 | * @brief PackAuthProtocol 生成本机认证封装包 27 | * { 28 | * "product":{ 29 | * "version": 产品版本, 30 | * "name": 产品名字 31 | * }, 32 | * "computer":{ 33 | * "mac": MAC地址, 34 | * "disk_serial": 系统盘逻辑序列号, 35 | * "cpu_version": CPU版本, 36 | * "cpu_arch": CPU架构 37 | * }, 38 | * "system":{ 39 | * "sys_version": 系统版本, 40 | * "sys_user": 系统用户 41 | * } 42 | * } 43 | * @return 44 | */ 45 | bool PackAuthProtocol(QString ¶m_packet); 46 | 47 | /** 48 | * @brief UnPackAuthProtocol 解析协议认证包 49 | * { 50 | * "product":{ 51 | * "version": 产品版本, 52 | * "name": 产品名字 53 | * }, 54 | * "computer":{ 55 | * "mac": MAC地址, 56 | * "disk_serial": 系统盘逻辑序列号, 57 | * "cpu_version": CPU版本, 58 | * "cpu_arch": CPU架构 59 | * }, 60 | * "system":{ 61 | * "sys_version": 系统版本, 62 | * "sys_user": 系统用户 63 | * } 64 | * } 65 | * @param param_key 66 | * @return 是否解析成功 67 | */ 68 | bool UnPackAuthProtocol(const QString ¶m_key); 69 | 70 | /** 71 | * @brief UnPackLicenseProtocol 解析授权认证包 72 | * { 73 | * "date":{ 74 | * "apply_date": 申请日期, 75 | * "begin_date": 开始日期, 76 | * "end_date": 失效日期 77 | * }, 78 | * "authority": 权限串 79 | * } 80 | * @param param_key 81 | * @return 82 | */ 83 | bool UnPackLicenseProtocol(const QString ¶m_key); 84 | 85 | /** 86 | * @brief getLastError 获取错误信息 87 | * @return 88 | */ 89 | QString getLastError() const; 90 | 91 | /** 92 | * @brief setLastError 设置错误信息 93 | * @param value 94 | */ 95 | void setLastError(const QString &value); 96 | 97 | private: 98 | /** 99 | * @brief getBaseRawInfo 获取本地环境的原始信息 100 | * @return 101 | */ 102 | bool getBaseRawInfo(); 103 | 104 | private: 105 | /** 106 | * @brief applyTime 申请时间 107 | */ 108 | QString applyTime; 109 | 110 | /** 111 | * @brief beginTime 有效期的开始时间 112 | */ 113 | QString beginTime; 114 | 115 | /** 116 | * @brief endTime 有效期的结束时间 117 | */ 118 | QString endTime; 119 | 120 | /** 121 | * @brief proVersion 产品版本 122 | */ 123 | QString proVersion; 124 | 125 | /** 126 | * @brief proName 产品名字 127 | */ 128 | QString proName; 129 | 130 | /** 131 | * @brief pcMAC 硬件MAC地址 132 | */ 133 | QString pcMAC; 134 | 135 | /** 136 | * @brief pcSerial 硬件逻辑序列号 137 | */ 138 | QString pcSerial; 139 | 140 | /** 141 | * @brief pcCPUVersion 硬件CPU版本 142 | */ 143 | QString pcCPUVersion; 144 | 145 | /** 146 | * @brief pcCPUArch 硬件CPU架构 147 | */ 148 | QString pcCPUArch; 149 | 150 | /** 151 | * @brief pcSysVersion 系统版本 152 | */ 153 | QString sysVersion; 154 | 155 | /** 156 | * @brief pcSysUser xiton 157 | */ 158 | QString sysUser; 159 | 160 | /** 161 | * @brief lastError 最近的错误信息 162 | */ 163 | QString lastError; 164 | 165 | /** 166 | * @brief innerPassword 协议包密码 167 | */ 168 | QString innerPassword; 169 | 170 | /** 171 | * @brief liscenceInstance 许可对象 172 | */ 173 | LiscenseObject liscenceInstance; 174 | }; 175 | 176 | #endif // NAUTHPROTOCOL_H 177 | -------------------------------------------------------------------------------- /NAuth/src/liscenseobject.cpp: -------------------------------------------------------------------------------- 1 | #include "liscenseobject.h" 2 | 3 | LiscenseObject::LiscenseObject(QObject *parent) : QObject(parent) 4 | { 5 | 6 | } 7 | 8 | bool LiscenseObject::unPackLiscense(const QString ¶m_key) 9 | { 10 | Q_UNUSED(param_key); 11 | // TODO 解析 12 | return true; 13 | } 14 | 15 | QString LiscenseObject::getLiscenseRaw() const 16 | { 17 | return liscenseRaw; 18 | } 19 | 20 | void LiscenseObject::setLiscenseRaw(const QString &value) 21 | { 22 | liscenseRaw = value; 23 | } 24 | 25 | QString LiscenseObject::getLastError() const 26 | { 27 | return lastError; 28 | } 29 | 30 | void LiscenseObject::setLastError(const QString &value) 31 | { 32 | lastError = value; 33 | } 34 | -------------------------------------------------------------------------------- /NAuth/src/nauth.cpp: -------------------------------------------------------------------------------- 1 | #include "nauth.h" 2 | #include 3 | #include 4 | #include 5 | 6 | NAuth::NAuth(QObject *parent) 7 | :QObject(parent) 8 | { 9 | } 10 | 11 | bool NAuth::exportKeyFile(const QString ¶mPath) 12 | { 13 | QString defaultKeyName = "pc.key"; 14 | QString testPackProtocol; 15 | bool ret = protocolInstance.PackAuthProtocol(testPackProtocol); 16 | if(!ret){ 17 | setLastError(protocolInstance.getLastError()); 18 | return false; 19 | } 20 | QFile innerFile(paramPath + defaultKeyName); 21 | try { 22 | if(innerFile.open(QFile::WriteOnly)){ 23 | QDataStream out(&innerFile); 24 | out.setVersion(QDataStream::Qt_5_7); 25 | out << testPackProtocol.toLocal8Bit(); 26 | innerFile.flush(); 27 | } 28 | } catch (...) { 29 | setLastError("pc.key打开失败"); 30 | return false; 31 | } 32 | innerFile.close(); 33 | return true; 34 | } 35 | 36 | bool NAuth::importKeyFile(const QString ¶mPath) 37 | { 38 | QString defaultKeyName = "pc.key"; 39 | QByteArray pcKeyRaw; 40 | QFile innerFile(paramPath + defaultKeyName); 41 | try { 42 | if(innerFile.open(QFile::ReadOnly)){ 43 | QDataStream in(&innerFile); 44 | in.setVersion(QDataStream::Qt_5_7); 45 | in >> pcKeyRaw; 46 | } 47 | } catch (...) { 48 | setLastError("pc.key读取失败"); 49 | } 50 | innerFile.close(); 51 | bool ret = protocolInstance.UnPackAuthProtocol(pcKeyRaw); 52 | if(!ret){ 53 | setLastError(protocolInstance.getLastError()); 54 | return false; 55 | } 56 | return true; 57 | } 58 | 59 | bool NAuth::authWorkFlow() 60 | { 61 | // 查找本地授权文件 62 | QString localLicense = QCoreApplication::applicationDirPath() + "/licence.lock"; 63 | if(!QFile::exists(localLicense)){ 64 | setLastError("未找到版权文件,请申请正版授权!"); 65 | return false; 66 | } 67 | 68 | // 解析本地授权文件 69 | } 70 | 71 | QString NAuth::getLastError() const 72 | { 73 | return lastError; 74 | } 75 | 76 | void NAuth::setLastError(const QString &value) 77 | { 78 | lastError = value; 79 | } 80 | -------------------------------------------------------------------------------- /NAuth/src/nauthprotocol.cpp: -------------------------------------------------------------------------------- 1 | #include "nauthprotocol.h" 2 | #include "ncomputerinfo.h" 3 | #include "nconfig.h" 4 | #include "nencryptionkit.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | NAuthProtocol::NAuthProtocol(QObject *parent) : QObject(parent) 12 | { 13 | innerPassword = "DaoDaoLiang'PWD_Nami!"; 14 | } 15 | 16 | bool NAuthProtocol::PackAuthProtocol(QString ¶m_packet) 17 | { 18 | // 获取原始信息 19 | bool ret = getBaseRawInfo(); 20 | if(!ret) return ret; 21 | // 协议封装 22 | QJsonObject outerJson; 23 | QJsonObject productionJson; 24 | productionJson.insert("version", proVersion); 25 | productionJson.insert("name", proName); 26 | QJsonObject pcJson; 27 | pcJson.insert("mac", pcMAC); 28 | pcJson.insert("disk_serial", pcSerial); 29 | pcJson.insert("cpu_version", pcCPUVersion); 30 | pcJson.insert("cpu_arch", pcCPUArch); 31 | QJsonObject sysJson; 32 | sysJson.insert("sys_version", sysVersion); 33 | sysJson.insert("sys_user", sysUser); 34 | outerJson.insert("product", productionJson); 35 | outerJson.insert("computer", pcJson); 36 | outerJson.insert("system", sysJson); 37 | QJsonDocument temp_document; 38 | temp_document.setObject(outerJson); 39 | QString tempJson = QString(temp_document.toJson(QJsonDocument::Compact)); 40 | 41 | // 协议加密 42 | NEncryptionKit EncryptionInstance; 43 | EncryptionInstance.setPassword(innerPassword); 44 | string temp_str; 45 | ret = EncryptionInstance.getEncryByAES(tempJson, temp_str); 46 | if(!ret) { 47 | setLastError("加密失败"); 48 | return ret; 49 | } 50 | param_packet = QByteArray::fromStdString(temp_str).toHex(); 51 | param_packet += EncryptionInstance.getMD5Hash(param_packet); 52 | return true; 53 | } 54 | 55 | bool NAuthProtocol::UnPackAuthProtocol(const QString ¶m_key) 56 | { 57 | QString decryptQstring; 58 | NEncryptionKit EncryptionInstance; 59 | QString tempMD5 = param_key.right(32); 60 | QString tempRaw = param_key.left(param_key.length() - 32); 61 | if(EncryptionInstance.getMD5Hash(tempRaw) != tempMD5){ 62 | setLastError("认证文件被非法修改"); 63 | return false; 64 | } 65 | // 协议解密 66 | try{ 67 | EncryptionInstance.setPassword(innerPassword); 68 | bool ret = EncryptionInstance.decryptByAES(QByteArray::fromHex(tempRaw.toLocal8Bit()).toStdString(), decryptQstring); 69 | if(!ret){ 70 | setLastError("解密失败"); 71 | return ret; 72 | } 73 | } catch (...){ 74 | setLastError("解密失败-发生未知错误"); 75 | return false; 76 | } 77 | 78 | // 协议解析 79 | QJsonDocument innerDocument; 80 | QJsonObject productionJson; 81 | QJsonParseError tempJsonError; 82 | innerDocument = QJsonDocument::fromJson(decryptQstring.toLocal8Bit(), &tempJsonError); 83 | if(tempJsonError.error != QJsonParseError::NoError){ 84 | setLastError(tempJsonError.errorString()); 85 | return false; 86 | } else { 87 | if(innerDocument.isObject()){ 88 | productionJson = innerDocument.object(); 89 | } else { 90 | setLastError("协议解析失败"); 91 | return false; 92 | } 93 | } 94 | 95 | QJsonValue product = productionJson.take("product"); 96 | QJsonValue pc = productionJson.take("computer"); 97 | QJsonValue sys = productionJson.take("system"); 98 | 99 | if(!product.isObject()){ 100 | setLastError("协议解析失败-product is not object"); 101 | return false; 102 | } 103 | if(!pc.isObject()){ 104 | setLastError("协议解析失败-pc is not object"); 105 | return false; 106 | } 107 | if(!sys.isObject()){ 108 | setLastError("协议解析失败-sys is not object"); 109 | return false; 110 | } 111 | 112 | // 获取原始基础信息 113 | bool ret = getBaseRawInfo(); 114 | if(!ret) return ret; 115 | 116 | // product 匹配 117 | QString temStr; 118 | QJsonObject temObj = product.toObject(); 119 | 120 | if(temObj.contains("version")){ 121 | temStr = temObj.take("version").toString(); 122 | } else { 123 | setLastError("auth中product没有version"); 124 | return false; 125 | } 126 | if(temStr != proVersion){ 127 | setLastError("本地软件版本和授权版本不一致"); 128 | return false; 129 | } 130 | 131 | if(temObj.contains("name")){ 132 | temStr = temObj.take("name").toString(); 133 | } else { 134 | setLastError("auth中product没有name"); 135 | return false; 136 | } 137 | if(temStr != proName){ 138 | setLastError("本地软件名字和授权不一致"); 139 | return false; 140 | } 141 | 142 | // computer 匹配 143 | temObj = pc.toObject(); 144 | if(temObj.contains("mac")){ 145 | temStr = temObj.take("mac").toString(); 146 | } else { 147 | setLastError("auth中computer没有mac"); 148 | return false; 149 | } 150 | if(temStr != pcMAC){ 151 | setLastError("本地网卡设备和授权不一致"); 152 | return false; 153 | } 154 | if(temObj.contains("disk_serial")){ 155 | temStr = temObj.take("disk_serial").toString(); 156 | } else { 157 | setLastError("auth中computer没有disk_serial"); 158 | return false; 159 | } 160 | if(temStr != pcSerial){ 161 | setLastError("本地硬盘序列号和授权不一致"); 162 | return false; 163 | } 164 | if(temObj.contains("cpu_version")){ 165 | temStr = temObj.take("cpu_version").toString(); 166 | } else { 167 | setLastError("auth中computer没有cpu_version"); 168 | return false; 169 | } 170 | if(temStr != pcCPUVersion){ 171 | setLastError("本地CPU和授权不一致"); 172 | return false; 173 | } 174 | if(temObj.contains("cpu_arch")){ 175 | temStr = temObj.take("cpu_arch").toString(); 176 | } else { 177 | setLastError("auth中computer没有cpu_arch"); 178 | return false; 179 | } 180 | if(temStr != pcCPUArch){ 181 | setLastError("本地CPU和授权不一致"); 182 | return false; 183 | } 184 | 185 | // sys 匹配 186 | temObj = sys.toObject(); 187 | if(temObj.contains("sys_version")){ 188 | temStr = temObj.take("sys_version").toString(); 189 | } else { 190 | setLastError("auth中computer没有sys_version"); 191 | return false; 192 | } 193 | if(temStr != sysVersion){ 194 | setLastError("本地系统和授权不一致"); 195 | return false; 196 | } 197 | if(temObj.contains("sys_user")){ 198 | temStr = temObj.take("sys_user").toString(); 199 | } else { 200 | setLastError("auth中computer没有sys_user"); 201 | return false; 202 | } 203 | if(temStr != sysUser){ 204 | setLastError("本地系统用户和授权不一致"); 205 | return false; 206 | } 207 | return true; 208 | } 209 | 210 | bool NAuthProtocol::UnPackLicenseProtocol(const QString ¶m_key) 211 | { 212 | QString decryptQstring; 213 | NEncryptionKit EncryptionInstance; 214 | QString tempMD5 = param_key.right(32); 215 | QString tempRaw = param_key.left(param_key.length() - 32); 216 | if(EncryptionInstance.getMD5Hash(tempRaw) != tempMD5){ 217 | setLastError("授权文件被非法修改"); 218 | return false; 219 | } 220 | 221 | // 协议解密 222 | try{ 223 | EncryptionInstance.setPassword(innerPassword); 224 | bool ret = EncryptionInstance.decryptByAES(QByteArray::fromHex(tempRaw.toLocal8Bit()).toStdString(), decryptQstring); 225 | if(!ret) { 226 | setLastError("解密失败"); 227 | return ret; 228 | } 229 | } catch (...) { 230 | setLastError("解密失败-发生未知错误"); 231 | return false; 232 | } 233 | 234 | // 协议解析 235 | QJsonDocument innerDocument; 236 | QJsonObject productionJson; 237 | QJsonParseError tempJsonError; 238 | innerDocument = QJsonDocument::fromJson(decryptQstring.toLocal8Bit(), &tempJsonError); 239 | if(tempJsonError.error != QJsonParseError::NoError){ 240 | setLastError(tempJsonError.errorString()); 241 | return false; 242 | } else { 243 | if(innerDocument.isObject()){ 244 | productionJson = innerDocument.object(); 245 | } else { 246 | setLastError("协议解析失败"); 247 | return false; 248 | } 249 | } 250 | 251 | QJsonValue date; 252 | if(productionJson.contains("date")){ 253 | productionJson.take("date"); 254 | } else { 255 | setLastError("协议解析失败-没有date"); 256 | return false; 257 | } 258 | 259 | QJsonValue authority; 260 | if(productionJson.contains("authority")){ 261 | productionJson.take("authority"); 262 | } else { 263 | setLastError("协议解析失败-没有authority"); 264 | } 265 | 266 | if(!date.isObject()){ 267 | setLastError("协议解析失败-date is not object"); 268 | return false; 269 | } 270 | 271 | if(!authority.isObject()){ 272 | setLastError("协议解析失败-authority is not object"); 273 | return false; 274 | } 275 | 276 | // date 解析 277 | QJsonObject temObj = date.toObject(); 278 | 279 | if(temObj.contains("apply_date")){ 280 | applyTime = temObj.take("apply_date").toString(); 281 | } else { 282 | setLastError("liscense中没有apply_date"); 283 | return false; 284 | } 285 | 286 | if(temObj.contains("begin_date")){ 287 | beginTime = temObj.take("begin_date").toString(); 288 | } else { 289 | setLastError("liscense中没有begin_date"); 290 | return false; 291 | } 292 | 293 | if(temObj.contains("end_date")){ 294 | beginTime = temObj.take("end_date").toString(); 295 | } else { 296 | setLastError("liscense中没有end_date"); 297 | return false; 298 | } 299 | 300 | // authority 解析 301 | if(!liscenceInstance.unPackLiscense(authority.toString())){ 302 | setLastError(liscenceInstance.getLastError()); 303 | return false; 304 | } 305 | 306 | return true; 307 | } 308 | 309 | QString NAuthProtocol::getLastError() const 310 | { 311 | return lastError; 312 | } 313 | 314 | void NAuthProtocol::setLastError(const QString &value) 315 | { 316 | lastError = value; 317 | } 318 | 319 | bool NAuthProtocol::getBaseRawInfo() 320 | { 321 | // 数据归零 322 | applyTime = ""; 323 | beginTime = ""; 324 | endTime = ""; 325 | proVersion = ""; 326 | proName = ""; 327 | pcMAC = ""; 328 | pcSerial = ""; 329 | pcCPUVersion = ""; 330 | pcCPUArch = ""; 331 | sysVersion = ""; 332 | sysUser = ""; 333 | // 读取产品信息 334 | NConfig NConfigInstance; 335 | bool ret = NConfigInstance.installAndInit(QCoreApplication::applicationDirPath() + "/app.conf", "daodaoliang", false); 336 | if(!ret) { 337 | setLastError("初始化配置文件失败"); 338 | return ret; 339 | } 340 | ret = NConfigInstance.getConfigItem("app","version",proVersion); 341 | if(!ret) { 342 | setLastError("获取 产品:版本 信息失败"); 343 | return ret; 344 | } 345 | ret = NConfigInstance.getConfigItem("app","name",proName); 346 | if(!ret) { 347 | setLastError("获取 产品:名字 信息失败"); 348 | return ret; 349 | } 350 | NConfigInstance.releaseConf(); 351 | 352 | // 读取电脑硬件信息 353 | NComputerInfo NComputerInstance; 354 | ret = NComputerInstance.getMAC(pcMAC, false); 355 | if(!ret) { 356 | setLastError("获取 PC:MAC 信息失败"); 357 | return ret; 358 | } 359 | ret = NComputerInstance.getVolumeSerialNumber(pcSerial); 360 | if(!ret) { 361 | setLastError("获取 PC:Serial 信息失败"); 362 | return ret; 363 | } 364 | ret = NComputerInstance.getCPUVersion(pcCPUVersion); 365 | if(!ret) { 366 | setLastError("获取 PC:CPUV 信息失败"); 367 | return ret; 368 | } 369 | ret = NComputerInstance.getCPUArch(pcCPUArch); 370 | if(!ret) { 371 | setLastError("获取 PC:CPUA 信息失败"); 372 | return ret; 373 | } 374 | 375 | // 读取系统信息 376 | ret = NComputerInstance.getSysVersion(sysVersion); 377 | if(!ret) { 378 | setLastError("获取 SYS:Version 信息失败"); 379 | return ret; 380 | } 381 | ret = NComputerInstance.getSysUserName(sysUser); 382 | if(!ret) { 383 | setLastError("获取 SYS:User 信息失败"); 384 | return ret; 385 | } 386 | return true; 387 | } 388 | -------------------------------------------------------------------------------- /NComputerInfo/.gitignore: -------------------------------------------------------------------------------- 1 | /NComputerInfo.pro.user 2 | /.qmake.stash 3 | -------------------------------------------------------------------------------- /NComputerInfo/Example/Example.pro: -------------------------------------------------------------------------------- 1 | QT += core network 2 | QT -= gui 3 | 4 | CONFIG += c++11 5 | 6 | TARGET = NComputerInfo_Example 7 | CONFIG += console 8 | CONFIG -= app_bundle 9 | 10 | TEMPLATE = app 11 | 12 | SOURCES += main.cpp 13 | 14 | win32{ 15 | CONFIG += debug_and_release 16 | CONFIG(release, debug|release) { 17 | target_path = ./build_/dist 18 | } else { 19 | target_path = ./build_/debug 20 | } 21 | DESTDIR = ../../bin 22 | MOC_DIR = $$target_path/moc 23 | RCC_DIR = $$target_path/rcc 24 | OBJECTS_DIR = $$target_path/obj 25 | } 26 | 27 | CONFIG += c++11 28 | 29 | include(../NComputerInfo/NComputerInfo_inc.pri) 30 | -------------------------------------------------------------------------------- /NComputerInfo/Example/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ncomputerinfo.h" 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QCoreApplication a(argc, argv); 7 | qDebug()<<"**************************************获取电脑信息测试开始*************************************"; 8 | NComputerInfo test_case_instance; 9 | 10 | qDebug()<<"---------------------获取MAC用例开始------------------------"; 11 | QString test_case_001_mac; 12 | bool ret = test_case_instance.getMAC(test_case_001_mac, false); 13 | qDebug()<<"是否获取成功:"< 2 | 3 | struct diskInfo{ 4 | char controllerType[1024]; 5 | char type[1024]; 6 | char modelNumber[1024]; 7 | char serialNumber[1024]; 8 | char revisionNumber[1024]; 9 | char driveType[1024]; 10 | DWORD bufferSize; 11 | DWORD cylinders; 12 | DWORD heads; 13 | DWORD sectors; 14 | //_int64 computerID; 15 | }; 16 | -------------------------------------------------------------------------------- /NComputerInfo/NComputerInfo/3rdparty/library/getdiskinfo.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daodaoliang/NAuth/2ae4a54e2e9e81ced511326123b27c2e40500a65/NComputerInfo/NComputerInfo/3rdparty/library/getdiskinfo.dll -------------------------------------------------------------------------------- /NComputerInfo/NComputerInfo/NComputerInfo.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2016-08-12T14:03:05 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += widgets network 8 | 9 | TARGET = NComputerInfo 10 | TEMPLATE = lib 11 | 12 | DEFINES += NCOMPUTERINFO_LIBRARY 13 | 14 | # 引入源码 15 | include(./NComputerInfo_src.pri) 16 | 17 | # 引入版本信息 18 | RC_FILE += ./NComputerInfo_resource.rc 19 | 20 | win32{ 21 | CONFIG += debug_and_release 22 | CONFIG(release, debug|release) { 23 | target_path = ./build_/dist 24 | } else { 25 | target_path = ./build_/debug 26 | } 27 | DESTDIR = ../../bin 28 | MOC_DIR = $$target_path/moc 29 | RCC_DIR = $$target_path/rcc 30 | OBJECTS_DIR = $$target_path/obj 31 | } 32 | CONFIG += c++11 33 | message(Qt version: $$[QT_VERSION]) 34 | message(Qt is installed in $$[QT_INSTALL_PREFIX]) 35 | message(the NComputerInfo will create in folder: $$target_path) 36 | -------------------------------------------------------------------------------- /NComputerInfo/NComputerInfo/NComputerInfo_inc.pri: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2016-08-12T14:03:05 4 | # 5 | #------------------------------------------------- 6 | 7 | INCLUDEPATH += $$PWD/inc \ 8 | INCLUDEPATH += $$PWD/3rdparty/includepath \ 9 | 10 | LIBS += -L$$PWD/../../bin/ -lNComputerInfo 11 | -------------------------------------------------------------------------------- /NComputerInfo/NComputerInfo/NComputerInfo_resource.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daodaoliang/NAuth/2ae4a54e2e9e81ced511326123b27c2e40500a65/NComputerInfo/NComputerInfo/NComputerInfo_resource.rc -------------------------------------------------------------------------------- /NComputerInfo/NComputerInfo/NComputerInfo_src.pri: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2016-08-12T14:03:05 4 | # 5 | #------------------------------------------------- 6 | 7 | SOURCES += $$PWD/src/ncomputerinfo.cpp \ 8 | 9 | HEADERS += $$PWD/inc/ncomputerinfo.h \ 10 | $$PWD/inc/ncomputerinfo_global.h \ 11 | 12 | INCLUDEPATH += $$PWD/inc \ 13 | INCLUDEPATH += $$PWD/3rdparty/includepath \ 14 | -------------------------------------------------------------------------------- /NComputerInfo/NComputerInfo/inc/ncomputerinfo.h: -------------------------------------------------------------------------------- 1 | #ifndef NCOMPUTERINFO_H 2 | #define NCOMPUTERINFO_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "getdiskinfo.h" 16 | #include "ncomputerinfo_global.h" 17 | 18 | /** 19 | * @brief The NComputerInfo class 获取程序运行期间的电脑硬件信息 20 | */ 21 | class NCOMPUTERINFOSHARED_EXPORT NComputerInfo: public QObject 22 | { 23 | Q_OBJECT 24 | public: 25 | explicit NComputerInfo(QObject *parent = NULL); 26 | enum MACHINE_CODE_ERROR{ 27 | NOTHING_ERROR, 28 | MAC_ERROR, 29 | SERIAL_ERROR, 30 | CPUV_ERROR, 31 | CPUA_ERROR, 32 | SYSV_ERROR, 33 | SYSU_ERROR, 34 | PARSE_ERROR 35 | }; 36 | 37 | /*******************************电脑硬件信息****************************/ 38 | public: 39 | /** 40 | * @brief getMAC 获取MAC地址 41 | * @param param_mac mac地址 42 | * @param param_have_param_mac 是否有分号 43 | * @return 是否获取成功 44 | */ 45 | bool getMAC(QString ¶m_mac, bool param_have_param_mac); 46 | 47 | /** 48 | * @brief getVolumeSerialNumber 获取C盘逻辑序列号 49 | * @param param_serial 序列号 50 | * @return 是否获取成功 51 | */ 52 | bool getVolumeSerialNumber(QString ¶m_serial); 53 | 54 | /** 55 | * @brief getHardDiskSerialNumber 获取硬盘的物理序列号 56 | * @param param_serial 57 | * @return 58 | */ 59 | bool getHardDiskSerialNumber(QString ¶m_serial); 60 | 61 | /** 62 | * @brief getCPUCount 获取CPU个数 63 | * @param param_count CPU个数 64 | * @return 是否获取成功 65 | */ 66 | bool getCPUCount(QString ¶m_count); 67 | 68 | /** 69 | * @brief getCPUVersion 获取CPU版本信息 70 | * @param param_version 版本 71 | * @return 是否获取成功 72 | */ 73 | bool getCPUVersion(QString ¶m_version); 74 | 75 | /** 76 | * @brief getCPUArch 获取CPU架构 77 | * @param param_cpu_arch 78 | * @return 79 | */ 80 | bool getCPUArch(QString ¶m_cpu_arch); 81 | 82 | /** 83 | * @brief getDiskSize 获取系统硬盘容量 84 | * @param param_total 总容量 85 | * @param param_available 可用容量 86 | * @return 是否获取成功 87 | */ 88 | bool getDiskSize(int ¶m_total,int ¶m_available); 89 | 90 | /*******************************电脑系统信息****************************/ 91 | public: 92 | /** 93 | * @brief getCPUUsageRate 获取资源使用率 94 | * @param param_rate 使用率 95 | * @return 96 | */ 97 | bool getUsageRate(QString ¶m_rate); 98 | 99 | /** 100 | * @brief getSysVersion 获取系统版本信息 101 | * @param param_version 版本 102 | * @return 是否获取成功 103 | */ 104 | bool getSysVersion(QString ¶m_version); 105 | 106 | /** 107 | * @brief getSysUserName 获取当前系统登录名字 108 | * @param param_version 109 | * @return 110 | */ 111 | bool getSysUserName(QString ¶m_version); 112 | 113 | /** 114 | * @brief getRuningTime 获取程序当前运行时间 115 | * @return 116 | */ 117 | quint64 getRuningTime(); 118 | 119 | /*******************************电脑唯一机器码****************************/ 120 | public: 121 | 122 | /** 123 | * @brief getMachineCode 获取本机机器码 124 | * @param param_machine_code 机器码 125 | * @return 是否获取成功 126 | */ 127 | bool getMachineCode(QString ¶m_machine_code); 128 | 129 | /** 130 | * @brief verifiMachineCode 验证机器码是否和本机匹配 131 | * @param param_machine_code 机器码 132 | * @return 是否验证成功 133 | */ 134 | bool verifiMachineCode(const QString ¶m_machine_code, MACHINE_CODE_ERROR ¶m_error); 135 | 136 | /*******************************电脑网络信息****************************/ 137 | public: 138 | 139 | /** 140 | * @brief getWlanIP 获取外网地址 141 | * @return 外网IP 142 | */ 143 | QString getWlanIP(); 144 | 145 | /** 146 | * @brief getLocalIP 获取本机IP地址 147 | * @return 本地IP 148 | */ 149 | QString getLocalIP(); 150 | 151 | /** 152 | * @brief isConnectedWlan 是否联通外网 153 | * @return 是否联通外网 154 | */ 155 | bool isConnectedWlan(); 156 | 157 | private: 158 | /** 159 | * @brief getHtml 获取指定网页的源码 160 | * @param param_html 指定的地址 161 | * @return 网页源码 162 | */ 163 | QString getHtml(const QString ¶m_html); 164 | 165 | bool ipLIve(const QString ¶m_ip, int port); 166 | public: 167 | quint64 mBegainTime; 168 | }; 169 | 170 | #endif // NCOMPUTERINFO_H 171 | -------------------------------------------------------------------------------- /NComputerInfo/NComputerInfo/inc/ncomputerinfo_global.h: -------------------------------------------------------------------------------- 1 | #ifndef NCOMPUTERINFO_GLOBAL_H 2 | #define NCOMPUTERINFO_GLOBAL_H 3 | 4 | #include 5 | 6 | #if defined(NCOMPUTERINFO_LIBRARY) 7 | # define NCOMPUTERINFOSHARED_EXPORT Q_DECL_EXPORT 8 | #else 9 | # define NCOMPUTERINFOSHARED_EXPORT Q_DECL_IMPORT 10 | #endif 11 | 12 | #endif // NCOMPUTERINFO_GLOBAL_H 13 | -------------------------------------------------------------------------------- /NComputerInfo/NComputerInfo/src/ncomputerinfo.cpp: -------------------------------------------------------------------------------- 1 | #include "ncomputerinfo.h" 2 | #include 3 | #define MB (1024 * 1024) 4 | #define KB (1024) 5 | 6 | NComputerInfo::NComputerInfo(QObject *parent) 7 | :QObject(parent) 8 | { 9 | mBegainTime = QDateTime::currentMSecsSinceEpoch(); 10 | } 11 | 12 | bool NComputerInfo::getMAC(QString ¶m_mac, bool param_have_param_mac) 13 | { 14 | foreach (QNetworkInterface temp_mac_info, QNetworkInterface::allInterfaces()) { 15 | if(!temp_mac_info.hardwareAddress().isEmpty()){ 16 | param_mac = temp_mac_info.hardwareAddress(); 17 | if(!param_have_param_mac){ 18 | param_mac = param_mac.remove(':'); 19 | } 20 | return true; 21 | } 22 | } 23 | param_mac = "00:00:00:00:00:00"; 24 | if(!param_have_param_mac) param_mac = "000000000000"; 25 | return false; 26 | } 27 | 28 | bool NComputerInfo::getVolumeSerialNumber(QString ¶m_serial) 29 | { 30 | QString lpRootPathName = "C:\\"; 31 | LPTSTR lpVolumeNameBuffer = new TCHAR[12]; 32 | DWORD nVolumeNameSize=12; 33 | DWORD VolumeSerialNumber; 34 | DWORD MaximumComponentLength; 35 | LPTSTR lpFileSystemNameBuffer = new TCHAR[10]; 36 | DWORD nFileSystemNameSize=10; 37 | DWORD FileSystemFlags; 38 | GetVolumeInformation((LPTSTR)lpRootPathName.utf16(), 39 | lpVolumeNameBuffer, nVolumeNameSize, 40 | &VolumeSerialNumber, &MaximumComponentLength, 41 | &FileSystemFlags, 42 | lpFileSystemNameBuffer, nFileSystemNameSize); 43 | param_serial = QString::number(VolumeSerialNumber,16).toUpper(); 44 | return true; 45 | } 46 | 47 | bool NComputerInfo::getHardDiskSerialNumber(QString ¶m_serial) 48 | { 49 | QLibrary tempLib("./getdiskinfo.dll"); 50 | typedef diskInfo (*getDiskInfo)(); 51 | getDiskInfo myFunction = (getDiskInfo) tempLib.resolve("getDiskInfo"); 52 | if(myFunction){ 53 | param_serial = QString(myFunction().serialNumber).trimmed(); 54 | return true; 55 | } 56 | param_serial = ""; 57 | return false; 58 | } 59 | 60 | bool NComputerInfo::getCPUCount(QString ¶m_count) 61 | { 62 | //备用(2014年11月3日--daodaoliang) 63 | //systemInfo.dwActiveProcessorMask ---处理器掩码 64 | //systemInfo.dwNumberOfProcessors ---处理器个数 65 | //systemInfo.dwPageSize ---处理器分页大小 66 | //systemInfo.dwProcessorType ---处理器类型 67 | //systemInfo.lpMaximumApplicationAddress ---最大寻址单元 68 | //systemInfo.lpMinimumApplicationAddress ---最小寻址单元 69 | //systemInfo.wProcessorLevel ---处理器等级 70 | //systemInfo.wProcessorRevision ---处理器版本 71 | SYSTEM_INFO systemInfo; 72 | GetSystemInfo(&systemInfo); 73 | param_count = QString::number(systemInfo.dwNumberOfProcessors); 74 | qDebug()<"); 333 | if(list.length()>3){ 334 | QString tar = list[3]; 335 | QStringList ip = tar.split("="); 336 | return ip[1]; 337 | } else { 338 | return ""; 339 | } 340 | } 341 | 342 | QString NComputerInfo::getLocalIP() 343 | { 344 | QString localHostName = QHostInfo::localHostName(); 345 | QHostInfo info = QHostInfo::fromName(localHostName); 346 | foreach(QHostAddress address,info.addresses()) 347 | { 348 | if(address.protocol() == QAbstractSocket::IPv4Protocol) 349 | return address.toString(); 350 | } 351 | } 352 | 353 | bool NComputerInfo::isConnectedWlan() 354 | { 355 | return ipLIve("111.13.101.208",80); 356 | } 357 | 358 | QString NComputerInfo::getHtml(const QString ¶m_html) 359 | { 360 | try{ 361 | QNetworkAccessManager *manager = new QNetworkAccessManager(); 362 | QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(param_html))); 363 | QByteArray responseData; 364 | QEventLoop eventLoop; 365 | QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit())); 366 | eventLoop.exec(); 367 | responseData = reply->readAll(); 368 | return QString(responseData); 369 | } catch(...){ 370 | return ""; 371 | } 372 | } 373 | 374 | bool NComputerInfo::ipLIve(const QString ¶m_ip, int port) 375 | { 376 | QTcpSocket tcpClient; 377 | tcpClient.abort(); 378 | tcpClient.connectToHost(param_ip, port); 379 | //100毫秒没有连接上则判断不在线 380 | return tcpClient.waitForConnected(100); 381 | } 382 | -------------------------------------------------------------------------------- /NComputerInfo/README.md: -------------------------------------------------------------------------------- 1 | # 电脑运行信息组件使用说明 2 | 3 | [TOC] 4 | 5 | ## 0.如何添加项目 6 | 7 | * 首先拷贝源码到你的工程文件目录; 8 | * 在你的项目 pro文件中添加: 9 | 10 | ```c 11 | # import dll 12 | win32: LIBS += -L$$PWD/../bin/ -lNComputerInfo 13 | DEPENDPATH += $$PWD/../bin 14 | 15 | # import dll file 16 | include($$PWD/../NComputerInfo/NComputerInfo_inc.pri) 17 | ``` 18 | 19 | ** 具体的路径请按照你的项目情况进行修改** 20 | 21 | ## 1. 测试用例 22 | 23 | * 获取硬件信息 24 | 25 | ``` 26 | NComputerInfo test_case_instance; 27 | 28 | qDebug()<<"---------------------获取MAC用例开始------------------------"; 29 | QString test_case_001_mac; 30 | bool ret = test_case_instance.getMAC(test_case_001_mac, false); 31 | qDebug()<<"是否获取成功:"< 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | namespace CallOnce { 12 | enum ECallOnce { 13 | CO_Request, 14 | CO_InProgress, 15 | CO_Finished 16 | }; 17 | 18 | Q_GLOBAL_STATIC(QThreadStorage, once_flag) } 19 | template 20 | inline static void qCallOnce(Function func, QBasicAtomicInt& flag) 21 | { 22 | using namespace CallOnce; 23 | #if QT_VERSION < 0x050000 24 | int protectFlag = flag.fetchAndStoreAcquire(flag); 25 | #elif QT_VERSION >= 0x050000 26 | int protectFlag = flag.fetchAndStoreAcquire(flag.load()); 27 | #endif 28 | if (protectFlag == CO_Finished) return; 29 | if (protectFlag == CO_Request && flag.testAndSetRelaxed(protectFlag, CO_InProgress)) 30 | { 31 | func(); 32 | flag.fetchAndStoreRelease(CO_Finished); 33 | } else { 34 | do { 35 | QThread::yieldCurrentThread(); 36 | } while (!flag.testAndSetAcquire(CO_Finished, CO_Finished)); 37 | } 38 | } 39 | 40 | template 41 | 42 | inline static void qCallOncePerThread(Function func) 43 | { 44 | using namespace CallOnce; 45 | if (!once_flag()->hasLocalData()) 46 | { 47 | once_flag()->setLocalData(new QAtomicInt(CO_Request)); 48 | qCallOnce(func, *once_flag()->localData()); 49 | } 50 | } 51 | 52 | #endif // CALL_ONCE_H 53 | -------------------------------------------------------------------------------- /NConfig/AppCustomConfig/3rdparty/singleton/singleton.h: -------------------------------------------------------------------------------- 1 | #ifndef SINGLETON_H 2 | #define SINGLETON_H 3 | 4 | #include 5 | #include 6 | #include "call_once.h" 7 | 8 | template 9 | class Singleton 10 | { 11 | public: 12 | static T& instance() { 13 | qCallOnce(init, flag); 14 | return *tptr; 15 | } 16 | static void init() { 17 | tptr.reset(new T); 18 | } 19 | private: 20 | Singleton() {} 21 | ~Singleton() {} 22 | Q_DISABLE_COPY(Singleton) 23 | static QScopedPointer tptr; 24 | static QBasicAtomicInt flag; 25 | }; 26 | 27 | /* BUG (run-time error) : If an instance is get during an initialization of global or static data that 28 | a Singleton::instance() function can be call before a constructor of a Singleton::tptr static 29 | variable. As result, the tptr variable will be zero, but a Singleton::flag will have a ECallOnce::CO_Finished 30 | value, so a next call of QScopedPointer::operator * generates an run-time error. */ 31 | 32 | template QScopedPointer Singleton::tptr(0); 33 | template QBasicAtomicInt Singleton::flag = Q_BASIC_ATOMIC_INITIALIZER(CallOnce::CO_Request); 34 | 35 | #endif // SINGLETON_H 36 | -------------------------------------------------------------------------------- /NConfig/AppCustomConfig/3rdparty/singleton/singleton.pri: -------------------------------------------------------------------------------- 1 | # 引入查找路径 2 | INCLUDEPATH += $$PWD 3 | DEPENDPATH += $$PWD 4 | 5 | # 引入源码 6 | HEADERS += \ 7 | $$PWD/call_once.h \ 8 | $$PWD/singleton.h \ 9 | 10 | -------------------------------------------------------------------------------- /NConfig/AppCustomConfig/AppCustomConfig.pro: -------------------------------------------------------------------------------- 1 | QT += core sql 2 | QT -= gui 3 | 4 | CONFIG += c++11 5 | 6 | TARGET = AppCustomConfig 7 | CONFIG += console 8 | CONFIG -= app_bundle 9 | 10 | TEMPLATE = app 11 | 12 | SOURCES += \ 13 | $$PWD/src/main.cpp 14 | 15 | win32{ 16 | DESTDIR = $$PWD/../../bin/ 17 | MOC_DIR = $$PWD/build_/moc 18 | RCC_DIR = $$PWD/build_/rcc 19 | OBJECTS_DIR = $$PWD/build_/obj 20 | } 21 | 22 | # import dll file 23 | include($$PWD/AppCustomConfig_inc.pri) 24 | -------------------------------------------------------------------------------- /NConfig/AppCustomConfig/AppCustomConfig_inc.pri: -------------------------------------------------------------------------------- 1 | SOURCES += \ 2 | $$PWD/src/appcustomconfig.cpp 3 | 4 | HEADERS += \ 5 | $$PWD/inc/appcustomconfig.h 6 | 7 | INCLUDEPATH += $$PWD/inc/ 8 | 9 | # 引入配置库 10 | include($$PWD/../NConfig/NConfig_inc.pri) 11 | win32: LIBS += -L$$PWD/../bin/ -lNConfig 12 | 13 | # 引入第三方 14 | include($$PWD/3rdparty/singleton/singleton.pri) 15 | -------------------------------------------------------------------------------- /NConfig/AppCustomConfig/inc/appcustomconfig.h: -------------------------------------------------------------------------------- 1 | #ifndef APPCUSTOMCONFIG_H 2 | #define APPCUSTOMCONFIG_H 3 | #include "nconfig.h" 4 | #include 5 | #include "singleton.h" 6 | 7 | #define APPConfigInstance Singleton::instance() 8 | 9 | /** 10 | * @brief The AppCustomConfig class 应用程序配置文件读取并对外提供变量 11 | */ 12 | class AppCustomConfig: public QObject 13 | { 14 | Q_OBJECT 15 | public: 16 | /** 17 | * @brief AppCustomConfig 构造函数 18 | */ 19 | AppCustomConfig(QObject *parent=NULL); 20 | virtual ~AppCustomConfig(); 21 | 22 | 23 | /** 24 | * @brief initAndReadConf 初始化并读取配置文件 25 | * @param confFilePath 26 | * @param confFilePwd 27 | * @return 28 | */ 29 | bool initAndReadConf(QString confFilePath, QString confFilePwd); 30 | 31 | public: 32 | /** 33 | * @brief configItem_localMysql_host 本地数据库的host 34 | */ 35 | QString configItem_localMysql_host; 36 | 37 | /** 38 | * @brief configItem_localMysql_user 本地数据库的user 39 | */ 40 | QString configItem_localMysql_user; 41 | 42 | /** 43 | * @brief configItem_localMysql_password 本地数据库的pwd 44 | */ 45 | QString configItem_localMysql_password; 46 | 47 | /** 48 | * @brief configItem_localMysql_db 本地数据库的db 49 | */ 50 | QString configItem_localMysql_db; 51 | 52 | /** 53 | * @brief configItem_localMysql_port 本地数据库的port 54 | */ 55 | int configItem_localMysql_port; 56 | 57 | private: 58 | NConfig nConfig_p; 59 | }; 60 | 61 | #endif // APPCUSTOMCONFIG_H 62 | -------------------------------------------------------------------------------- /NConfig/AppCustomConfig/src/appcustomconfig.cpp: -------------------------------------------------------------------------------- 1 | #include "appcustomconfig.h" 2 | 3 | AppCustomConfig::AppCustomConfig(QObject *parent) 4 | :QObject(parent) 5 | { 6 | 7 | } 8 | 9 | AppCustomConfig::~AppCustomConfig() 10 | { 11 | 12 | } 13 | 14 | bool AppCustomConfig::initAndReadConf(QString confFilePath, QString confFilePwd) 15 | { 16 | // 获取配置文件 17 | bool ret = nConfig_p.installAndInit(confFilePath, confFilePwd); 18 | if(!ret) return ret; 19 | // 读取配置项 20 | if(!nConfig_p.getConfigItem("localmysql","host",configItem_localMysql_host)){ 21 | qDebug() << "读取配置项失败"; 22 | return false; 23 | }else { 24 | qDebug() << "读取配置成功:" < 2 | #include "appcustomconfig.h" 3 | int main(int argc, char *argv[]) 4 | { 5 | QCoreApplication a(argc, argv); 6 | // 读一遍 7 | if(!APPConfigInstance.initAndReadConf(QCoreApplication::applicationDirPath() + "/app.conf","daodaoliang")){ 8 | qDebug()<<"读取配置文件失败"; 9 | } 10 | // 再读一遍 11 | if(!APPConfigInstance.initAndReadConf(QCoreApplication::applicationDirPath() + "/app.conf","daodaoliang")){ 12 | qDebug()<<"读取配置文件失败"; 13 | } 14 | return a.exec(); 15 | } 16 | -------------------------------------------------------------------------------- /NConfig/NConfig.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = subdirs 2 | CONFIG += ordered 3 | SUBDIRS += \ 4 | NConfig \ 5 | #example \ 6 | #AppCustomConfig 7 | -------------------------------------------------------------------------------- /NConfig/NConfig/3rdparty/wxsqlite3/codec.c: -------------------------------------------------------------------------------- 1 | /* 2 | /////////////////////////////////////////////////////////////////////////////// 3 | /////////////////////////////////////////////////////////////////////////////// 4 | // Name: codec.cpp 5 | // Purpose: 6 | // Author: Ulrich Telle 7 | // Modified by: 8 | // Created: 2006-12-06 9 | // RCS-ID: $$ 10 | // Copyright: (c) Ulrich Telle 11 | // Licence: wxWindows licence + RSA Data Security license 12 | /////////////////////////////////////////////////////////////////////////////// 13 | 14 | /// \file codec.cpp Implementation of MD5, RC4 and AES algorithms 15 | */ 16 | /* 17 | ********************************************************************** 18 | ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. ** 19 | ** ** 20 | ** License to copy and use this software is granted provided that ** 21 | ** it is identified as the "RSA Data Security, Inc. MD5 Message ** 22 | ** Digest Algorithm" in all material mentioning or referencing this ** 23 | ** software or this function. ** 24 | ** ** 25 | ** License is also granted to make and use derivative works ** 26 | ** provided that such works are identified as "derived from the RSA ** 27 | ** Data Security, Inc. MD5 Message Digest Algorithm" in all ** 28 | ** material mentioning or referencing the derived work. ** 29 | ** ** 30 | ** RSA Data Security, Inc. makes no representations concerning ** 31 | ** either the merchantability of this software or the suitability ** 32 | ** of this software for any particular purpose. It is provided "as ** 33 | ** is" without express or implied warranty of any kind. ** 34 | ** ** 35 | ** These notices must be retained in any copies of any part of this ** 36 | ** documentation and/or software. ** 37 | ********************************************************************** 38 | */ 39 | 40 | #include "codec.h" 41 | 42 | #if CODEC_TYPE == CODEC_TYPE_AES256 43 | #include "sha2.h" 44 | #include "sha2.c" 45 | #endif 46 | 47 | /* 48 | // ---------------- 49 | // MD5 by RSA 50 | // ---------------- 51 | 52 | // C headers for MD5 53 | */ 54 | #include 55 | #include 56 | #include 57 | #include 58 | 59 | #define MD5_HASHBYTES 16 60 | 61 | /* 62 | /// Structure representing an MD5 context while ecrypting. (For internal use only) 63 | */ 64 | typedef struct MD5Context 65 | { 66 | unsigned int buf[4]; 67 | unsigned int bits[2]; 68 | unsigned char in[64]; 69 | } MD5_CTX; 70 | 71 | static void MD5Init(MD5_CTX *context); 72 | static void MD5Update(MD5_CTX *context, unsigned char *buf, unsigned len); 73 | static void MD5Final(unsigned char digest[MD5_HASHBYTES], MD5_CTX *context); 74 | static void MD5Transform(unsigned int buf[4], unsigned int in[16]); 75 | 76 | static void byteReverse(unsigned char *buf, unsigned longs); 77 | 78 | /* 79 | * Note: this code is harmless on little-endian machines. 80 | */ 81 | static void byteReverse(unsigned char *buf, unsigned longs) 82 | { 83 | static int littleEndian = -1; 84 | if (littleEndian < 0) 85 | { 86 | /* Are we little or big endian? This method is from Harbison & Steele. */ 87 | union 88 | { 89 | long l; 90 | char c[sizeof(long)]; 91 | } u; 92 | u.l = 1; 93 | littleEndian = (u.c[0] == 1) ? 1 : 0; 94 | } 95 | 96 | if (littleEndian != 1) 97 | { 98 | unsigned int t; 99 | do 100 | { 101 | t = (unsigned int) ((unsigned) buf[3] << 8 | buf[2]) << 16 | 102 | ((unsigned) buf[1] << 8 | buf[0]); 103 | *(unsigned int *) buf = t; 104 | buf += 4; 105 | } 106 | while (--longs); 107 | } 108 | } 109 | 110 | #if 0 111 | static char* MD5End(MD5_CTX *, char *); 112 | 113 | static char* MD5End(MD5_CTX *ctx, char *buf) 114 | { 115 | int i; 116 | unsigned char digest[MD5_HASHBYTES]; 117 | char hex[]="0123456789abcdef"; 118 | 119 | if (!buf) 120 | { 121 | buf = (char *)malloc(33); 122 | } 123 | 124 | if (!buf) 125 | { 126 | return 0; 127 | } 128 | 129 | MD5Final(digest,ctx); 130 | for (i=0;i> 4]; 133 | buf[i+i+1] = hex[digest[i] & 0x0f]; 134 | } 135 | buf[i+i] = '\0'; 136 | return buf; 137 | } 138 | #endif 139 | 140 | /* 141 | * Final wrapup - pad to 64-byte boundary with the bit pattern 142 | * 1 0* (64-bit count of bits processed, MSB-first) 143 | */ 144 | static void MD5Final(unsigned char digest[16], MD5_CTX *ctx) 145 | { 146 | unsigned count; 147 | unsigned char *p; 148 | 149 | /* Compute number of bytes mod 64 */ 150 | count = (ctx->bits[0] >> 3) & 0x3F; 151 | 152 | /* Set the first char of padding to 0x80. This is safe since there is 153 | always at least one byte free */ 154 | p = ctx->in + count; 155 | *p++ = 0x80; 156 | 157 | /* Bytes of padding needed to make 64 bytes */ 158 | count = 64 - 1 - count; 159 | 160 | /* Pad out to 56 mod 64 */ 161 | if (count < 8) 162 | { 163 | /* Two lots of padding: Pad the first block to 64 bytes */ 164 | memset(p, 0, count); 165 | byteReverse(ctx->in, 16); 166 | MD5Transform(ctx->buf, (unsigned int *) ctx->in); 167 | 168 | /* Now fill the next block with 56 bytes */ 169 | memset(ctx->in, 0, 56); 170 | } 171 | else 172 | { 173 | /* Pad block to 56 bytes */ 174 | memset(p, 0, count - 8); 175 | } 176 | byteReverse(ctx->in, 14); 177 | 178 | /* Append length in bits and transform */ 179 | ((unsigned int *) ctx->in)[14] = ctx->bits[0]; 180 | ((unsigned int *) ctx->in)[15] = ctx->bits[1]; 181 | 182 | MD5Transform(ctx->buf, (unsigned int *) ctx->in); 183 | byteReverse((unsigned char *) ctx->buf, 4); 184 | memcpy(digest, ctx->buf, 16); 185 | memset((char *) ctx, 0, sizeof(ctx)); /* In case it's sensitive */ 186 | } 187 | 188 | static void MD5Init(MD5_CTX *ctx) 189 | { 190 | ctx->buf[0] = 0x67452301; 191 | ctx->buf[1] = 0xefcdab89; 192 | ctx->buf[2] = 0x98badcfe; 193 | ctx->buf[3] = 0x10325476; 194 | 195 | ctx->bits[0] = 0; 196 | ctx->bits[1] = 0; 197 | } 198 | 199 | static void MD5Update(MD5_CTX *ctx, unsigned char *buf, unsigned len) 200 | { 201 | unsigned int t; 202 | 203 | /* Update bitcount */ 204 | 205 | t = ctx->bits[0]; 206 | if ((ctx->bits[0] = t + ((unsigned int) len << 3)) < t) 207 | { 208 | ctx->bits[1]++; /* Carry from low to high */ 209 | } 210 | ctx->bits[1] += len >> 29; 211 | 212 | t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ 213 | 214 | /* Handle any leading odd-sized chunks */ 215 | 216 | if (t) 217 | { 218 | unsigned char *p = (unsigned char *) ctx->in + t; 219 | 220 | t = 64 - t; 221 | if (len < t) 222 | { 223 | memcpy(p, buf, len); 224 | return; 225 | } 226 | memcpy(p, buf, t); 227 | byteReverse(ctx->in, 16); 228 | MD5Transform(ctx->buf, (unsigned int *) ctx->in); 229 | buf += t; 230 | len -= t; 231 | } 232 | /* Process data in 64-byte chunks */ 233 | 234 | while (len >= 64) 235 | { 236 | memcpy(ctx->in, buf, 64); 237 | byteReverse(ctx->in, 16); 238 | MD5Transform(ctx->buf, (unsigned int *) ctx->in); 239 | buf += 64; 240 | len -= 64; 241 | } 242 | 243 | /* Handle any remaining bytes of data. */ 244 | 245 | memcpy(ctx->in, buf, len); 246 | } 247 | 248 | 249 | /* #define F1(x, y, z) (x & y | ~x & z) */ 250 | #define F1(x, y, z) (z ^ (x & (y ^ z))) 251 | #define F2(x, y, z) F1(z, x, y) 252 | #define F3(x, y, z) (x ^ y ^ z) 253 | #define F4(x, y, z) (y ^ (x | ~z)) 254 | 255 | /* This is the central step in the MD5 algorithm. */ 256 | #define MD5STEP(f, w, x, y, z, data, s) \ 257 | ( w += f(x, y, z) + data, w = w<>(32-s), w += x ) 258 | 259 | /* 260 | * The core of the MD5 algorithm, this alters an existing MD5 hash to 261 | * reflect the addition of 16 longwords of new data. MD5Update blocks 262 | * the data and converts bytes into longwords for this routine. 263 | */ 264 | static void MD5Transform(unsigned int buf[4], unsigned int in[16]) 265 | { 266 | register unsigned int a, b, c, d; 267 | 268 | a = buf[0]; 269 | b = buf[1]; 270 | c = buf[2]; 271 | d = buf[3]; 272 | 273 | MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); 274 | MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); 275 | MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); 276 | MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); 277 | MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); 278 | MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); 279 | MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); 280 | MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); 281 | MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); 282 | MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); 283 | MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); 284 | MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); 285 | MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); 286 | MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); 287 | MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); 288 | MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); 289 | 290 | MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); 291 | MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); 292 | MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); 293 | MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); 294 | MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); 295 | MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); 296 | MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); 297 | MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); 298 | MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); 299 | MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); 300 | MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); 301 | MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); 302 | MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); 303 | MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); 304 | MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); 305 | MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); 306 | 307 | MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); 308 | MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); 309 | MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); 310 | MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); 311 | MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); 312 | MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); 313 | MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); 314 | MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); 315 | MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); 316 | MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); 317 | MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); 318 | MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); 319 | MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); 320 | MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); 321 | MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); 322 | MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); 323 | 324 | MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); 325 | MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); 326 | MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); 327 | MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); 328 | MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); 329 | MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); 330 | MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); 331 | MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); 332 | MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); 333 | MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); 334 | MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); 335 | MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); 336 | MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); 337 | MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); 338 | MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); 339 | MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); 340 | 341 | buf[0] += a; 342 | buf[1] += b; 343 | buf[2] += c; 344 | buf[3] += d; 345 | } 346 | 347 | /* 348 | // --------------------------- 349 | // RC4 implementation 350 | // --------------------------- 351 | */ 352 | 353 | /** 354 | * RC4 is the standard encryption algorithm used in PDF format 355 | */ 356 | 357 | void 358 | CodecRC4(Codec* codec, unsigned char* key, int keylen, 359 | unsigned char* textin, int textlen, 360 | unsigned char* textout) 361 | { 362 | int i; 363 | int j; 364 | int t; 365 | unsigned char rc4[256]; 366 | 367 | int a = 0; 368 | int b = 0; 369 | unsigned char k; 370 | 371 | for (i = 0; i < 256; i++) 372 | { 373 | rc4[i] = i; 374 | } 375 | j = 0; 376 | for (i = 0; i < 256; i++) 377 | { 378 | t = rc4[i]; 379 | j = (j + t + key[i % keylen]) % 256; 380 | rc4[i] = rc4[j]; 381 | rc4[j] = t; 382 | } 383 | 384 | for (i = 0; i < textlen; i++) 385 | { 386 | a = (a + 1) % 256; 387 | t = rc4[a]; 388 | b = (b + t) % 256; 389 | rc4[a] = rc4[b]; 390 | rc4[b] = t; 391 | k = rc4[(rc4[a] + rc4[b]) % 256]; 392 | textout[i] = textin[i] ^ k; 393 | } 394 | } 395 | 396 | void 397 | CodecGetMD5Binary(Codec* codec, unsigned char* data, int length, unsigned char* digest) 398 | { 399 | MD5_CTX ctx; 400 | MD5Init(&ctx); 401 | MD5Update(&ctx, data, length); 402 | MD5Final(digest,&ctx); 403 | } 404 | 405 | #if CODEC_TYPE == CODEC_TYPE_AES256 406 | void 407 | CodecGetSHABinary(Codec* codec, unsigned char* data, int length, unsigned char* digest) 408 | { 409 | sha256(data, (unsigned int) length, digest); 410 | } 411 | #endif 412 | 413 | #define MODMULT(a, b, c, m, s) q = s / a; s = b * (s - a * q) - c * q; if (s < 0) s += m 414 | 415 | void 416 | CodecGenerateInitialVector(Codec* codec, int seed, unsigned char iv[16]) 417 | { 418 | unsigned char initkey[16]; 419 | int j, q; 420 | int z = seed + 1; 421 | for (j = 0; j < 4; j++) 422 | { 423 | MODMULT(52774, 40692, 3791, 2147483399L, z); 424 | initkey[4*j+0] = 0xff & z; 425 | initkey[4*j+1] = 0xff & (z >> 8); 426 | initkey[4*j+2] = 0xff & (z >> 16); 427 | initkey[4*j+3] = 0xff & (z >> 24); 428 | } 429 | CodecGetMD5Binary(codec, (unsigned char*) initkey, 16, iv); 430 | } 431 | 432 | void 433 | CodecAES(Codec* codec, int page, int encrypt, unsigned char encryptionKey[KEYLENGTH], 434 | unsigned char* datain, int datalen, unsigned char* dataout) 435 | { 436 | unsigned char initial[16]; 437 | unsigned char pagekey[KEYLENGTH]; 438 | unsigned char nkey[KEYLENGTH+4+4]; 439 | int keyLength = KEYLENGTH; 440 | int nkeylen = keyLength + 4 + 4; 441 | int j; 442 | int direction = (encrypt) ? RIJNDAEL_Direction_Encrypt : RIJNDAEL_Direction_Decrypt; 443 | int len = 0; 444 | 445 | for (j = 0; j < keyLength; j++) 446 | { 447 | nkey[j] = encryptionKey[j]; 448 | } 449 | nkey[keyLength+0] = 0xff & page; 450 | nkey[keyLength+1] = 0xff & (page >> 8); 451 | nkey[keyLength+2] = 0xff & (page >> 16); 452 | nkey[keyLength+3] = 0xff & (page >> 24); 453 | 454 | /* AES encryption needs some 'salt' */ 455 | nkey[keyLength+4] = 0x73; 456 | nkey[keyLength+5] = 0x41; 457 | nkey[keyLength+6] = 0x6c; 458 | nkey[keyLength+7] = 0x54; 459 | 460 | #if CODEC_TYPE == CODEC_TYPE_AES256 461 | CodecGetSHABinary(codec, nkey, nkeylen, pagekey); 462 | #else 463 | CodecGetMD5Binary(codec, nkey, nkeylen, pagekey); 464 | #endif 465 | CodecGenerateInitialVector(codec, page, initial); 466 | 467 | #if CODEC_TYPE == CODEC_TYPE_AES256 468 | RijndaelInit(codec->m_aes, RIJNDAEL_Direction_Mode_CBC, direction, pagekey, RIJNDAEL_Direction_KeyLength_Key32Bytes, initial); 469 | #else 470 | RijndaelInit(codec->m_aes, RIJNDAEL_Direction_Mode_CBC, direction, pagekey, RIJNDAEL_Direction_KeyLength_Key16Bytes, initial); 471 | #endif 472 | if (encrypt) 473 | { 474 | len = RijndaelBlockEncrypt(codec->m_aes, datain, datalen*8, dataout); 475 | } 476 | else 477 | { 478 | len = RijndaelBlockDecrypt(codec->m_aes, datain, datalen*8, dataout); 479 | } 480 | 481 | /* It is a good idea to check the error code */ 482 | if (len < 0) 483 | { 484 | /* AES: Error on encrypting. */ 485 | } 486 | } 487 | 488 | static unsigned char padding[] = 489 | "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A"; 490 | 491 | void 492 | CodecInit(Codec* codec) 493 | { 494 | codec->m_isEncrypted = 0; 495 | codec->m_hasReadKey = 0; 496 | codec->m_hasWriteKey = 0; 497 | codec->m_aes = (Rijndael*) sqlite3_malloc(sizeof(Rijndael)); 498 | RijndaelCreate(codec->m_aes); 499 | } 500 | 501 | void 502 | CodecTerm(Codec* codec) 503 | { 504 | sqlite3_free(codec->m_aes); 505 | } 506 | 507 | void 508 | CodecSetIsEncrypted(Codec* codec, int isEncrypted) 509 | { 510 | codec->m_isEncrypted = isEncrypted; 511 | } 512 | 513 | void 514 | CodecSetHasReadKey(Codec* codec, int hasReadKey) 515 | { 516 | codec->m_hasReadKey = hasReadKey; 517 | } 518 | 519 | void 520 | CodecSetHasWriteKey(Codec* codec, int hasWriteKey) 521 | { 522 | codec->m_hasWriteKey = hasWriteKey; 523 | } 524 | 525 | void 526 | CodecSetBtree(Codec* codec, Btree* bt) 527 | { 528 | codec->m_bt = bt; 529 | } 530 | 531 | int 532 | CodecIsEncrypted(Codec* codec) 533 | { 534 | return codec->m_isEncrypted; 535 | } 536 | 537 | int 538 | CodecHasReadKey(Codec* codec) 539 | { 540 | return codec->m_hasReadKey; 541 | } 542 | 543 | int 544 | CodecHasWriteKey(Codec* codec) 545 | { 546 | return codec->m_hasWriteKey; 547 | } 548 | 549 | Btree* 550 | CodecGetBtree(Codec* codec) 551 | { 552 | return codec->m_bt; 553 | } 554 | 555 | unsigned char* 556 | CodecGetPageBuffer(Codec* codec) 557 | { 558 | return &codec->m_page[4]; 559 | } 560 | 561 | void 562 | CodecCopy(Codec* codec, Codec* other) 563 | { 564 | int j; 565 | codec->m_isEncrypted = other->m_isEncrypted; 566 | codec->m_hasReadKey = other->m_hasReadKey; 567 | codec->m_hasWriteKey = other->m_hasWriteKey; 568 | for (j = 0; j < KEYLENGTH; j++) 569 | { 570 | codec->m_readKey[j] = other->m_readKey[j]; 571 | codec->m_writeKey[j] = other->m_writeKey[j]; 572 | } 573 | codec->m_bt = other->m_bt; 574 | RijndaelInvalidate(codec->m_aes); 575 | } 576 | 577 | void 578 | CodecCopyKey(Codec* codec, int read2write) 579 | { 580 | int j; 581 | if (read2write) 582 | { 583 | for (j = 0; j < KEYLENGTH; j++) 584 | { 585 | codec->m_writeKey[j] = codec->m_readKey[j]; 586 | } 587 | } 588 | else 589 | { 590 | for (j = 0; j < KEYLENGTH; j++) 591 | { 592 | codec->m_readKey[j] = codec->m_writeKey[j]; 593 | } 594 | } 595 | } 596 | 597 | void 598 | CodecPadPassword(Codec* codec, char* password, int pswdlen, unsigned char pswd[32]) 599 | { 600 | int j; 601 | int p = 0; 602 | int m = pswdlen; 603 | if (m > 32) m = 32; 604 | 605 | for (j = 0; j < m; j++) 606 | { 607 | pswd[p++] = (unsigned char) password[j]; 608 | } 609 | for (j = 0; p < 32 && j < 32; j++) 610 | { 611 | pswd[p++] = padding[j]; 612 | } 613 | } 614 | 615 | void 616 | CodecGenerateReadKey(Codec* codec, char* userPassword, int passwordLength) 617 | { 618 | CodecGenerateEncryptionKey(codec, userPassword, passwordLength, codec->m_readKey); 619 | } 620 | 621 | void 622 | CodecGenerateWriteKey(Codec* codec, char* userPassword, int passwordLength) 623 | { 624 | CodecGenerateEncryptionKey(codec, userPassword, passwordLength, codec->m_writeKey); 625 | } 626 | 627 | void 628 | CodecGenerateEncryptionKey(Codec* codec, char* userPassword, int passwordLength, 629 | unsigned char encryptionKey[KEYLENGTH]) 630 | { 631 | #if CODEC_TYPE == CODEC_TYPE_AES256 632 | unsigned char userPad[32]; 633 | unsigned char digest[KEYLENGTH]; 634 | int keyLength = KEYLENGTH; 635 | int k; 636 | 637 | /* Pad password */ 638 | CodecPadPassword(codec, userPassword, passwordLength, userPad); 639 | 640 | sha256(userPad, 32, digest); 641 | for (k = 0; k < CODEC_SHA_ITER; ++k) 642 | { 643 | sha256(digest, KEYLENGTH, digest); 644 | } 645 | memcpy(encryptionKey, digest, keyLength); 646 | #else 647 | unsigned char userPad[32]; 648 | unsigned char ownerPad[32]; 649 | unsigned char ownerKey[32]; 650 | 651 | unsigned char mkey[MD5_HASHBYTES]; 652 | unsigned char digest[MD5_HASHBYTES]; 653 | int keyLength = MD5_HASHBYTES; 654 | int i, j, k; 655 | MD5_CTX ctx; 656 | 657 | /* Pad passwords */ 658 | CodecPadPassword(codec, userPassword, passwordLength, userPad); 659 | CodecPadPassword(codec, "", 0, ownerPad); 660 | 661 | /* Compute owner key */ 662 | 663 | MD5Init(&ctx); 664 | MD5Update(&ctx, ownerPad, 32); 665 | MD5Final(digest,&ctx); 666 | 667 | /* only use for the input as many bit as the key consists of */ 668 | for (k = 0; k < 50; ++k) 669 | { 670 | MD5Init(&ctx); 671 | MD5Update(&ctx, digest, keyLength); 672 | MD5Final(digest,&ctx); 673 | } 674 | memcpy(ownerKey, userPad, 32); 675 | for (i = 0; i < 20; ++i) 676 | { 677 | for (j = 0; j < keyLength ; ++j) 678 | { 679 | mkey[j] = (digest[j] ^ i); 680 | } 681 | CodecRC4(codec, mkey, keyLength, ownerKey, 32, ownerKey); 682 | } 683 | 684 | /* Compute encryption key */ 685 | 686 | MD5Init(&ctx); 687 | MD5Update(&ctx, userPad, 32); 688 | MD5Update(&ctx, ownerKey, 32); 689 | MD5Final(digest,&ctx); 690 | 691 | /* only use the really needed bits as input for the hash */ 692 | for (k = 0; k < 50; ++k) 693 | { 694 | MD5Init(&ctx); 695 | MD5Update(&ctx, digest, keyLength); 696 | MD5Final(digest, &ctx); 697 | } 698 | memcpy(encryptionKey, digest, keyLength); 699 | #endif 700 | } 701 | 702 | void 703 | CodecEncrypt(Codec* codec, int page, unsigned char* data, int len, int useWriteKey) 704 | { 705 | unsigned char* key = (useWriteKey) ? codec->m_writeKey : codec->m_readKey; 706 | CodecAES(codec, page, 1, key, data, len, data); 707 | 708 | } 709 | 710 | void 711 | CodecDecrypt(Codec* codec, int page, unsigned char* data, int len) 712 | { 713 | CodecAES(codec, page, 0, codec->m_readKey, data, len, data); 714 | } 715 | 716 | -------------------------------------------------------------------------------- /NConfig/NConfig/3rdparty/wxsqlite3/codec.h: -------------------------------------------------------------------------------- 1 | /* 2 | /////////////////////////////////////////////////////////////////////////////// 3 | // Name: codec.h 4 | // Purpose: 5 | // Author: Ulrich Telle 6 | // Modified by: 7 | // Created: 2006-12-06 8 | // Copyright: (c) Ulrich Telle 9 | // Licence: wxWindows licence 10 | /////////////////////////////////////////////////////////////////////////////// 11 | 12 | /// \file codec.h Interface of the codec class 13 | */ 14 | 15 | #ifndef _CODEC_H_ 16 | #define _CODEC_H_ 17 | 18 | #ifdef __cplusplus 19 | extern "C" { 20 | #endif 21 | 22 | #if defined(__BORLANDC__) 23 | #define __STDC__ 1 24 | #endif 25 | 26 | #if defined(__BORLANDC__) 27 | #undef __STDC__ 28 | #endif 29 | 30 | /* 31 | // ATTENTION: Macro similar to that in pager.c 32 | // TODO: Check in case of new version of SQLite 33 | */ 34 | #define WX_PAGER_MJ_PGNO(x) ((PENDING_BYTE/(x))+1) 35 | 36 | #ifdef __cplusplus 37 | } /* End of the 'extern "C"' block */ 38 | #endif 39 | 40 | #include "rijndael.h" 41 | 42 | #define CODEC_TYPE_AES128 1 43 | #define CODEC_TYPE_AES256 2 44 | 45 | #ifndef CODEC_TYPE 46 | #define CODEC_TYPE CODEC_TYPE_AES256 47 | #endif 48 | 49 | #if CODEC_TYPE == CODEC_TYPE_AES256 50 | #define KEYLENGTH 32 51 | #define CODEC_SHA_ITER 4001 52 | #else 53 | #define KEYLENGTH 16 54 | #endif 55 | 56 | typedef struct _Codec 57 | { 58 | int m_isEncrypted; 59 | int m_hasReadKey; 60 | unsigned char m_readKey[KEYLENGTH]; 61 | int m_hasWriteKey; 62 | unsigned char m_writeKey[KEYLENGTH]; 63 | Rijndael* m_aes; 64 | 65 | Btree* m_bt; /* Pointer to B-tree used by DB */ 66 | unsigned char m_page[SQLITE_MAX_PAGE_SIZE+8]; 67 | } Codec; 68 | 69 | void CodecInit(Codec* codec); 70 | void CodecTerm(Codec* codec); 71 | 72 | void CodecCopy(Codec* codec, Codec* other); 73 | 74 | void CodecGenerateReadKey(Codec* codec, char* userPassword, int passwordLength); 75 | 76 | void CodecGenerateWriteKey(Codec* codec, char* userPassword, int passwordLength); 77 | 78 | void CodecEncrypt(Codec* codec, int page, unsigned char* data, int len, int useWriteKey); 79 | 80 | void CodecDecrypt(Codec* codec, int page, unsigned char* data, int len); 81 | 82 | void CodecCopyKey(Codec* codec, int read2write); 83 | 84 | void CodecSetIsEncrypted(Codec* codec, int isEncrypted); 85 | void CodecSetHasReadKey(Codec* codec, int hasReadKey); 86 | void CodecSetHasWriteKey(Codec* codec, int hasWriteKey); 87 | void CodecSetBtree(Codec* codec, Btree* bt); 88 | 89 | int CodecIsEncrypted(Codec* codec); 90 | int CodecHasReadKey(Codec* codec); 91 | int CodecHasWriteKey(Codec* codec); 92 | Btree* CodecGetBtree(Codec* codec); 93 | unsigned char* CodecGetPageBuffer(Codec* codec); 94 | 95 | void CodecGenerateEncryptionKey(Codec* codec, char* userPassword, int passwordLength, 96 | unsigned char encryptionKey[KEYLENGTH]); 97 | 98 | void CodecPadPassword(Codec* codec, char* password, int pswdlen, unsigned char pswd[32]); 99 | 100 | void CodecRC4(Codec* codec, unsigned char* key, int keylen, 101 | unsigned char* textin, int textlen, 102 | unsigned char* textout); 103 | 104 | void CodecGetMD5Binary(Codec* codec, unsigned char* data, int length, unsigned char* digest); 105 | 106 | #if CODEC_TYPE == CODEC_TYPE_AES256 107 | void CodecGetSHABinary(Codec* codec, unsigned char* data, int length, unsigned char* digest); 108 | #endif 109 | 110 | void CodecGenerateInitialVector(Codec* codec, int seed, unsigned char iv[16]); 111 | 112 | void CodecAES(Codec* codec, int page, int encrypt, unsigned char encryptionKey[KEYLENGTH], 113 | unsigned char* datain, int datalen, unsigned char* dataout); 114 | 115 | #endif 116 | -------------------------------------------------------------------------------- /NConfig/NConfig/3rdparty/wxsqlite3/codecext.c: -------------------------------------------------------------------------------- 1 | #ifndef SQLITE_OMIT_DISKIO 2 | #ifdef SQLITE_HAS_CODEC 3 | 4 | #include "codec.h" 5 | 6 | void sqlite3_activate_see(const char *info) 7 | { 8 | } 9 | 10 | /* 11 | // Free the encryption data structure associated with a pager instance. 12 | // (called from the modified code in pager.c) 13 | */ 14 | void sqlite3CodecFree(void *pCodecArg) 15 | { 16 | if (pCodecArg) 17 | { 18 | CodecTerm(pCodecArg); 19 | sqlite3_free(pCodecArg); 20 | } 21 | } 22 | 23 | void sqlite3CodecSizeChange(void *pArg, int pageSize, int reservedSize) 24 | { 25 | } 26 | 27 | /* 28 | // Encrypt/Decrypt functionality, called by pager.c 29 | */ 30 | void* sqlite3Codec(void* pCodecArg, void* data, Pgno nPageNum, int nMode) 31 | { 32 | Codec* codec = NULL; 33 | int pageSize; 34 | if (pCodecArg == NULL) 35 | { 36 | return data; 37 | } 38 | codec = (Codec*) pCodecArg; 39 | if (!CodecIsEncrypted(codec)) 40 | { 41 | return data; 42 | } 43 | 44 | pageSize = sqlite3BtreeGetPageSize(CodecGetBtree(codec)); 45 | 46 | switch(nMode) 47 | { 48 | case 0: /* Undo a "case 7" journal file encryption */ 49 | case 2: /* Reload a page */ 50 | case 3: /* Load a page */ 51 | if (CodecHasReadKey(codec)) 52 | { 53 | CodecDecrypt(codec, nPageNum, (unsigned char*) data, pageSize); 54 | } 55 | break; 56 | 57 | case 6: /* Encrypt a page for the main database file */ 58 | if (CodecHasWriteKey(codec)) 59 | { 60 | unsigned char* pageBuffer = CodecGetPageBuffer(codec); 61 | memcpy(pageBuffer, data, pageSize); 62 | data = pageBuffer; 63 | CodecEncrypt(codec, nPageNum, (unsigned char*) data, pageSize, 1); 64 | } 65 | break; 66 | 67 | case 7: /* Encrypt a page for the journal file */ 68 | /* Under normal circumstances, the readkey is the same as the writekey. However, 69 | when the database is being rekeyed, the readkey is not the same as the writekey. 70 | The rollback journal must be written using the original key for the 71 | database file because it is, by nature, a rollback journal. 72 | Therefore, for case 7, when the rollback is being written, always encrypt using 73 | the database's readkey, which is guaranteed to be the same key that was used to 74 | read the original data. 75 | */ 76 | if (CodecHasReadKey(codec)) 77 | { 78 | unsigned char* pageBuffer = CodecGetPageBuffer(codec); 79 | memcpy(pageBuffer, data, pageSize); 80 | data = pageBuffer; 81 | CodecEncrypt(codec, nPageNum, (unsigned char*) data, pageSize, 0); 82 | } 83 | break; 84 | } 85 | return data; 86 | } 87 | 88 | void* mySqlite3PagerGetCodec( 89 | Pager *pPager 90 | ); 91 | 92 | void mySqlite3PagerSetCodec( 93 | Pager *pPager, 94 | void *(*xCodec)(void*,void*,Pgno,int), 95 | void (*xCodecSizeChng)(void*,int,int), 96 | void (*xCodecFree)(void*), 97 | void *pCodec 98 | ); 99 | 100 | int sqlite3CodecAttach(sqlite3* db, int nDb, const void* zKey, int nKey) 101 | { 102 | /* Attach a key to a database. */ 103 | Codec* codec = (Codec*) sqlite3_malloc(sizeof(Codec)); 104 | CodecInit(codec); 105 | 106 | /* No key specified, could mean either use the main db's encryption or no encryption */ 107 | if (zKey == NULL || nKey <= 0) 108 | { 109 | /* No key specified */ 110 | if (nDb != 0 && nKey > 0) 111 | { 112 | Codec* mainCodec = (Codec*) mySqlite3PagerGetCodec(sqlite3BtreePager(db->aDb[0].pBt)); 113 | /* Attached database, therefore use the key of main database, if main database is encrypted */ 114 | if (mainCodec != NULL && CodecIsEncrypted(mainCodec)) 115 | { 116 | CodecCopy(codec, mainCodec); 117 | CodecSetBtree(codec, db->aDb[nDb].pBt); 118 | #if (SQLITE_VERSION_NUMBER >= 3006016) 119 | mySqlite3PagerSetCodec(sqlite3BtreePager(db->aDb[nDb].pBt), sqlite3Codec, sqlite3CodecSizeChange, sqlite3CodecFree, codec); 120 | #else 121 | #if (SQLITE_VERSION_NUMBER >= 3003014) 122 | sqlite3PagerSetCodec(sqlite3BtreePager(db->aDb[nDb].pBt), sqlite3Codec, codec); 123 | #else 124 | sqlite3pager_set_codec(sqlite3BtreePager(db->aDb[nDb].pBt), sqlite3Codec, codec); 125 | #endif 126 | db->aDb[nDb].pAux = codec; 127 | db->aDb[nDb].xFreeAux = sqlite3CodecFree; 128 | #endif 129 | } 130 | else 131 | { 132 | CodecSetIsEncrypted(codec, 0); 133 | sqlite3_free(codec); 134 | } 135 | } 136 | } 137 | else 138 | { 139 | /* Key specified, setup encryption key for database */ 140 | CodecSetIsEncrypted(codec, 1); 141 | CodecSetHasReadKey(codec, 1); 142 | CodecSetHasWriteKey(codec, 1); 143 | CodecGenerateReadKey(codec, (char*) zKey, nKey); 144 | CodecCopyKey(codec, 1); 145 | CodecSetBtree(codec, db->aDb[nDb].pBt); 146 | #if (SQLITE_VERSION_NUMBER >= 3006016) 147 | mySqlite3PagerSetCodec(sqlite3BtreePager(db->aDb[nDb].pBt), sqlite3Codec, sqlite3CodecSizeChange, sqlite3CodecFree, codec); 148 | #else 149 | #if (SQLITE_VERSION_NUMBER >= 3003014) 150 | sqlite3PagerSetCodec(sqlite3BtreePager(db->aDb[nDb].pBt), sqlite3Codec, codec); 151 | #else 152 | sqlite3pager_set_codec(sqlite3BtreePager(db->aDb[nDb].pBt), sqlite3Codec, codec); 153 | #endif 154 | db->aDb[nDb].pAux = codec; 155 | db->aDb[nDb].xFreeAux = sqlite3CodecFree; 156 | #endif 157 | } 158 | return SQLITE_OK; 159 | } 160 | 161 | void sqlite3CodecGetKey(sqlite3* db, int nDb, void** zKey, int* nKey) 162 | { 163 | /* 164 | // The unencrypted password is not stored for security reasons 165 | // therefore always return NULL 166 | // If the main database is encrypted a key length of 1 is returned. 167 | // In that case an attached database will get the same encryption key 168 | // as the main database if no key was explicitly given for the attached database. 169 | */ 170 | Codec* mainCodec = (Codec*) mySqlite3PagerGetCodec(sqlite3BtreePager(db->aDb[0].pBt)); 171 | int keylen = (mainCodec != NULL && CodecIsEncrypted(mainCodec)) ? 1 : 0; 172 | *zKey = NULL; 173 | *nKey = keylen; 174 | } 175 | 176 | static int dbFindIndex(sqlite3* db, const char* zDb) 177 | { 178 | int dbIndex = 0; 179 | if (zDb != NULL) 180 | { 181 | int found = 0; 182 | int index; 183 | for (index = 0; found == 0 && index < db->nDb; ++index) 184 | { 185 | struct Db* pDb = &db->aDb[dbIndex]; 186 | if (strcmp(pDb->zName, zDb) == 0) 187 | { 188 | found = 1; 189 | dbIndex = index; 190 | } 191 | } 192 | if (found == 0) dbIndex = 0; 193 | } 194 | return dbIndex; 195 | } 196 | 197 | int sqlite3_key(sqlite3 *db, const void *zKey, int nKey) 198 | { 199 | /* The key is only set for the main database, not the temp database */ 200 | return sqlite3_key_v2(db, "main", zKey, nKey); 201 | } 202 | 203 | int sqlite3_key_v2(sqlite3 *db, const char *zDbName, const void *zKey, int nKey) 204 | { 205 | /* The key is only set for the main database, not the temp database */ 206 | int dbIndex = dbFindIndex(db, zDbName); 207 | return sqlite3CodecAttach(db, dbIndex, zKey, nKey); 208 | } 209 | 210 | int sqlite3_rekey_v2(sqlite3 *db, const char *zDbName, const void *zKey, int nKey) 211 | { 212 | /* Changes the encryption key for an existing database. */ 213 | int dbIndex = dbFindIndex(db, zDbName); 214 | int rc = SQLITE_ERROR; 215 | Btree* pbt = db->aDb[dbIndex].pBt; 216 | Pager* pPager = sqlite3BtreePager(pbt); 217 | Codec* codec = (Codec*) mySqlite3PagerGetCodec(pPager); 218 | 219 | if ((zKey == NULL || nKey == 0) && (codec == NULL || !CodecIsEncrypted(codec))) 220 | { 221 | /* 222 | // Database not encrypted and key not specified 223 | // therefore do nothing 224 | */ 225 | return SQLITE_OK; 226 | } 227 | 228 | if (codec == NULL || !CodecIsEncrypted(codec)) 229 | { 230 | /* 231 | // Database not encrypted, but key specified 232 | // therefore encrypt database 233 | */ 234 | if (codec == NULL) 235 | { 236 | codec = (Codec*) sqlite3_malloc(sizeof(Codec)); 237 | CodecInit(codec); 238 | } 239 | 240 | CodecSetIsEncrypted(codec, 1); 241 | CodecSetHasReadKey(codec, 0); /* Original database is not encrypted */ 242 | CodecSetHasWriteKey(codec, 1); 243 | CodecGenerateWriteKey(codec, (char*) zKey, nKey); 244 | CodecSetBtree(codec, pbt); 245 | #if (SQLITE_VERSION_NUMBER >= 3006016) 246 | mySqlite3PagerSetCodec(pPager, sqlite3Codec, sqlite3CodecSizeChange, sqlite3CodecFree, codec); 247 | #else 248 | #if (SQLITE_VERSION_NUMBER >= 3003014) 249 | sqlite3PagerSetCodec(pPager, sqlite3Codec, codec); 250 | #else 251 | sqlite3pager_set_codec(pPager, sqlite3Codec, codec); 252 | #endif 253 | db->aDb[dbIndex].pAux = codec; 254 | db->aDb[dbIndex].xFreeAux = sqlite3CodecFree; 255 | #endif 256 | } 257 | else if (zKey == NULL || nKey == 0) 258 | { 259 | /* 260 | // Database encrypted, but key not specified 261 | // therefore decrypt database 262 | // Keep read key, drop write key 263 | */ 264 | CodecSetHasWriteKey(codec, 0); 265 | } 266 | else 267 | { 268 | /* 269 | // Database encrypted and key specified 270 | // therefore re-encrypt database with new key 271 | // Keep read key, change write key to new key 272 | */ 273 | CodecGenerateWriteKey(codec, (char*) zKey, nKey); 274 | CodecSetHasWriteKey(codec, 1); 275 | } 276 | 277 | /* Start transaction */ 278 | rc = sqlite3BtreeBeginTrans(pbt, 1); 279 | if (!rc) 280 | { 281 | int pageSize = sqlite3BtreeGetPageSize(pbt); 282 | Pgno nSkip = WX_PAGER_MJ_PGNO(pageSize); 283 | #if (SQLITE_VERSION_NUMBER >= 3003014) 284 | DbPage *pPage; 285 | #else 286 | void *pPage; 287 | #endif 288 | Pgno n; 289 | /* Rewrite all pages using the new encryption key (if specified) */ 290 | #if (SQLITE_VERSION_NUMBER >= 3007001) 291 | Pgno nPage; 292 | int nPageCount = -1; 293 | sqlite3PagerPagecount(pPager, &nPageCount); 294 | nPage = nPageCount; 295 | #elif (SQLITE_VERSION_NUMBER >= 3006000) 296 | int nPageCount = -1; 297 | int rc = sqlite3PagerPagecount(pPager, &nPageCount); 298 | Pgno nPage = (Pgno) nPageCount; 299 | #elif (SQLITE_VERSION_NUMBER >= 3003014) 300 | Pgno nPage = sqlite3PagerPagecount(pPager); 301 | #else 302 | Pgno nPage = sqlite3pager_pagecount(pPager); 303 | #endif 304 | 305 | for (n = 1; rc == SQLITE_OK && n <= nPage; n++) 306 | { 307 | if (n == nSkip) continue; 308 | #if (SQLITE_VERSION_NUMBER >= 3003014) 309 | rc = sqlite3PagerGet(pPager, n, &pPage); 310 | #else 311 | rc = sqlite3pager_get(pPager, n, &pPage); 312 | #endif 313 | if (!rc) 314 | { 315 | #if (SQLITE_VERSION_NUMBER >= 3003014) 316 | rc = sqlite3PagerWrite(pPage); 317 | sqlite3PagerUnref(pPage); 318 | #else 319 | rc = sqlite3pager_write(pPage); 320 | sqlite3pager_unref(pPage); 321 | #endif 322 | } 323 | } 324 | } 325 | 326 | if (rc == SQLITE_OK) 327 | { 328 | /* Commit transaction if all pages could be rewritten */ 329 | rc = sqlite3BtreeCommit(pbt); 330 | } 331 | if (rc != SQLITE_OK) 332 | { 333 | /* Rollback in case of error */ 334 | #if (SQLITE_VERSION_NUMBER >= 3007011) 335 | sqlite3BtreeRollback(pbt, SQLITE_OK); 336 | #else 337 | sqlite3BtreeRollback(pbt); 338 | #endif 339 | } 340 | 341 | if (rc == SQLITE_OK) 342 | { 343 | /* Set read key equal to write key if necessary */ 344 | if (CodecHasWriteKey(codec)) 345 | { 346 | CodecCopyKey(codec, 0); 347 | CodecSetHasReadKey(codec, 1); 348 | } 349 | else 350 | { 351 | CodecSetIsEncrypted(codec, 0); 352 | } 353 | } 354 | else 355 | { 356 | /* Restore write key if necessary */ 357 | if (CodecHasReadKey(codec)) 358 | { 359 | CodecCopyKey(codec, 1); 360 | } 361 | else 362 | { 363 | CodecSetIsEncrypted(codec, 0); 364 | } 365 | } 366 | 367 | if (!CodecIsEncrypted(codec)) 368 | { 369 | /* Remove codec for unencrypted database */ 370 | #if (SQLITE_VERSION_NUMBER >= 3006016) 371 | mySqlite3PagerSetCodec(pPager, NULL, NULL, NULL, NULL); 372 | #else 373 | #if (SQLITE_VERSION_NUMBER >= 3003014) 374 | sqlite3PagerSetCodec(pPager, NULL, NULL); 375 | #else 376 | sqlite3pager_set_codec(pPager, NULL, NULL); 377 | #endif 378 | db->aDb[dbIndex].pAux = NULL; 379 | db->aDb[dbIndex].xFreeAux = NULL; 380 | sqlite3CodecFree(codec); 381 | #endif 382 | } 383 | return rc; 384 | } 385 | 386 | int sqlite3_rekey(sqlite3 *db, const void *zKey, int nKey) 387 | { 388 | return sqlite3_rekey_v2(db, "main", zKey, nKey); 389 | } 390 | 391 | #endif /* SQLITE_HAS_CODEC */ 392 | 393 | #endif /* SQLITE_OMIT_DISKIO */ 394 | -------------------------------------------------------------------------------- /NConfig/NConfig/3rdparty/wxsqlite3/rijndael.h: -------------------------------------------------------------------------------- 1 | /* 2 | /////////////////////////////////////////////////////////////////////////////// 3 | // Name: rijndael.h 4 | // Purpose: 5 | // Author: Ulrich Telle 6 | // Modified by: 7 | // Created: 2006-12-06 8 | // Copyright: (c) Ulrich Telle (Copyright for original code see below) 9 | // Licence: wxWindows licence 10 | // 11 | // The original code is unchanged 12 | /////////////////////////////////////////////////////////////////////////////// 13 | 14 | /// \file rijndael.h Interface of the Rijndael cipher 15 | */ 16 | 17 | #ifndef _RIJNDAEL_H_ 18 | #define _RIJNDAEL_H_ 19 | 20 | /* 21 | // File : rijndael.h 22 | // Creation date : Sun Nov 5 2000 03:21:05 CEST 23 | // Author : Szymon Stefanek (stefanek@tin.it) 24 | // 25 | // Another implementation of the Rijndael cipher. 26 | // This is intended to be an easily usable library file. 27 | // This code is public domain. 28 | // Based on the Vincent Rijmen and K.U.Leuven implementation 2.4. 29 | // 30 | // Original Copyright notice: 31 | // 32 | // rijndael-alg-fst.c v2.4 April '2000 33 | // rijndael-alg-fst.h 34 | // rijndael-api-fst.c 35 | // rijndael-api-fst.h 36 | // 37 | // Optimised ANSI C code 38 | // 39 | // authors: v1.0: Antoon Bosselaers 40 | // v2.0: Vincent Rijmen, K.U.Leuven 41 | // v2.3: Paulo Barreto 42 | // v2.4: Vincent Rijmen, K.U.Leuven 43 | // 44 | // This code is placed in the public domain. 45 | // 46 | 47 | // 48 | // This implementation works on 128 , 192 , 256 bit keys 49 | // and on 128 bit blocks 50 | // 51 | 52 | // 53 | // Example of usage: 54 | // 55 | // // Input data 56 | // unsigned char key[32]; // The key 57 | // initializeYour256BitKey(); // Obviously initialized with sth 58 | // const unsigned char * plainText = getYourPlainText(); // Your plain text 59 | // int plainTextLen = strlen(plainText); // Plain text length 60 | // 61 | // // Encrypting 62 | // Rijndael rin; 63 | // unsigned char output[plainTextLen + 16]; 64 | // 65 | // rin.init(Rijndael::CBC,Rijndael::Encrypt,key,Rijndael::Key32Bytes); 66 | // // It is a good idea to check the error code 67 | // int len = rin.padEncrypt(plainText,len,output); 68 | // if(len >= 0)useYourEncryptedText(); 69 | // else encryptError(len); 70 | // 71 | // // Decrypting: we can reuse the same object 72 | // unsigned char output2[len]; 73 | // rin.init(Rijndael::CBC,Rijndael::Decrypt,key,Rijndael::Key32Bytes)); 74 | // len = rin.padDecrypt(output,len,output2); 75 | // if(len >= 0)useYourDecryptedText(); 76 | // else decryptError(len); 77 | // 78 | */ 79 | 80 | #define _MAX_KEY_COLUMNS (256/32) 81 | #define _MAX_ROUNDS 14 82 | #define MAX_IV_SIZE 16 83 | 84 | /* We assume that unsigned int is 32 bits long.... */ 85 | typedef unsigned char UINT8; 86 | typedef unsigned int UINT32; 87 | typedef unsigned short UINT16; 88 | 89 | /* Error codes */ 90 | #define RIJNDAEL_SUCCESS 0 91 | #define RIJNDAEL_UNSUPPORTED_MODE -1 92 | #define RIJNDAEL_UNSUPPORTED_DIRECTION -2 93 | #define RIJNDAEL_UNSUPPORTED_KEY_LENGTH -3 94 | #define RIJNDAEL_BAD_KEY -4 95 | #define RIJNDAEL_NOT_INITIALIZED -5 96 | #define RIJNDAEL_BAD_DIRECTION -6 97 | #define RIJNDAEL_CORRUPTED_DATA -7 98 | 99 | #define RIJNDAEL_Direction_Encrypt 0 100 | #define RIJNDAEL_Direction_Decrypt 1 101 | 102 | #define RIJNDAEL_Direction_Mode_ECB 0 103 | #define RIJNDAEL_Direction_Mode_CBC 1 104 | #define RIJNDAEL_Direction_Mode_CFB1 2 105 | 106 | #define RIJNDAEL_Direction_KeyLength_Key16Bytes 0 107 | #define RIJNDAEL_Direction_KeyLength_Key24Bytes 1 108 | #define RIJNDAEL_Direction_KeyLength_Key32Bytes 2 109 | 110 | #define RIJNDAEL_State_Valid 0 111 | #define RIJNDAEL_State_Invalid 1 112 | 113 | /* 114 | /// Class implementing the Rijndael cipher. (For internal use only) 115 | */ 116 | 117 | typedef struct _Rijndael 118 | { 119 | int m_state; 120 | int m_mode; 121 | int m_direction; 122 | UINT8 m_initVector[MAX_IV_SIZE]; 123 | UINT32 m_uRounds; 124 | UINT8 m_expandedKey[_MAX_ROUNDS+1][4][4]; 125 | } Rijndael; 126 | 127 | void RijndaelCreate(Rijndael* rijndael); 128 | 129 | /* 130 | ////////////////////////////////////////////////////////////////////////////////////////// 131 | // API 132 | ////////////////////////////////////////////////////////////////////////////////////////// 133 | 134 | // init(): Initializes the crypt session 135 | // Returns RIJNDAEL_SUCCESS or an error code 136 | // mode : Rijndael::ECB, Rijndael::CBC or Rijndael::CFB1 137 | // You have to use the same mode for encrypting and decrypting 138 | // dir : Rijndael::Encrypt or Rijndael::Decrypt 139 | // A cipher instance works only in one direction 140 | // (Well , it could be easily modified to work in both 141 | // directions with a single init() call, but it looks 142 | // useless to me...anyway , it is a matter of generating 143 | // two expanded keys) 144 | // key : array of unsigned octets , it can be 16 , 24 or 32 bytes long 145 | // this CAN be binary data (it is not expected to be null terminated) 146 | // keyLen : Rijndael::Key16Bytes , Rijndael::Key24Bytes or Rijndael::Key32Bytes 147 | // initVector: initialization vector, you will usually use 0 here 148 | */ 149 | int RijndaelInit(Rijndael* rijndael, int mode, int dir, UINT8* key, int keyLen, UINT8* initVector); 150 | 151 | /* 152 | // Encrypts the input array (can be binary data) 153 | // The input array length must be a multiple of 16 bytes, the remaining part 154 | // is DISCARDED. 155 | // so it actually encrypts inputLen / 128 blocks of input and puts it in outBuffer 156 | // Input len is in BITS! 157 | // outBuffer must be at least inputLen / 8 bytes long. 158 | // Returns the encrypted buffer length in BITS or an error code < 0 in case of error 159 | */ 160 | int RijndaelBlockEncrypt(Rijndael* rijndael, UINT8 *input, int inputLen, UINT8 *outBuffer); 161 | 162 | /* 163 | // Encrypts the input array (can be binary data) 164 | // The input array can be any length , it is automatically padded on a 16 byte boundary. 165 | // Input len is in BYTES! 166 | // outBuffer must be at least (inputLen + 16) bytes long 167 | // Returns the encrypted buffer length in BYTES or an error code < 0 in case of error 168 | */ 169 | int RijndaelPadEncrypt(Rijndael* rijndael, UINT8 *input, int inputOctets, UINT8 *outBuffer); 170 | 171 | /* 172 | // Decrypts the input vector 173 | // Input len is in BITS! 174 | // outBuffer must be at least inputLen / 8 bytes long 175 | // Returns the decrypted buffer length in BITS and an error code < 0 in case of error 176 | */ 177 | int RijndaelBlockDecrypt(Rijndael* rijndael, UINT8 *input, int inputLen, UINT8 *outBuffer); 178 | 179 | /* 180 | // Decrypts the input vector 181 | // Input len is in BYTES! 182 | // outBuffer must be at least inputLen bytes long 183 | // Returns the decrypted buffer length in BYTES and an error code < 0 in case of error 184 | */ 185 | int RijndaelPadDecrypt(Rijndael* rijndael, UINT8 *input, int inputOctets, UINT8 *outBuffer); 186 | 187 | void RijndaelInvalidate(Rijndael* rijndael); 188 | void RijndaelKeySched(Rijndael* rijndael, UINT8 key[_MAX_KEY_COLUMNS][4]); 189 | void RijndaelKeyEncToDec(Rijndael* rijndael); 190 | void RijndaelEncrypt(Rijndael* rijndael, UINT8 a[16], UINT8 b[16]); 191 | void RijndaelDecrypt(Rijndael* rijndael, UINT8 a[16], UINT8 b[16]); 192 | 193 | #endif /* _RIJNDAEL_H_ */ 194 | -------------------------------------------------------------------------------- /NConfig/NConfig/3rdparty/wxsqlite3/sha2.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FIPS 180-2 SHA-224/256/384/512 implementation 3 | * Last update: 02/02/2007 4 | * Issue date: 04/30/2005 5 | * 6 | * Copyright (C) 2005, 2007 Olivier Gay 7 | * All rights reserved. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 1. Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 3. Neither the name of the project nor the names of its contributors 18 | * may be used to endorse or promote products derived from this software 19 | * without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | * SUCH DAMAGE. 32 | */ 33 | 34 | #ifndef SHA2_H 35 | #define SHA2_H 36 | 37 | #define SHA224_DIGEST_SIZE ( 224 / 8) 38 | #define SHA256_DIGEST_SIZE ( 256 / 8) 39 | #define SHA384_DIGEST_SIZE ( 384 / 8) 40 | #define SHA512_DIGEST_SIZE ( 512 / 8) 41 | 42 | #define SHA256_BLOCK_SIZE ( 512 / 8) 43 | #define SHA512_BLOCK_SIZE (1024 / 8) 44 | #define SHA384_BLOCK_SIZE SHA512_BLOCK_SIZE 45 | #define SHA224_BLOCK_SIZE SHA256_BLOCK_SIZE 46 | 47 | #ifndef SHA2_TYPES 48 | #define SHA2_TYPES 49 | typedef unsigned char uint8; 50 | typedef unsigned int uint32; 51 | 52 | typedef sqlite3_uint64 uint64; 53 | 54 | #if defined(_MSC_VER) || defined(__BORLANDC__) 55 | #define li_64(h) 0x##h##ui64 56 | #else 57 | #define li_64(h) 0x##h##ull 58 | #endif 59 | 60 | #if 0 // Start of original int64 defines 61 | 62 | #if defined(_MSC_VER) 63 | #if _MSC_VER >= 1310 64 | typedef unsigned long long uint64; 65 | #define li_64(h) 0x##h##ull 66 | #else 67 | typedef unsigned __int64 uint64; 68 | #define li_64(h) 0x##h##ui64 69 | #endif 70 | #elif defined(__BORLANDC__) && !defined(__MSDOS__) 71 | #define li_64(h) 0x##h##ull 72 | typedef __int64 uint64; 73 | #elif defined(__sun) 74 | #if defined(ULONG_MAX) && ULONG_MAX == 0xfffffffful 75 | #define li_64(h) 0x##h##ull 76 | typedef unsigned long long uint64; 77 | #elif defined(ULONG_LONG_MAX) && ULONG_LONG_MAX == 0xfffffffffffffffful 78 | #define li_64(h) 0x##h##ul 79 | typedef unsigned long uint64; 80 | #endif 81 | #elif defined(__MVS__) 82 | #define li_64(h) 0x##h##ull 83 | typedef unsigned int long long uint64; 84 | #elif defined(ULLONG_MAX) && ULLONG_MAX > 4294967295 85 | #if ULLONG_MAX == 18446744073709551615ull 86 | #define li_64(h) 0x##h##ull 87 | typedef unsigned long long uint64; 88 | #endif 89 | #elif defined(ULONG_LONG_MAX) && ULONG_LONG_MAX > 4294967295 90 | #if ULONG_LONG_MAX == 18446744073709551615 91 | #define li_64(h) 0x##h##ull 92 | typedef unsigned long long uint64; 93 | #endif 94 | #elif defined(ULONG_MAX) && ULONG_MAX > 4294967295 95 | #if ULONG_MAX == 18446744073709551615 96 | #define li_64(h) 0x##h##ul 97 | typedef unsigned long uint64; 98 | #endif 99 | #elif defined(UINT_MAX) && UINT_MAX > 4294967295 100 | #if UINT_MAX == 18446744073709551615 101 | #define li_64(h) 0x##h##u 102 | typedef unsigned int uint64; 103 | #endif 104 | #endif 105 | #endif 106 | 107 | #endif // End of original int64 defines 108 | // 109 | 110 | #ifdef __cplusplus 111 | extern "C" { 112 | #endif 113 | 114 | typedef struct { 115 | unsigned int tot_len; 116 | unsigned int len; 117 | unsigned char block[2 * SHA256_BLOCK_SIZE]; 118 | uint32 h[8]; 119 | } sha256_ctx; 120 | 121 | typedef struct { 122 | unsigned int tot_len; 123 | unsigned int len; 124 | unsigned char block[2 * SHA512_BLOCK_SIZE]; 125 | uint64 h[8]; 126 | } sha512_ctx; 127 | 128 | typedef sha512_ctx sha384_ctx; 129 | typedef sha256_ctx sha224_ctx; 130 | 131 | void sha224_init(sha224_ctx *ctx); 132 | void sha224_update(sha224_ctx *ctx, const unsigned char *message, 133 | unsigned int len); 134 | void sha224_final(sha224_ctx *ctx, unsigned char *digest); 135 | void sha224(const unsigned char *message, unsigned int len, 136 | unsigned char *digest); 137 | 138 | void sha256_init(sha256_ctx * ctx); 139 | void sha256_update(sha256_ctx *ctx, const unsigned char *message, 140 | unsigned int len); 141 | void sha256_final(sha256_ctx *ctx, unsigned char *digest); 142 | void sha256(const unsigned char *message, unsigned int len, 143 | unsigned char *digest); 144 | 145 | void sha384_init(sha384_ctx *ctx); 146 | void sha384_update(sha384_ctx *ctx, const unsigned char *message, 147 | unsigned int len); 148 | void sha384_final(sha384_ctx *ctx, unsigned char *digest); 149 | void sha384(const unsigned char *message, unsigned int len, 150 | unsigned char *digest); 151 | 152 | void sha512_init(sha512_ctx *ctx); 153 | void sha512_update(sha512_ctx *ctx, const unsigned char *message, 154 | unsigned int len); 155 | void sha512_final(sha512_ctx *ctx, unsigned char *digest); 156 | void sha512(const unsigned char *message, unsigned int len, 157 | unsigned char *digest); 158 | 159 | #ifdef __cplusplus 160 | } 161 | #endif 162 | 163 | #endif /* !SHA2_H */ 164 | 165 | -------------------------------------------------------------------------------- /NConfig/NConfig/3rdparty/wxsqlite3/sqlite3secure.c: -------------------------------------------------------------------------------- 1 | // To enable the extension functions define SQLITE_ENABLE_EXTFUNC on compiling this module 2 | #ifdef SQLITE_ENABLE_EXTFUNC 3 | #define sqlite3_open sqlite3_open_internal 4 | #define sqlite3_open16 sqlite3_open16_internal 5 | #define sqlite3_open_v2 sqlite3_open_v2_internal 6 | #endif 7 | 8 | #include "sqlite3.c" 9 | 10 | #ifdef SQLITE_ENABLE_EXTFUNC 11 | #undef sqlite3_open 12 | #undef sqlite3_open16 13 | #undef sqlite3_open_v2 14 | #endif 15 | 16 | #ifndef SQLITE_OMIT_DISKIO 17 | 18 | #ifdef SQLITE_HAS_CODEC 19 | 20 | /* 21 | ** Get the codec argument for this pager 22 | */ 23 | 24 | void* mySqlite3PagerGetCodec( 25 | Pager *pPager 26 | ){ 27 | #if (SQLITE_VERSION_NUMBER >= 3006016) 28 | return sqlite3PagerGetCodec(pPager); 29 | #else 30 | return (pPager->xCodec) ? pPager->pCodecArg : NULL; 31 | #endif 32 | } 33 | 34 | /* 35 | ** Set the codec argument for this pager 36 | */ 37 | 38 | void mySqlite3PagerSetCodec( 39 | Pager *pPager, 40 | void *(*xCodec)(void*,void*,Pgno,int), 41 | void (*xCodecSizeChng)(void*,int,int), 42 | void (*xCodecFree)(void*), 43 | void *pCodec 44 | ){ 45 | sqlite3PagerSetCodec(pPager, xCodec, xCodecSizeChng, xCodecFree, pCodec); 46 | } 47 | 48 | #include "rijndael.c" 49 | #include "codec.c" 50 | #include "codecext.c" 51 | 52 | #endif 53 | 54 | #endif 55 | 56 | #ifdef SQLITE_ENABLE_EXTFUNC 57 | 58 | #include "extensionfunctions.c" 59 | 60 | SQLITE_API int sqlite3_open( 61 | const char *filename, /* Database filename (UTF-8) */ 62 | sqlite3 **ppDb /* OUT: SQLite db handle */ 63 | ) 64 | { 65 | int ret = sqlite3_open_internal(filename, ppDb); 66 | if (ret == 0) 67 | { 68 | RegisterExtensionFunctions(*ppDb); 69 | } 70 | return ret; 71 | } 72 | 73 | SQLITE_API int sqlite3_open16( 74 | const void *filename, /* Database filename (UTF-16) */ 75 | sqlite3 **ppDb /* OUT: SQLite db handle */ 76 | ) 77 | { 78 | int ret = sqlite3_open16_internal(filename, ppDb); 79 | if (ret == 0) 80 | { 81 | RegisterExtensionFunctions(*ppDb); 82 | } 83 | return ret; 84 | } 85 | 86 | SQLITE_API int sqlite3_open_v2( 87 | const char *filename, /* Database filename (UTF-8) */ 88 | sqlite3 **ppDb, /* OUT: SQLite db handle */ 89 | int flags, /* Flags */ 90 | const char *zVfs /* Name of VFS module to use */ 91 | ) 92 | { 93 | int ret = sqlite3_open_v2_internal(filename, ppDb, flags, zVfs); 94 | if (ret == 0) 95 | { 96 | RegisterExtensionFunctions(*ppDb); 97 | } 98 | return ret; 99 | } 100 | 101 | #endif 102 | -------------------------------------------------------------------------------- /NConfig/NConfig/3rdparty/wxsqlite3/wxsqlite3.pri: -------------------------------------------------------------------------------- 1 | CONFIG(release, debug|release):DEFINES *= NDEBUG 2 | 3 | DEFINES += SQLITE_HAS_CODEC 4 | 5 | # 引入查找路径 6 | INCLUDEPATH += $$PWD 7 | DEPENDPATH += $$PWD 8 | 9 | # 引入源码 10 | HEADERS += \ 11 | $$PWD/sqlite3.h \ 12 | $$PWD/codec.h \ 13 | $$PWD/rijndael.h \ 14 | $$PWD/sha2.h 15 | 16 | SOURCES += \ 17 | $$PWD/sqlite3.c \ 18 | $$PWD/sqlite3secure.c \ 19 | $$PWD/codec.c \ 20 | $$PWD/rijndael.c \ 21 | $$PWD/codecext.c \ 22 | $$PWD/sha2.c 23 | -------------------------------------------------------------------------------- /NConfig/NConfig/3rdparty/zsqlitecipherdriver/zsqliteciphercachedresult.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | ** 3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). 4 | ** Contact: http://www.qt-project.org/legal 5 | ** 6 | ** This file is part of the QtSql module of the Qt Toolkit. 7 | ** 8 | ** $QT_BEGIN_LICENSE:LGPL$ 9 | ** Commercial License Usage 10 | ** Licensees holding valid commercial Qt licenses may use this file in 11 | ** accordance with the commercial license agreement provided with the 12 | ** Software or, alternatively, in accordance with the terms contained in 13 | ** a written agreement between you and Digia. For licensing terms and 14 | ** conditions see http://qt.digia.com/licensing. For further information 15 | ** use the contact form at http://qt.digia.com/contact-us. 16 | ** 17 | ** GNU Lesser General Public License Usage 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser 19 | ** General Public License version 2.1 as published by the Free Software 20 | ** Foundation and appearing in the file LICENSE.LGPL included in the 21 | ** packaging of this file. Please review the following information to 22 | ** ensure the GNU Lesser General Public License version 2.1 requirements 23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 24 | ** 25 | ** In addition, as a special exception, Digia gives you certain additional 26 | ** rights. These rights are described in the Digia Qt LGPL Exception 27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 28 | ** 29 | ** GNU General Public License Usage 30 | ** Alternatively, this file may be used under the terms of the GNU 31 | ** General Public License version 3.0 as published by the Free Software 32 | ** Foundation and appearing in the file LICENSE.GPL included in the 33 | ** packaging of this file. Please review the following information to 34 | ** ensure the GNU General Public License version 3.0 requirements will be 35 | ** met: http://www.gnu.org/copyleft/gpl.html. 36 | ** 37 | ** 38 | ** $QT_END_LICENSE$ 39 | ** 40 | ****************************************************************************/ 41 | 42 | #include "zsqliteciphercachedresult.h" 43 | 44 | #include 45 | #include 46 | #include 47 | 48 | QT_BEGIN_NAMESPACE 49 | 50 | /* 51 | QSqlCachedResult is a convenience class for databases that only allow 52 | forward only fetching. It will cache all the results so we can iterate 53 | backwards over the results again. 54 | 55 | All you need to do is to inherit from QSqlCachedResult and reimplement 56 | gotoNext(). gotoNext() will have a reference to the internal cache and 57 | will give you an index where you can start filling in your data. Special 58 | case: If the user actually wants a forward-only query, idx will be -1 59 | to indicate that we are not interested in the actual values. 60 | */ 61 | 62 | //This file is from %QtSrcCode%\qtbase\src\sql\kernel\qsqlcachedresult.cpp 63 | //Modified by zq @20140410. 64 | 65 | static const uint initial_cache_size = 128; 66 | 67 | class ZSqliteCipherCachedResultPrivate 68 | { 69 | public: 70 | ZSqliteCipherCachedResultPrivate(); 71 | bool canSeek(int i) const; 72 | inline int cacheCount() const; 73 | void init(int count, bool fo); 74 | void cleanup(); 75 | int nextIndex(); 76 | void revertLast(); 77 | 78 | ZSqliteCipherCachedResult::ValueCache cache; 79 | int rowCacheEnd; 80 | int colCount; 81 | bool forwardOnly; 82 | bool atEnd; 83 | }; 84 | 85 | ZSqliteCipherCachedResultPrivate::ZSqliteCipherCachedResultPrivate(): 86 | rowCacheEnd(0), colCount(0), forwardOnly(false), atEnd(false) 87 | { 88 | } 89 | 90 | void ZSqliteCipherCachedResultPrivate::cleanup() 91 | { 92 | cache.clear(); 93 | forwardOnly = false; 94 | atEnd = false; 95 | colCount = 0; 96 | rowCacheEnd = 0; 97 | } 98 | 99 | void ZSqliteCipherCachedResultPrivate::init(int count, bool fo) 100 | { 101 | Q_ASSERT(count); 102 | cleanup(); 103 | forwardOnly = fo; 104 | colCount = count; 105 | if (fo) { 106 | cache.resize(count); 107 | rowCacheEnd = count; 108 | } else { 109 | cache.resize(initial_cache_size * count); 110 | } 111 | } 112 | 113 | int ZSqliteCipherCachedResultPrivate::nextIndex() 114 | { 115 | if (forwardOnly) 116 | return 0; 117 | int newIdx = rowCacheEnd; 118 | if (newIdx + colCount > cache.size()) 119 | cache.resize(qMin(cache.size() * 2, cache.size() + 10000)); 120 | rowCacheEnd += colCount; 121 | 122 | return newIdx; 123 | } 124 | 125 | bool ZSqliteCipherCachedResultPrivate::canSeek(int i) const 126 | { 127 | if (forwardOnly || i < 0) 128 | return false; 129 | return rowCacheEnd >= (i + 1) * colCount; 130 | } 131 | 132 | void ZSqliteCipherCachedResultPrivate::revertLast() 133 | { 134 | if (forwardOnly) 135 | return; 136 | rowCacheEnd -= colCount; 137 | } 138 | 139 | inline int ZSqliteCipherCachedResultPrivate::cacheCount() const 140 | { 141 | Q_ASSERT(!forwardOnly); 142 | Q_ASSERT(colCount); 143 | return rowCacheEnd / colCount; 144 | } 145 | 146 | ////////////// 147 | 148 | ZSqliteCipherCachedResult::ZSqliteCipherCachedResult(const QSqlDriver * db): QSqlResult (db) 149 | { 150 | d = new ZSqliteCipherCachedResultPrivate(); 151 | } 152 | 153 | ZSqliteCipherCachedResult::~ZSqliteCipherCachedResult() 154 | { 155 | delete d; 156 | } 157 | 158 | void ZSqliteCipherCachedResult::init(int colCount) 159 | { 160 | d->init(colCount, isForwardOnly()); 161 | } 162 | 163 | bool ZSqliteCipherCachedResult::fetch(int i) 164 | { 165 | if ((!isActive()) || (i < 0)) 166 | return false; 167 | if (at() == i) 168 | return true; 169 | if (d->forwardOnly) { 170 | // speed hack - do not copy values if not needed 171 | if (at() > i || at() == QSql::AfterLastRow) 172 | return false; 173 | while(at() < i - 1) { 174 | if (!gotoNext(d->cache, -1)) 175 | return false; 176 | setAt(at() + 1); 177 | } 178 | if (!gotoNext(d->cache, 0)) 179 | return false; 180 | setAt(at() + 1); 181 | return true; 182 | } 183 | if (d->canSeek(i)) { 184 | setAt(i); 185 | return true; 186 | } 187 | if (d->rowCacheEnd > 0) 188 | setAt(d->cacheCount()); 189 | while (at() < i + 1) { 190 | if (!cacheNext()) { 191 | if (d->canSeek(i)) 192 | break; 193 | return false; 194 | } 195 | } 196 | setAt(i); 197 | 198 | return true; 199 | } 200 | 201 | bool ZSqliteCipherCachedResult::fetchNext() 202 | { 203 | if (d->canSeek(at() + 1)) { 204 | setAt(at() + 1); 205 | return true; 206 | } 207 | return cacheNext(); 208 | } 209 | 210 | bool ZSqliteCipherCachedResult::fetchPrevious() 211 | { 212 | return fetch(at() - 1); 213 | } 214 | 215 | bool ZSqliteCipherCachedResult::fetchFirst() 216 | { 217 | if (d->forwardOnly && at() != QSql::BeforeFirstRow) { 218 | return false; 219 | } 220 | if (d->canSeek(0)) { 221 | setAt(0); 222 | return true; 223 | } 224 | return cacheNext(); 225 | } 226 | 227 | bool ZSqliteCipherCachedResult::fetchLast() 228 | { 229 | if (d->atEnd) { 230 | if (d->forwardOnly) 231 | return false; 232 | else 233 | return fetch(d->cacheCount() - 1); 234 | } 235 | 236 | int i = at(); 237 | while (fetchNext()) 238 | ++i; /* brute force */ 239 | if (d->forwardOnly && at() == QSql::AfterLastRow) { 240 | setAt(i); 241 | return true; 242 | } else { 243 | return fetch(i); 244 | } 245 | } 246 | 247 | QVariant ZSqliteCipherCachedResult::data(int i) 248 | { 249 | int idx = d->forwardOnly ? i : at() * d->colCount + i; 250 | if (i >= d->colCount || i < 0 || at() < 0 || idx >= d->rowCacheEnd) 251 | return QVariant(); 252 | 253 | return d->cache.at(idx); 254 | } 255 | 256 | bool ZSqliteCipherCachedResult::isNull(int i) 257 | { 258 | int idx = d->forwardOnly ? i : at() * d->colCount + i; 259 | if (i >= d->colCount || i < 0 || at() < 0 || idx >= d->rowCacheEnd) 260 | return true; 261 | 262 | return d->cache.at(idx).isNull(); 263 | } 264 | 265 | void ZSqliteCipherCachedResult::cleanup() 266 | { 267 | setAt(QSql::BeforeFirstRow); 268 | setActive(false); 269 | d->cleanup(); 270 | } 271 | 272 | void ZSqliteCipherCachedResult::clearValues() 273 | { 274 | setAt(QSql::BeforeFirstRow); 275 | d->rowCacheEnd = 0; 276 | d->atEnd = false; 277 | } 278 | 279 | bool ZSqliteCipherCachedResult::cacheNext() 280 | { 281 | if (d->atEnd) 282 | return false; 283 | 284 | if(isForwardOnly()) { 285 | d->cache.clear(); 286 | d->cache.resize(d->colCount); 287 | } 288 | 289 | if (!gotoNext(d->cache, d->nextIndex())) { 290 | d->revertLast(); 291 | d->atEnd = true; 292 | return false; 293 | } 294 | setAt(at() + 1); 295 | return true; 296 | } 297 | 298 | int ZSqliteCipherCachedResult::colCount() const 299 | { 300 | return d->colCount; 301 | } 302 | 303 | ZSqliteCipherCachedResult::ValueCache &ZSqliteCipherCachedResult::cache() 304 | { 305 | return d->cache; 306 | } 307 | 308 | void ZSqliteCipherCachedResult::virtual_hook(int id, void *data) 309 | { 310 | QSqlResult::virtual_hook(id, data); 311 | } 312 | 313 | void ZSqliteCipherCachedResult::detachFromResultSet() 314 | { 315 | cleanup(); 316 | } 317 | 318 | void ZSqliteCipherCachedResult::setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy policy) 319 | { 320 | QSqlResult::setNumericalPrecisionPolicy(policy); 321 | cleanup(); 322 | } 323 | 324 | 325 | QT_END_NAMESPACE 326 | -------------------------------------------------------------------------------- /NConfig/NConfig/3rdparty/zsqlitecipherdriver/zsqliteciphercachedresult.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | ** 3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). 4 | ** Contact: http://www.qt-project.org/legal 5 | ** 6 | ** This file is part of the QtSql module of the Qt Toolkit. 7 | ** 8 | ** $QT_BEGIN_LICENSE:LGPL$ 9 | ** Commercial License Usage 10 | ** Licensees holding valid commercial Qt licenses may use this file in 11 | ** accordance with the commercial license agreement provided with the 12 | ** Software or, alternatively, in accordance with the terms contained in 13 | ** a written agreement between you and Digia. For licensing terms and 14 | ** conditions see http://qt.digia.com/licensing. For further information 15 | ** use the contact form at http://qt.digia.com/contact-us. 16 | ** 17 | ** GNU Lesser General Public License Usage 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser 19 | ** General Public License version 2.1 as published by the Free Software 20 | ** Foundation and appearing in the file LICENSE.LGPL included in the 21 | ** packaging of this file. Please review the following information to 22 | ** ensure the GNU Lesser General Public License version 2.1 requirements 23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 24 | ** 25 | ** In addition, as a special exception, Digia gives you certain additional 26 | ** rights. These rights are described in the Digia Qt LGPL Exception 27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 28 | ** 29 | ** GNU General Public License Usage 30 | ** Alternatively, this file may be used under the terms of the GNU 31 | ** General Public License version 3.0 as published by the Free Software 32 | ** Foundation and appearing in the file LICENSE.GPL included in the 33 | ** packaging of this file. Please review the following information to 34 | ** ensure the GNU General Public License version 3.0 requirements will be 35 | ** met: http://www.gnu.org/copyleft/gpl.html. 36 | ** 37 | ** 38 | ** $QT_END_LICENSE$ 39 | ** 40 | ****************************************************************************/ 41 | 42 | #ifndef ZSQLITECIPHERCACHEDRESULT_H 43 | #define ZSQLITECIPHERCACHEDRESULT_H 44 | 45 | //This file is from %QtSrcCode%\qtbase\src\sql\kernel\qsqlcachedresult_p.h 46 | //Modified by zq @20140410. 47 | 48 | // 49 | // W A R N I N G 50 | // ------------- 51 | // 52 | // This file is not part of the Qt API. It exists for the convenience 53 | // of other Qt classes. This header file may change from version to 54 | // version without notice, or even be removed. 55 | // 56 | // We mean it. 57 | // 58 | 59 | #include "QtSql/qsqlresult.h" 60 | 61 | QT_BEGIN_NAMESPACE 62 | 63 | class QVariant; 64 | template class QVector; 65 | 66 | class ZSqliteCipherCachedResultPrivate; 67 | 68 | class ZSqliteCipherCachedResult: public QSqlResult 69 | { 70 | public: 71 | virtual ~ZSqliteCipherCachedResult(); 72 | 73 | typedef QVector ValueCache; 74 | 75 | protected: 76 | ZSqliteCipherCachedResult(const QSqlDriver * db); 77 | 78 | void init(int colCount); 79 | void cleanup(); 80 | void clearValues(); 81 | 82 | virtual bool gotoNext(ValueCache &values, int index) = 0; 83 | 84 | QVariant data(int i); 85 | bool isNull(int i); 86 | bool fetch(int i); 87 | bool fetchNext(); 88 | bool fetchPrevious(); 89 | bool fetchFirst(); 90 | bool fetchLast(); 91 | 92 | int colCount() const; 93 | ValueCache &cache(); 94 | 95 | void virtual_hook(int id, void *data); 96 | void detachFromResultSet(); 97 | void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy policy); 98 | private: 99 | bool cacheNext(); 100 | ZSqliteCipherCachedResultPrivate *d; 101 | }; 102 | 103 | QT_END_NAMESPACE 104 | 105 | #endif // QSQLCACHEDRESULT_P_H 106 | -------------------------------------------------------------------------------- /NConfig/NConfig/3rdparty/zsqlitecipherdriver/zsqlitecipherdriver.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | ** 3 | ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). 4 | ** Contact: http://www.qt-project.org/legal 5 | ** 6 | ** This file is part of the QtSql module of the Qt Toolkit. 7 | ** 8 | ** $QT_BEGIN_LICENSE:LGPL$ 9 | ** Commercial License Usage 10 | ** Licensees holding valid commercial Qt licenses may use this file in 11 | ** accordance with the commercial license agreement provided with the 12 | ** Software or, alternatively, in accordance with the terms contained in 13 | ** a written agreement between you and Digia. For licensing terms and 14 | ** conditions see http://qt.digia.com/licensing. For further information 15 | ** use the contact form at http://qt.digia.com/contact-us. 16 | ** 17 | ** GNU Lesser General Public License Usage 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser 19 | ** General Public License version 2.1 as published by the Free Software 20 | ** Foundation and appearing in the file LICENSE.LGPL included in the 21 | ** packaging of this file. Please review the following information to 22 | ** ensure the GNU Lesser General Public License version 2.1 requirements 23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 24 | ** 25 | ** In addition, as a special exception, Digia gives you certain additional 26 | ** rights. These rights are described in the Digia Qt LGPL Exception 27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 28 | ** 29 | ** GNU General Public License Usage 30 | ** Alternatively, this file may be used under the terms of the GNU 31 | ** General Public License version 3.0 as published by the Free Software 32 | ** Foundation and appearing in the file LICENSE.GPL included in the 33 | ** packaging of this file. Please review the following information to 34 | ** ensure the GNU General Public License version 3.0 requirements will be 35 | ** met: http://www.gnu.org/copyleft/gpl.html. 36 | ** 37 | ** 38 | ** $QT_END_LICENSE$ 39 | ** 40 | ****************************************************************************/ 41 | 42 | #ifndef ZSQLITECIPHERDRIVER_H 43 | #define ZSQLITECIPHERDRIVER_H 44 | 45 | #include 46 | #include 47 | #include "zsqliteciphercachedresult.h" 48 | 49 | struct sqlite3; 50 | 51 | class ZSQLiteCipherDriverPrivate; 52 | class ZSQLiteCipherResultPrivate; 53 | class ZSQLiteCipherDriver; 54 | 55 | class ZSQLiteCipherResult : public ZSqliteCipherCachedResult 56 | { 57 | friend class ZSQLiteCipherDriver; 58 | friend class ZSQLiteCipherResultPrivate; 59 | public: 60 | explicit ZSQLiteCipherResult(const ZSQLiteCipherDriver* db); 61 | ~ZSQLiteCipherResult(); 62 | QVariant handle() const; 63 | 64 | protected: 65 | bool gotoNext(ZSqliteCipherCachedResult::ValueCache& row, int idx); 66 | bool reset(const QString &query); 67 | bool prepare(const QString &query); 68 | bool exec(); 69 | int size(); 70 | int numRowsAffected(); 71 | QVariant lastInsertId() const; 72 | QSqlRecord record() const; 73 | #if (QT_VERSION >= 0x050000) 74 | void detachFromResultSet(); 75 | #endif 76 | void virtual_hook(int id, void *data); 77 | 78 | private: 79 | ZSQLiteCipherResultPrivate* d; 80 | }; 81 | 82 | class ZSQLiteCipherDriver : public QSqlDriver 83 | { 84 | Q_OBJECT 85 | friend class ZSQLiteCipherResult; 86 | public: 87 | explicit ZSQLiteCipherDriver(QObject *parent = 0); 88 | explicit ZSQLiteCipherDriver(sqlite3 *connection, QObject *parent = 0); 89 | ~ZSQLiteCipherDriver(); 90 | bool hasFeature(DriverFeature f) const; 91 | bool open(const QString & db, 92 | const QString & user, 93 | const QString & password, 94 | const QString & host, 95 | int port, 96 | const QString & connOpts); 97 | void close(); 98 | QSqlResult *createResult() const; 99 | bool beginTransaction(); 100 | bool commitTransaction(); 101 | bool rollbackTransaction(); 102 | QStringList tables(QSql::TableType) const; 103 | 104 | QSqlRecord record(const QString& tablename) const; 105 | QSqlIndex primaryIndex(const QString &table) const; 106 | QVariant handle() const; 107 | QString escapeIdentifier(const QString &identifier, IdentifierType) const; 108 | 109 | private: 110 | ZSQLiteCipherDriverPrivate* d; 111 | }; 112 | 113 | #endif // QSQL_SQLITE_H 114 | -------------------------------------------------------------------------------- /NConfig/NConfig/3rdparty/zsqlitecipherdriver/zsqlitecipherdriver.pri: -------------------------------------------------------------------------------- 1 | QT += core sql 2 | 3 | # 引入源码 4 | HEADERS += $$PWD/zsqlitecipherdriver.h \ 5 | $$PWD/zsqliteciphercachedresult.h 6 | 7 | SOURCES += $$PWD/zsqlitecipherdriver.cpp \ 8 | $$PWD/zsqliteciphercachedresult.cpp 9 | # 引入查找路径 10 | INCLUDEPATH += $$PWD 11 | -------------------------------------------------------------------------------- /NConfig/NConfig/NConfig.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2016-07-26T10:03:02 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += sql 8 | 9 | QT -= gui 10 | 11 | TARGET = NConfig 12 | TEMPLATE = lib 13 | CONFIG += c++11 14 | DEFINES += NCONFIG_LIBRARY 15 | 16 | # 引入源码 17 | include(NConfig_src.pri) 18 | INCLUDEPATH += ./inc/ \ 19 | 20 | # 引入第三方源码 21 | 22 | include($$PWD/3rdparty/wxsqlite3/wxsqlite3.pri) 23 | include($$PWD/3rdparty/zsqlitecipherdriver/zsqlitecipherdriver.pri) 24 | 25 | # 引入第三方头文件路径 26 | INCLUDEPATH += $$PWD\3rdparty\wxsqlite3 27 | INCLUDEPATH += $$PWD\3rdparty\zsqlitecipherdriver 28 | 29 | # 设置版本信息 30 | 31 | RC_FILE += ./NConfig_resource.rc 32 | 33 | # 设定编译输出路径 34 | win32{ 35 | CONFIG += debug_and_release 36 | CONFIG(release, debug|release) { 37 | target_path = ./build_/dist 38 | } else { 39 | target_path = ./build_/debug 40 | } 41 | DESTDIR = ./../../bin 42 | MOC_DIR = $$target_path/moc 43 | RCC_DIR = $$target_path/rcc 44 | OBJECTS_DIR = $$target_path/obj 45 | } 46 | 47 | # 输出编译套件信息 48 | message(Qt version: $$[QT_VERSION]) 49 | message(Qt is installed in $$[QT_INSTALL_PREFIX]) 50 | message(the NConfig will create in folder: $$target_path) 51 | -------------------------------------------------------------------------------- /NConfig/NConfig/NConfig_inc.pri: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2016-07-26T10:03:02 4 | # 5 | #------------------------------------------------- 6 | 7 | INCLUDEPATH += $$PWD\inc 8 | INCLUDEPATH += $$PWD\3rdparty\wxsqlite3 9 | INCLUDEPATH += $$PWD\3rdparty\zsqlitecipherdriver 10 | 11 | LIBS += -L$$PWD/../../bin/ -lNConfig 12 | 13 | -------------------------------------------------------------------------------- /NConfig/NConfig/NConfig_resource.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daodaoliang/NAuth/2ae4a54e2e9e81ced511326123b27c2e40500a65/NConfig/NConfig/NConfig_resource.rc -------------------------------------------------------------------------------- /NConfig/NConfig/NConfig_src.pri: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2016-07-26T10:03:02 4 | # 5 | #------------------------------------------------- 6 | 7 | SOURCES += $$PWD/src/nconfig.cpp 8 | 9 | HEADERS += $$PWD/inc/nconfig.h\ 10 | $$PWD/inc/nconfig_global.h 11 | 12 | -------------------------------------------------------------------------------- /NConfig/NConfig/inc/nconfig.h: -------------------------------------------------------------------------------- 1 | #ifndef NCONFIG_H 2 | #define NCONFIG_H 3 | /** 4 | * 作者: daodaoliang 5 | * 时间: 2016年7月28日 6 | * 版本: 1.0.5.0 7 | * 邮箱: daodaoliang@yeah.net 8 | */ 9 | #include "nconfig_global.h" 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include "zsqlitecipherdriver.h" 18 | 19 | /** 20 | * @brief The NConfigOperator class 配置项存取操作接口 21 | */ 22 | class NConfig_p : public QObject 23 | { 24 | Q_OBJECT 25 | public: 26 | NConfig_p(QObject * parent =0); 27 | ~NConfig_p(); 28 | public: 29 | /** 30 | * @brief initConfigDB 初始化配置数据库 31 | * @param paramPWD 密码 32 | * @param paramDBName 数据库名字 33 | * @return 是否初始化成功 34 | */ 35 | bool initConfigDB(const QString ¶mDBName, const QString ¶mPWD); 36 | 37 | /** 38 | * @brief release 释放资源 39 | */ 40 | void release(); 41 | 42 | /** 43 | * @brief getInnerDB 获取加密后的数据库实例 44 | * @return 45 | */ 46 | QSqlDatabase* getInnerDB(); 47 | 48 | private: 49 | 50 | /** 51 | * @brief openDB 打开指定数据库,若是不存在则创建 52 | * @param db 53 | * @param dbName 54 | * @param password 55 | * @return 56 | * 0 表示密码错误,或该文件不是数据库文件 57 | * 1 表示新建成功 58 | * 2 表示打开成功 59 | * -1 表示新建失败 60 | * -2 表示打开失败 61 | */ 62 | int openDB(QSqlDatabase &db, const QString &dbName,const QString &password); 63 | 64 | /** 65 | * @brief isDbDecrypted 数据库是否被揭秘 66 | * @param db 数据库 67 | * @return 68 | */ 69 | bool isDbDecrypted(QSqlDatabase &db); 70 | 71 | /** 72 | * @brief changePassword 修改数据库密码 73 | * 对已经成功打开了的数据库修改密码。注意: 74 | * 1. 如果数据库还未用正确密码打开,是无法使用本函数修改密码的。 75 | * 2. 如果数据库文件比较大,修改密码可能需要消耗比较长的时间(需要将所有的数据重新加密). 76 | * 3. 密码支持的字符,这里未测试,但是单引号肯定是不能用在密码中的. 77 | * @param db 数据库 78 | * @param newPassword 新密码 79 | * @return 是否设置成功 80 | */ 81 | bool changePassword(QSqlDatabase &db, const QString &newPassword); 82 | 83 | private: 84 | /** 85 | * @brief innerDriver 加密的数据库驱动 86 | */ 87 | ZSQLiteCipherDriver *innerDriver; 88 | 89 | /** 90 | * @brief innerDB 加密的数据库实例 91 | */ 92 | QSqlDatabase innerDB; 93 | }; 94 | 95 | /** 96 | * @brief The NConfig class 配置库接口 97 | */ 98 | class NCONFIGSHARED_EXPORT NConfig 99 | { 100 | 101 | public: 102 | NConfig(); 103 | ~NConfig(); 104 | public: 105 | 106 | /** 107 | * @brief installAndInit 初始化配置实例 108 | * @param paramDB 配置数据文件的名字 109 | * @param paramPWD 数据文件密码 110 | * @param paramCheckExist 是否检查文件是否存在 111 | * @return 112 | */ 113 | bool installAndInit(const QString ¶mDB, const QString ¶mPWD ,bool paramCheckExist = true); 114 | 115 | /** 116 | * @brief releaseConf 释放资源 117 | */ 118 | void releaseConf(); 119 | 120 | /** 121 | * @brief addConfigItem 增加配置项目(存更否创) 122 | * @param paramTableItem 配置项的表名 123 | * @param paramTableKey 配置项的名字 124 | * @param paramTableValue 配置项内容 125 | * @return 是否添加成功 126 | */ 127 | bool addConfigItem(const QString ¶mTableItem, const QString ¶mTableKey, const QString ¶mTableValue); 128 | 129 | /** 130 | * @brief getConfigItem 获取配置项 131 | * @param paramTableItem 配置项名字 132 | * @param paramTableKey 配置项key 133 | * @param paramTableValue 查询到的配置项值 134 | * @return 是否获取成功 135 | */ 136 | bool getConfigItem(const QString ¶mTableItem, const QString ¶mTableKey, QString ¶mTableValue); 137 | 138 | /** 139 | * @brief clearConfig 清空整个配置内容 140 | * @return 是否清空成功 141 | */ 142 | bool clearConfig(); 143 | 144 | /** 145 | * @brief exportConfigToFile 导出初始化后的配置库到文件里面(初始化后使用) 146 | * @return 是否导出成功 147 | */ 148 | bool exportConfigToFile(); 149 | 150 | /** 151 | * @brief importConfigFromFile 从ini文件导入到配置库(初始化后使用) 152 | * @param paramIniFile ini文件路径 153 | * @return 154 | */ 155 | bool importConfigFromFile(const QString paramIniFile); 156 | 157 | /** 158 | * @brief getIsDebug 获取运行模式 159 | * @return 160 | */ 161 | bool getIsDebug() const; 162 | 163 | /** 164 | * @brief setIsDebug 设置调试模式 165 | * @param value 166 | */ 167 | void setIsDebug(bool value); 168 | 169 | /** 170 | * @brief getConfigFilePath 获取配置库路径 171 | * @return 路径 172 | */ 173 | QString getConfigFilePath() const; 174 | 175 | /** 176 | * @brief setConfigFilePath 设置配置库路径 177 | * @param value 178 | */ 179 | void setConfigFilePath(const QString &value); 180 | 181 | private: 182 | /** 183 | * @brief innerOperator 内部加密库操作实例 184 | */ 185 | NConfig_p innerOperator; 186 | 187 | /** 188 | * @brief isDebug 是否是调试模式 189 | */ 190 | bool isDebug; 191 | 192 | /** 193 | * @brief configFilePath 配置库的文件路径 194 | */ 195 | QString configFilePath; 196 | 197 | /** 198 | * @brief configFileName 配置文件的名字 199 | */ 200 | QString configFileName; 201 | }; 202 | 203 | #endif // NCONFIG_H 204 | -------------------------------------------------------------------------------- /NConfig/NConfig/inc/nconfig_global.h: -------------------------------------------------------------------------------- 1 | #ifndef NCONFIG_GLOBAL_H 2 | #define NCONFIG_GLOBAL_H 3 | 4 | #include 5 | 6 | #if defined(NCONFIG_LIBRARY) 7 | # define NCONFIGSHARED_EXPORT Q_DECL_EXPORT 8 | #else 9 | # define NCONFIGSHARED_EXPORT Q_DECL_IMPORT 10 | #endif 11 | 12 | #endif // NCONFIG_GLOBAL_H 13 | -------------------------------------------------------------------------------- /NConfig/NConfig/src/nconfig.cpp: -------------------------------------------------------------------------------- 1 | #include "nconfig.h" 2 | NConfig_p::NConfig_p(QObject *parent):QObject(parent) 3 | { 4 | innerDriver = NULL; 5 | } 6 | 7 | NConfig_p::~NConfig_p() 8 | { 9 | if(innerDB.isOpen() && innerDB.isValid()){ 10 | innerDB.close(); 11 | } 12 | if(innerDriver != NULL) { 13 | innerDriver->deleteLater(); 14 | } 15 | innerDriver = NULL; 16 | QString name; 17 | { 18 | name = QSqlDatabase::database().connectionName(); 19 | } 20 | QSqlDatabase::removeDatabase(name); 21 | } 22 | 23 | bool NConfig_p::isDbDecrypted(QSqlDatabase &db) 24 | { 25 | QSqlQuery query(db); 26 | return query.exec("select name,sql from sqlite_master" 27 | " where type = 'table'"); 28 | } 29 | 30 | bool NConfig_p::changePassword(QSqlDatabase &db, const QString &newPassword) 31 | { 32 | QSqlQuery query(db); 33 | return query.exec(QString("PRAGMA rekey='%1';").arg(newPassword)); 34 | } 35 | 36 | QSqlDatabase *NConfig_p::getInnerDB() 37 | { 38 | return &innerDB; 39 | } 40 | 41 | bool NConfig_p::initConfigDB(const QString ¶mDBName, const QString ¶mPWD) 42 | { 43 | bool flag = false; 44 | if(!innerDriver){ 45 | innerDriver = new ZSQLiteCipherDriver(); 46 | } 47 | innerDB = QSqlDatabase::addDatabase(innerDriver); 48 | switch (openDB(innerDB, paramDBName, paramPWD)) { 49 | case -2: 50 | case -1: 51 | case 0: 52 | flag = false; 53 | break; 54 | case 1: 55 | case 2: 56 | flag = true; 57 | break; 58 | } 59 | return flag; 60 | } 61 | 62 | void NConfig_p::release() 63 | { 64 | if(innerDB.isOpen() && innerDB.isValid()){ 65 | innerDB.close(); 66 | } 67 | if(innerDriver != NULL) { 68 | innerDriver->deleteLater(); 69 | } 70 | innerDriver = NULL; 71 | QString name; 72 | { 73 | name = QSqlDatabase::database().connectionName(); 74 | } 75 | QSqlDatabase::removeDatabase(name); 76 | } 77 | 78 | int NConfig_p::openDB(QSqlDatabase &db, const QString &dbName, const QString &password) 79 | { 80 | QFile file(dbName); 81 | bool isDbFileExists = file.exists() && (file.size() > 0); 82 | db.setDatabaseName(dbName); 83 | db.setPassword(password); 84 | if (!db.open()) return isDbFileExists ? -2 : -1; 85 | if (isDbFileExists && !isDbDecrypted(db)) 86 | { 87 | db.close(); 88 | return 0; 89 | } 90 | return isDbFileExists ? 2 : 1; 91 | } 92 | 93 | NConfig::NConfig() 94 | { 95 | setIsDebug(true); 96 | } 97 | 98 | NConfig::~NConfig() 99 | { 100 | 101 | } 102 | 103 | bool NConfig::installAndInit(const QString ¶mDB, const QString ¶mPWD, bool paramCheckExist) 104 | { 105 | if(paramCheckExist){ 106 | QDir tempPath(paramDB); 107 | if(!QFile::exists(tempPath.absolutePath())){ 108 | if(isDebug) qDebug() << "文件不存在:" << tempPath.absolutePath(); 109 | return false; 110 | } 111 | } 112 | setConfigFilePath(paramDB); 113 | return innerOperator.initConfigDB(paramDB, paramPWD); 114 | } 115 | 116 | void NConfig::releaseConf() 117 | { 118 | innerOperator.release(); 119 | } 120 | 121 | bool NConfig::addConfigItem(const QString ¶mTableItem, const QString ¶mTableKey, const QString ¶mTableValue) 122 | { 123 | // 语句准备 124 | QString tableIsExistSql = QString("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='%1';").arg(paramTableItem); 125 | QString tableCreateSql = QString("create table `%1` (`key` varchar primary key,`value` varchar)").arg(paramTableItem); 126 | QString configInsertSql = QString("replace into `%1` (`key`,`value`) values ('%2','%3');").arg(paramTableItem, paramTableKey, paramTableValue); 127 | // 存在性保证 128 | QSqlQuery existQuery = innerOperator.getInnerDB()->exec(tableIsExistSql); 129 | if(existQuery.lastError().type() != QSqlError::NoError){ 130 | if(isDebug) qDebug()<< "addConfigItem error:" << existQuery.lastError().text(); 131 | existQuery.clear(); 132 | return false; 133 | } 134 | existQuery.next(); 135 | bool isExist = existQuery.value(0).toString() == "0" ? false : true; 136 | if(!isExist){ 137 | if(isDebug) qDebug()<exec(tableCreateSql); 139 | if(createQuery.lastError().type() != QSqlError::NoError){ 140 | if(isDebug) qDebug()<< createQuery.lastError().text(); 141 | createQuery.clear(); 142 | return false; 143 | } 144 | createQuery.clear(); 145 | if(isDebug) qDebug()<exec(configInsertSql); 150 | if(insertQuery.lastError().type() != QSqlError::NoError){ 151 | if(isDebug) qDebug()<< insertQuery.lastError().text(); 152 | insertQuery.clear(); 153 | return false; 154 | } 155 | insertQuery.clear(); 156 | if(isDebug) qDebug()<exec(tableIsExistSql); 167 | if(existQuery.lastError().type() != QSqlError::NoError){ 168 | if(isDebug) qDebug()<< "getConfigItem error:" << existQuery.lastError().text(); 169 | existQuery.clear(); 170 | return false; 171 | } 172 | existQuery.clear(); 173 | // 获取配置项 174 | QSqlQuery selectQuery = innerOperator.getInnerDB()->exec(configSelectSql); 175 | if(selectQuery.lastError().type() != QSqlError::NoError){ 176 | if(isDebug) qDebug()<< "getConfigItem error:" << selectQuery.lastError().text(); 177 | selectQuery.clear(); 178 | return false; 179 | } 180 | if(!selectQuery.next()){ 181 | if(isDebug) qDebug()<< "getConfigItem error:" << "no data!"; 182 | selectQuery.clear(); 183 | return false; 184 | } 185 | paramTableValue = QString("%1").arg(selectQuery.value("value").toString()); 186 | selectQuery.clear(); 187 | return true; 188 | } 189 | 190 | bool NConfig::clearConfig() 191 | { 192 | // 语句准备 193 | QString tableListSql = QString("select * from sqlite_master where type = 'table';"); 194 | // 清空操作 195 | QSqlQuery tableQuery = innerOperator.getInnerDB()->exec(tableListSql); 196 | if(tableQuery.lastError().type() != QSqlError::NoError){ 197 | if(isDebug) qDebug() << "clearConfig error:" << tableQuery.lastError().text(); 198 | tableQuery.clear(); 199 | return false; 200 | } 201 | QStringList tableNameList; 202 | while (tableQuery.next()){ 203 | tableNameList.append(tableQuery.value(1).toString()); 204 | } 205 | tableQuery.clear(); 206 | foreach (QString tableName, tableNameList) { 207 | if(isDebug) qDebug()<exec(dropTable); 210 | if(dropQuery.lastError().type() != QSqlError::NoError){ 211 | if(isDebug) qDebug() << QString("清理配置:%1 失败").arg(tableName) << dropQuery.lastError().text(); 212 | } 213 | } 214 | return true; 215 | } 216 | 217 | bool NConfig::exportConfigToFile() 218 | { 219 | // 语句准备 220 | QString tableListSql = QString("select * from sqlite_master where type = 'table';"); 221 | // 文件准备 222 | QString tempFileName = QString("%1%2.ini").arg(configFilePath, configFileName); 223 | if(QFile::exists(tempFileName)){ 224 | QFile::remove(tempFileName); 225 | } else { 226 | QFile temp(tempFileName); 227 | temp.open(QIODevice::ReadWrite); 228 | temp.close(); 229 | } 230 | QSettings tempFile(tempFileName,QSettings::IniFormat); 231 | // 文件操作 232 | QSqlQuery tableQuery = innerOperator.getInnerDB()->exec(tableListSql); 233 | if(tableQuery.lastError().type() != QSqlError::NoError){ 234 | if(isDebug) qDebug() << "exportConfigToFile error:" << tableQuery.lastError().text(); 235 | tableQuery.clear(); 236 | return false; 237 | } 238 | QStringList tempNameList; 239 | while (tableQuery.next()){ 240 | tempNameList.append(tableQuery.value(1).toString()); 241 | } 242 | tableQuery.clear(); 243 | foreach (QString tempName, tempNameList) { 244 | QString selectSql = QString("select * from `%1`").arg(tempName); 245 | QSqlQuery selectQuery = innerOperator.getInnerDB()->exec(selectSql); 246 | if(selectQuery.lastError().type() != QSqlError::NoError){ 247 | if(isDebug) qDebug() << QString("导出配置:%1 失败").arg(tempName) << selectQuery.lastError().text(); 248 | } else { 249 | if(isDebug) qDebug() << QString("导出配置:%1 开始").arg(tempName); 250 | } 251 | tempFile.beginGroup(tempName); 252 | while (selectQuery.next()) { 253 | QString tempKey = selectQuery.value("key").toString(); 254 | QString tempValue = selectQuery.value("value").toString(); 255 | if(isDebug) qDebug() << QString(" %1 : %2 ").arg(tempKey, tempValue); 256 | tempFile.setValue(tempKey, tempValue); 257 | } 258 | tempFile.endGroup(); 259 | if(isDebug) qDebug() << QString("导出配置:%1 完毕").arg(tempName); 260 | } 261 | return true; 262 | } 263 | 264 | bool NConfig::importConfigFromFile(const QString paramIniFile) 265 | { 266 | // 存在性判断 267 | QDir tempInnerFile(paramIniFile); 268 | QString tempInnerFilePath = tempInnerFile.absolutePath(); 269 | if(!QFile::exists(tempInnerFilePath)){ 270 | if(isDebug) qDebug() << QString("导入失败-文件:%1 不存在").arg(tempInnerFilePath); 271 | return false; 272 | } 273 | // 格式判断 274 | if(tempInnerFilePath.split(".").last() != "ini"){ 275 | if(isDebug) qDebug() <installAndInit("./test.conf", "daodaoliang", false); 33 | 34 | # 使用已经存在的配置库的初始化 35 | NConfig::instance()->installAndInit("./test.conf", "daodaoliang"); 36 | ``` 37 | 38 | * 添加配置项内容 39 | 40 | ```c 41 | NConfig::instance()->addConfigItem("nami","233333","MissU"); 42 | ``` 43 | 44 | * 获取配置项内容 45 | 46 | ```c 47 | QString tempValue; 48 | NConfig::instance()->getConfigItem("nami","233333",tempValue); 49 | ``` 50 | 51 | * 清空配置项内容 52 | 53 | ```c 54 | NConfig::instance()->clearConfig(); 55 | ``` 56 | 57 | * 导出为ini类型的配置文件 58 | 59 | ```c 60 | NConfig::instance()->exportConfigToFile(); 61 | ``` 62 | 63 | * 从ini类型的配置文件导入到配置库 64 | 65 | ```c 66 | NConfig::instance()->importConfigFromFile("./test.ini"); 67 | ``` 68 | 69 | ## 3. 组件路线图 70 | 71 | * ~~增加配置库文件的导出为ini文件;~~ 72 | * ~~增加从ini文件导入为配置库文件;~~ 73 | 74 | ## 4. changelog 75 | 76 | * V 1.0.1.0 集成第三方加密sqlite库进行初始化项目; 77 | * V 1.0.2.0 增加接口封装,实现初始化实例接口,增加项目配置接口,获取配置项目接口,清空配置接口,并编写测试用例; 78 | * V 1.0.3.0 增加导出为ini文件的接口; 79 | * V 1.0.4.0 增加从ini文件导入的接口; 80 | * V 1.0.5.0 增加应用程序调用配置库的方法; 81 | -------------------------------------------------------------------------------- /NConfig/example/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /NConfig/example/example.pro: -------------------------------------------------------------------------------- 1 | QT += sql 2 | QT -= gui 3 | 4 | CONFIG += c++11 5 | 6 | TARGET = NConfig_example 7 | CONFIG += console 8 | CONFIG -= app_bundle 9 | 10 | TEMPLATE = app 11 | 12 | SOURCES += main.cpp 13 | 14 | 15 | win32{ 16 | DESTDIR = $$PWD/../../bin/ 17 | MOC_DIR = $$PWD/build_/moc 18 | RCC_DIR = $$PWD/build_/rcc 19 | OBJECTS_DIR = $$PWD/build_/obj 20 | } 21 | 22 | # import dll file 23 | include($$PWD/../NConfig/NConfig_inc.pri) 24 | -------------------------------------------------------------------------------- /NConfig/example/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "nconfig.h" 4 | #include 5 | #include 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | QCoreApplication a(argc, argv); 10 | qDebug() <<"**************************************测试用例开始*************************************" << "\r\n"; 11 | 12 | qDebug() <<"--------------------------------------初始化测试开始-----------------------------------"; 13 | NConfig NConfigInstance; 14 | qDebug() << "配置文件是否打开成功:" << NConfigInstance.installAndInit(QCoreApplication::applicationDirPath() + "/app.conf", "daodaoliang", false); 15 | qDebug() <<"--------------------------------------初始化测试完毕-----------------------------------" << "\r\n"; 16 | 17 | qDebug() <<"--------------------------------------添加配置测试开始---------------------------------"; 18 | qDebug() << "增加测试配置项开始"; 19 | NConfigInstance.addConfigItem("localmysql","host","127.0.0.1"); 20 | NConfigInstance.addConfigItem("localmysql","user","root"); 21 | NConfigInstance.addConfigItem("localmysql","password","Fuck1024"); 22 | NConfigInstance.addConfigItem("localmysql","db","caterwisdom"); 23 | NConfigInstance.addConfigItem("localmysql","port","3306"); 24 | NConfigInstance.addConfigItem("app","version","1.0.1.0"); 25 | NConfigInstance.addConfigItem("app","name","cater-center"); 26 | qDebug() << "增加测试配置项结束"; 27 | qDebug() <<"--------------------------------------添加配置测试完毕---------------------------------" << "\r\n"; 28 | 29 | qDebug() <<"--------------------------------------读取配置测试开始---------------------------------"; 30 | qDebug() << "读取测试配置项"; 31 | QString tempValue; 32 | qDebug() << "是否读取成功:" << NConfigInstance.getConfigItem("localmysql","user",tempValue); 33 | qDebug() << "读取到的值:" << tempValue; 34 | qDebug() << "是否读取成功:" << NConfigInstance.getConfigItem("localmysql","password",tempValue); 35 | qDebug() << "是否读取成功:" << NConfigInstance.getConfigItem("localmysql","errorKey",tempValue); 36 | qDebug() <<"--------------------------------------读取配置测试完毕---------------------------------" << "\r\n"; 37 | 38 | qDebug() <<"--------------------------------------导出配置测试开始---------------------------------"; 39 | qDebug() << "是否导出成功:" << NConfigInstance.exportConfigToFile(); 40 | qDebug() <<"--------------------------------------导出配置测试完毕---------------------------------" << "\r\n"; 41 | 42 | qDebug() <<"--------------------------------------清空配置测试开始---------------------------------"; 43 | qDebug() << "测试清空配置库"; 44 | qDebug()<< "是否清理成功:" << NConfigInstance.clearConfig(); 45 | qDebug() <<"--------------------------------------清空配置测试完毕---------------------------------" << "\r\n"; 46 | 47 | qDebug() <<"--------------------------------------导入配置测试开始---------------------------------"; 48 | qDebug() << "是否导入成功:" << NConfigInstance.importConfigFromFile(QCoreApplication::applicationDirPath() + "/app.ini"); 49 | qDebug() <<"--------------------------------------导入配置测试完毕---------------------------------" << "\r\n"; 50 | 51 | NConfigInstance.releaseConf(); 52 | qDebug() <<"**************************************测试用例结束************************************"; 53 | 54 | return a.exec(); 55 | } 56 | -------------------------------------------------------------------------------- /NConfig/scripts/clear.bat: -------------------------------------------------------------------------------- 1 | rmdir /S /Q %~dp0..\NConfig\release 2 | rmdir /S /Q %~dp0..\NConfig\debug 3 | rmdir /S /Q %~dp0..\NConfig\build_ 4 | rmdir /S /Q %~dp0..\example\build_ 5 | rmdir /S /Q %~dp0..\example\release 6 | rmdir /S /Q %~dp0..\example\debug 7 | rmdir /S /Q %~dp0..\AppCustomConfig\build_ 8 | rmdir /S /Q %~dp0..\AppCustomConfig\release 9 | rmdir /S /Q %~dp0..\AppCustomConfig\debug 10 | rmdir /S /Q %~dp0..\bin 11 | del /Q %~dp0..\Makefile* 12 | del /Q %~dp0..\NConfig\Makefile* 13 | del /Q %~dp0..\example\Makefile* 14 | del /Q %~dp0..\AppCustomConfig\Makefile* 15 | -------------------------------------------------------------------------------- /NConfig/scripts/sqlite3shell.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daodaoliang/NAuth/2ae4a54e2e9e81ced511326123b27c2e40500a65/NConfig/scripts/sqlite3shell.exe -------------------------------------------------------------------------------- /NEncryptionKit/.gitignore: -------------------------------------------------------------------------------- 1 | /NEncryptionKit.pro.user 2 | /NEncryptionKit/NEncryptionKit.pro.user 3 | /.qmake.stash 4 | -------------------------------------------------------------------------------- /NEncryptionKit/Example/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /NEncryptionKit/Example/Example.pro: -------------------------------------------------------------------------------- 1 | QT += core 2 | QT -= gui 3 | 4 | CONFIG += c++11 5 | 6 | TARGET = NEncryptionKit_Example 7 | CONFIG += console 8 | CONFIG -= app_bundle 9 | 10 | TEMPLATE = app 11 | 12 | SOURCES += main.cpp 13 | 14 | win32{ 15 | DESTDIR = $$PWD/../../bin 16 | MOC_DIR = $$PWD/build_/moc 17 | RCC_DIR = $$PWD/build_/rcc 18 | OBJECTS_DIR = $$PWD/build_/obj 19 | } 20 | 21 | # import dll file 22 | include($$PWD/../NEncryptionKit/NEncryptionKit_inc.pri) 23 | -------------------------------------------------------------------------------- /NEncryptionKit/Example/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "nencryptionkit.h" 5 | int main(int argc, char *argv[]) 6 | { 7 | QCoreApplication a(argc, argv); 8 | qDebug()<<"******************************************测试用例开始*****************************************************"; 9 | NEncryptionKit test_instance; 10 | qDebug()<<"----------------------------MD5测试开始----------------------------"; 11 | QString test_case_001("I am daodaoliang"); 12 | QString test_encry_str = test_instance.getMD5Hash(test_case_001); 13 | qDebug()<<"MD5前的字符串:" << test_case_001; 14 | qDebug()<<"MD5后的字符串:" << test_encry_str; 15 | qDebug()<<"----------------------------MD5测试结束----------------------------"; 16 | 17 | qDebug()<<"----------------------------SHA测试开始----------------------------"; 18 | QString test_case_002("I am nami"); 19 | QString test_encry_str_002 = test_instance.getSHAHash(test_case_002); 20 | qDebug()<<"SHA前的字符串:" << test_case_002; 21 | qDebug()<<"SHA后的字符串:" << test_encry_str_002; 22 | qDebug()<<"----------------------------SHA测试结束----------------------------"; 23 | 24 | qDebug()<<"----------------------------Kaiser测试开始-------------------------"; 25 | QString test_case_003("I am wangxiaowei"); 26 | qint8 test_case_key(7); 27 | QString test_encry_str_003 = test_case_003; 28 | bool ret = test_instance.getByKaiser(test_case_003, test_case_key); 29 | qDebug()<<"加密是否成功:"< 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | using namespace std; 21 | 22 | /** 23 | * @brief The NEncryptionKit class 24 | * 数据的加解密、签名、校验等组件 25 | */ 26 | class NENCRYPTIONKITSHARED_EXPORT NEncryptionKit : public QObject 27 | { 28 | Q_OBJECT 29 | public: 30 | explicit NEncryptionKit(QObject *parent = 0); 31 | 32 | public: 33 | 34 | /** 35 | * @brief getMD5Hash 获取字符串的MD5散列值 36 | * @param param_data 待转换的数据 37 | * @return 转换后的散列值 38 | */ 39 | QString getMD5Hash(const QString ¶m_data); 40 | 41 | /** 42 | * @brief getSHAHash 获取字符串的SHA散列值 43 | * @param param_data 待转换的数据 44 | * @return 转换后的散列值 45 | */ 46 | QString getSHAHash(const QString ¶m_data); 47 | 48 | /** 49 | * @brief getByKaiser 获取采用凯撒机密的字符串 50 | * @param param_data 待加密的数据 51 | * @param param_key 加密秘钥 52 | * @return 是否加密成功 53 | */ 54 | bool getByKaiser(QString ¶m_data,qint8 param_key); 55 | 56 | /** 57 | * @brief createRSAKey 产生RSA密钥 58 | * @param paramPubKeyFile 公钥文件存储地址 59 | * @param paramPriKeyFile 私钥文件存储地址 60 | * @return 61 | */ 62 | bool createRSAKey(QString paramPubKeyFile, QString paramPriKeyFile); 63 | 64 | /** 65 | * @brief getEncryptByRSA 用RSA公钥进行加密 66 | * @param paramDataSource 需要被加密的数据 67 | * @param paramDataDest 加密后的数据 68 | * @param paramPubKey 公钥文件路径 69 | * @return 70 | */ 71 | bool getEncryptByRSA(QString paramDataSource, QByteArray ¶mDataDest, QString paramPubKeyPath); 72 | 73 | /** 74 | * @brief decryptionByRSA 用RSA私钥进行解密 75 | * @param paramDataSource 需要进行解密的数据 76 | * @param paramDataDest 解密后的数据 77 | * @param paramPriKeyPath 私钥文件路劲 78 | * @return 79 | */ 80 | bool decryptionByRSA(QByteArray paramDataSource, QString ¶mDataDest, QString paramPriKeyPath); 81 | 82 | /** 83 | * @brief getEncryByAES 用AES进行加密 84 | * @param paramSource 待加密字符串 85 | * @param paramDest 加密后的字符串 86 | * @return 是否解密成功 87 | */ 88 | bool getEncryByAES(QString paramSource, string ¶mDest); 89 | 90 | /** 91 | * @brief decryptByAES 用AES进行解密 92 | * @param paramSource 待解密字符串 93 | * @param paramDest 解密后的字符串 94 | * @return 是否解密成功 95 | */ 96 | bool decryptByAES(string paramSource, QString ¶mDest); 97 | 98 | /** 99 | * @brief setPassword 设置加密的密钥 100 | * @param param_password 密钥 101 | */ 102 | void setPassword(QString param_password); 103 | 104 | /** 105 | * @brief setSalt 设置加密盐 106 | * @param param_salt 107 | */ 108 | void setSalt(QString param_salt); 109 | 110 | private: 111 | 112 | /** 113 | * @brief mPassword 密码 114 | */ 115 | QString mPassword; 116 | }; 117 | 118 | #endif // NENCRYPTIONKIT_H 119 | 120 | -------------------------------------------------------------------------------- /NEncryptionKit/NEncryptionKit/inc/nencryptionkit_global.h: -------------------------------------------------------------------------------- 1 | #ifndef NENCRYPTIONKIT_GLOBAL_H 2 | #define NENCRYPTIONKIT_GLOBAL_H 3 | 4 | #include 5 | 6 | #if defined(NENCRYPTIONKIT_LIBRARY) 7 | # define NENCRYPTIONKITSHARED_EXPORT Q_DECL_EXPORT 8 | #else 9 | # define NENCRYPTIONKITSHARED_EXPORT Q_DECL_IMPORT 10 | #endif 11 | 12 | #endif // NENCRYPTIONKIT_GLOBAL_H 13 | -------------------------------------------------------------------------------- /NEncryptionKit/NEncryptionKit/src/nencryptionkit.cpp: -------------------------------------------------------------------------------- 1 | #include "nencryptionkit.h" 2 | #include 3 | #include 4 | #include 5 | #include "botan.h" 6 | 7 | using namespace Botan; 8 | 9 | NEncryptionKit::NEncryptionKit(QObject *parent):QObject(parent) 10 | { 11 | //设置默认密码 12 | mPassword = getSHAHash("iamdaodaoliang(nami_salt)"); 13 | } 14 | 15 | QString NEncryptionKit::getMD5Hash(const QString ¶m_data) 16 | { 17 | return QString(QCryptographicHash::hash(param_data.toLocal8Bit(),QCryptographicHash::Md5).toHex()); 18 | } 19 | 20 | QString NEncryptionKit::getSHAHash(const QString ¶m_data) 21 | { 22 | return QString(QCryptographicHash::hash(param_data.toLocal8Bit(),QCryptographicHash::Sha3_512).toHex()); 23 | } 24 | 25 | bool NEncryptionKit::getByKaiser(QString ¶m_data, qint8 param_key) 26 | { 27 | if(param_data.isEmpty()){ 28 | return false; 29 | } 30 | for(int temp_index = 0; temp_index != param_data.size();++temp_index){ 31 | char tempChar = param_data.at(temp_index).toLatin1(); 32 | int tempValue = 0; 33 | if(tempChar >= 0x30 && tempChar <= 0x39){ 34 | tempValue = (tempChar - '0' + param_key); 35 | tempChar = (tempValue >= 0) ? (tempValue % 10 + '0') : (tempValue % 10 + 0x3a); 36 | } else if (tempChar >= 0x41 && tempChar <= 0x5a) { 37 | tempValue = (tempChar - 'A' + param_key); 38 | tempChar = (tempValue >= 0) ? (tempValue % 26 + 'A') : (tempValue % 26 + 0x5b); 39 | } else if (tempChar >= 0x61 && tempChar <= 0x7a) { 40 | tempValue = (tempChar - 'a' + param_key); 41 | tempChar = (tempValue >= 0) ? (tempValue % 26 + 'a') : (tempValue % 26 + 0x7b); 42 | } 43 | param_data[temp_index] = QChar(tempChar); 44 | } 45 | return true; 46 | } 47 | 48 | bool NEncryptionKit::createRSAKey(QString paramPubKeyFile, QString paramPriKeyFile) 49 | { 50 | try{ 51 | Botan::AutoSeeded_RNG rng; 52 | Botan::RSA_PrivateKey privateKey(rng, 1024); 53 | QString temp_pri(Botan::X509::PEM_encode(privateKey).c_str()); 54 | QString temp_pub(Botan::PKCS8::PEM_encode(privateKey).c_str()); 55 | 56 | if(QFile::exists(paramPriKeyFile)){ 57 | QFile::remove(paramPriKeyFile); 58 | } 59 | QFile innerFile(paramPriKeyFile); 60 | if(innerFile.open(QFile::ReadWrite)){ 61 | innerFile.write(temp_pub.toLocal8Bit()); 62 | innerFile.flush(); 63 | } 64 | innerFile.close(); 65 | 66 | if(QFile::exists(paramPubKeyFile)){ 67 | QFile::remove(paramPubKeyFile); 68 | } 69 | QFile innerFile_1(paramPubKeyFile); 70 | if(innerFile_1.open(QFile::ReadWrite)){ 71 | innerFile_1.write(temp_pri.toLocal8Bit()); 72 | innerFile_1.flush(); 73 | } 74 | innerFile_1.close(); 75 | return true; 76 | } catch(Exception &e){ 77 | qDebug()< en = enc.encrypt(paramDataSource.toStdString().c_str(), paramDataSource.toStdString().size(), rng); 89 | QByteArray temp; 90 | for (uint i = 0; i < en.size(); i++) 91 | { 92 | temp[i] = en[i]; 93 | } 94 | paramDataDest = temp; 95 | return true; 96 | } catch(Exception &e){ 97 | qDebug()< temp_mem; 109 | temp_mem.resize(paramDataSource.size()); 110 | for(uint i = 0; i< paramDataSource.size(); ++i){ 111 | temp_mem[i] = paramDataSource[i]; 112 | } 113 | SecureVector re = dec.decrypt(temp_mem,temp_mem.size()); 114 | QByteArray temp; 115 | for (uint i = 0; i < re.size(); i++) 116 | { 117 | temp[i] = re[i]; 118 | } 119 | paramDataDest = QString(temp); 120 | return true; 121 | } catch(Exception &e){ 122 | qDebug()<process(mPassword.toStdString()); 133 | SecureVector raw_iv = hash->process('0'+ mPassword.toStdString()); 134 | InitializationVector iv(raw_iv, 16); 135 | Pipe pipe(get_cipher("AES-128/CBC/PKCS7", key, iv, ENCRYPTION)); 136 | pipe.process_msg(paramSource.toStdString()); 137 | string output=pipe.read_all_as_string(); 138 | paramDest = output; 139 | return true; 140 | } 141 | catch(Exception &e) 142 | { 143 | qDebug()<process(mPassword.toStdString()); 154 | SecureVector raw_iv = hash->process('0'+ mPassword.toStdString()); 155 | InitializationVector iv(raw_iv, 16); 156 | Pipe pipe(get_cipher("AES-128/CBC/PKCS7", key, iv, DECRYPTION)); 157 | pipe.process_msg(paramSource); 158 | string output=pipe.read_all_as_string(); 159 | paramDest = QString::fromStdString(output); 160 | return true; 161 | } 162 | catch(Exception &e) 163 | { 164 | qDebug()<