#define WIN32_LEAN_AND_MEAN #define _CRT_SECURE_NO_WARNINGS #include <windows.h> #include <wincrypt.h> #include <stdio.h> #include <stdlib.h> char* GenericHash(unsigned int AlgId,char* Buffer, int lBuffer) { HCRYPTPROV hProv; HCRYPTHASH hHash; BYTE* Hash; DWORD lHash, len; char* sHash; unsigned char i; if (CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) { if (CryptCreateHash(hProv,AlgId,0,0,&hHash)) { if (CryptHashData(hHash,(BYTE*)Buffer,lBuffer,0)) { if (CryptGetHashParam(hHash,HP_HASHSIZE,(BYTE*)&lHash,&len,0)) { Hash = malloc(lHash); if (!Hash) return NULL; if (!CryptGetHashParam(hHash,HP_HASHVAL,Hash,&lHash,0)) { free(Hash); return NULL; } } } else return NULL; } else return NULL; } else return NULL; CryptDestroyHash(hHash); CryptReleaseContext(hProv,0); sHash = malloc(lHash*2+1); memset(sHash,0,lHash*2+1); for (i = 0; i <= lHash-1; i++) sprintf(sHash+(i*2),"%02X",Hash[i]); free(Hash); return sHash; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question/*________________________________________________________________________ */ #include #include <string> /*________________________________________________________________________ */ template < int p1 '\0' , int p2 '\0' , int p3 = '\0' , int p4 = '\0' > struct password { static int const c_key = 0x80808080 ; static int const c_block_nb = 4 ; bool check( const std::string & s ) { static int const v[] = { c_key | p1 , c_key | p2 , c_key | p3 , c_key | p4 } ; if ( s.size() > c_block_nb * sizeof( int ) ) return false ; for ( size_t i = 0 ; i < c_block_nb ; ++i ) { int n = 0 ; for ( size_t j = 0 ; j < c_block_nb ; ++j ) { if ( i * c_block_nb + j < s.size() ) { n <<= 8 ; n |= s[ i * c_block_nb + j ] ; } } n |= c_key ; if ( v[ i ] != n ) return false ; } return true ; } }; /*________________________________________________________________________ */ int main() { password<'supe','r_pa','sswo','rd'> pwd ; std::string in ; while ( std::cout << "password ? " && std::cin >> in ) std::cout << "good password : " << std::boolalpha << pwd.check( in ) << std::endl ; } /*________________________________________________________________________ */