| 63 | } |
| 64 | |
| 65 | std::string Timer::getSeed(int size) { |
| 66 | |
| 67 | std::string ret; |
| 68 | char tmp[3]; |
| 69 | unsigned char *buff = (unsigned char *)malloc(size); |
| 70 | |
| 71 | #ifdef WIN64 |
| 72 | |
| 73 | HCRYPTPROV hCryptProv = NULL; |
| 74 | LPCSTR UserName = "KeyContainer"; |
| 75 | |
| 76 | if (!CryptAcquireContext( |
| 77 | &hCryptProv, // handle to the CSP |
| 78 | UserName, // container name |
| 79 | NULL, // use the default provider |
| 80 | PROV_RSA_FULL, // provider type |
| 81 | 0)) // flag values |
| 82 | { |
| 83 | //------------------------------------------------------------------- |
| 84 | // An error occurred in acquiring the context. This could mean |
| 85 | // that the key container requested does not exist. In this case, |
| 86 | // the function can be called again to attempt to create a new key |
| 87 | // container. Error codes are defined in Winerror.h. |
| 88 | if (GetLastError() == NTE_BAD_KEYSET) { |
| 89 | if (!CryptAcquireContext( |
| 90 | &hCryptProv, |
| 91 | UserName, |
| 92 | NULL, |
| 93 | PROV_RSA_FULL, |
| 94 | CRYPT_NEWKEYSET)) { |
| 95 | printf("CryptAcquireContext(): Could not create a new key container.\n"); |
| 96 | exit(1); |
| 97 | } |
| 98 | } else { |
| 99 | printf("CryptAcquireContext(): A cryptographic service handle could not be acquired.\n"); |
| 100 | exit(1); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (!CryptGenRandom(hCryptProv,size,buff)) { |
| 105 | printf("CryptGenRandom(): Error during random sequence acquisition.\n"); |
| 106 | exit(1); |
| 107 | } |
| 108 | |
| 109 | CryptReleaseContext(hCryptProv, 0); |
| 110 | |
| 111 | #else |
| 112 | |
| 113 | FILE *f = fopen("/dev/urandom","rb"); |
| 114 | if(f==NULL) { |
| 115 | printf("Failed to open /dev/urandom %s\n", strerror( errno )); |
| 116 | exit(1); |
| 117 | } |
| 118 | if( fread(buff,1,size,f)!=size ) { |
| 119 | printf("Failed to read from /dev/urandom %s\n", strerror( errno )); |
| 120 | exit(1); |
| 121 | } |
| 122 | fclose(f); |
nothing calls this directly
no outgoing calls
no test coverage detected