#include "SHSSDK.h" #ifdef WIN32 // include neon config.h #include #endif //WIN32 #include "main.h" #include "auth.h" #include "mcrypt.h" #include "ne_string.h" #define KEY_LENGTH 24 static const int g_pnKeyPos[] = { 41, 8467, 6334, 6500, 9169, 5724, 1478, 9358, 6962, 4464, 5705, 8145, 3281, 6827, 9961, 491, 2995, 1942, 4827, 5436, 2391, 10004, 3902, 153 }; const char *sh_get_auth_string(const char *userId, const char *password, const char *serviceId, const char *authKey, const char *authFile, time_t expire) { char *pszResult = NULL; char szMid[128]={0}; char *pszKey = NULL, szIV[KEY_LENGTH]; char szMD5[128], szPassword[128], szAuthString[256]; int i, nAuthStringLen, nSkipLen; char* pData=NULL; MCRYPT mcrypt = NULL; pszKey = (char*)_sh_GetKey(authFile); if (!pszKey) { return NULL; } strcpy(szMD5, password); md5_buffer(szMD5, szPassword); sprintf(szMD5, "%s@%s%s%010d", serviceId, userId, szPassword, (int)expire); md5_buffer(szMD5, szPassword); sprintf(szAuthString, "%s@%s:%s@%s-%s-%010d", serviceId, userId, serviceId, userId, szPassword, (int)expire); mcrypt = mcrypt_module_open(MCRYPT_BLOWFISH, NULL, MCRYPT_CBC, NULL); if (mcrypt == MCRYPT_FAILED) goto EXIT_FUNCTION; memset(szIV, NULL, KEY_LENGTH); if (KEY_LENGTH <= strlen(authKey)) memcpy(szIV, authKey, KEY_LENGTH); else strcpy(szIV, authKey); if (mcrypt_generic_init(mcrypt, pszKey, KEY_LENGTH, szIV) < 0) goto EXIT_FUNCTION; nSkipLen = (int)strlen(serviceId) + (int)strlen(userId) + 2; nAuthStringLen = (int)strlen(szAuthString); if (mcrypt_generic(mcrypt, szAuthString + nSkipLen, nAuthStringLen - nSkipLen) < 0) goto EXIT_FUNCTION; mcrypt_generic_deinit(mcrypt); pszResult = ne_base64((const unsigned char*)szAuthString, nAuthStringLen); #ifdef _NEWAUTH_WHERE_USE__ pData = ne_base64((const unsigned char*)szAuthString, nAuthStringLen); md5_buffer(pData,szMid); pszResult = malloc(strlen(pData) + strlen(szMid)+2); strcpy(pszResult,pData); strcat(pszResult,":"); strcat(pszResult,szMid); if (pData) free(pData); #endif // _NEWAUTH_WHERE_USE__ EXIT_FUNCTION: i = GetLastError(); if (pszKey) free(pszKey); if (mcrypt) mcrypt_module_close(mcrypt); SetLastError(i); return pszResult; } const char *sh_get_auth_sp_string(const char *userId, const char *password) { char *pszResult = NULL; char szAuthString[256] = {0}; char szMD5[1024] = {0}; //char* pszBase64 = NULL; md5_buffer(password, szMD5); sprintf(szAuthString, "%s:%s", userId, szMD5); pszResult = ne_base64((const unsigned char*)szAuthString, strlen(szAuthString)); return pszResult; } #define DECRYPT_KEY "I'm your father" #define DECRYPT_BIT 24 const char *sh_decrypt(const char *encrypted, int acceptGap) { char *pszResult = NULL; char szKey[DECRYPT_BIT], szIV[DECRYPT_BIT]; char *pszEncrypted = NULL, *pszDecrypted = NULL; int i, nEncryptedSize, nDecryptedSize; char szCreatedTime[15], szCurrentTime[15]; time_t tmNow; struct tm *ptmGMT; struct tm stm; MCRYPT mcrypt = NULL; pszEncrypted = strdup(encrypted); if (!pszEncrypted) return NULL; memset(szKey, NULL, DECRYPT_BIT); strcpy(szKey, DECRYPT_KEY); nEncryptedSize = (int)strlen(pszEncrypted) - DECRYPT_BIT; memcpy(szIV, pszEncrypted + nEncryptedSize, DECRYPT_BIT); pszEncrypted[nEncryptedSize] = (char)NULL; nDecryptedSize = (int)ne_unbase64(pszEncrypted, (unsigned char **)&pszDecrypted); pszDecrypted[nDecryptedSize] = (char)NULL; // º¹È£È­ mcrypt = mcrypt_module_open(MCRYPT_BLOWFISH, NULL, MCRYPT_CBC, NULL); if (mcrypt == MCRYPT_FAILED) goto EXIT_FUNCTION; if (mcrypt_generic_init(mcrypt, szKey, DECRYPT_BIT, szIV) < 0) goto EXIT_FUNCTION; if (mdecrypt_generic(mcrypt, pszDecrypted, nDecryptedSize) < 0) goto EXIT_FUNCTION; mcrypt_generic_deinit(mcrypt); // ½Ã°£ ºñ±³ nDecryptedSize -= 14; memcpy(szCreatedTime, pszDecrypted + nDecryptedSize, 14); pszDecrypted[nDecryptedSize] = (char)NULL; tmNow = (time_t)(get_time_from_sntp() - acceptGap); ptmGMT = gmtime(&tmNow); sprintf(szCurrentTime, "%04d%02d%02d%02d%02d%02d", ptmGMT->tm_year + 1900, ptmGMT->tm_mon, ptmGMT->tm_wday, ptmGMT->tm_hour, ptmGMT->tm_min, ptmGMT->tm_sec); if (strncmp(szCreatedTime, szCurrentTime, 14) < 0) goto EXIT_FUNCTION; tmNow = (time_t)(get_time_from_sntp() + acceptGap); ptmGMT = gmtime(&tmNow); sprintf(szCurrentTime, "%04d%02d%02d%02d%02d%02d", ptmGMT->tm_year + 1900, ptmGMT->tm_mon, ptmGMT->tm_wday, ptmGMT->tm_hour, ptmGMT->tm_min, ptmGMT->tm_sec); if (strncmp(szCreatedTime, szCurrentTime, 14) > 0) goto EXIT_FUNCTION; pszResult = strdup(pszDecrypted); EXIT_FUNCTION: i = GetLastError(); if (pszEncrypted) free(pszEncrypted); if (pszDecrypted) free(pszDecrypted); if (mcrypt) mcrypt_module_close(mcrypt); SetLastError(i); return pszResult; } static const char* _sh_GetKey(const char* pszAuthFile) { FILE *pf = NULL; char *pszKey = NULL; int i = 0; pf = fopen(pszAuthFile, "r"); if (!pf) return NULL; pszKey = (char*)malloc(KEY_LENGTH + 1); for (i = 0; i < KEY_LENGTH ;i++) { if (fseek(pf, g_pnKeyPos[i], 0) != 0) { free(pszKey); return NULL; } fread(pszKey + i, 1, 1, pf); } fclose(pf); pszKey[KEY_LENGTH] = (char)NULL; return pszKey; }