1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| void Samples::Hash(const std::string host, int PORT) { Announce("Hash");
std::cout << "Starting server..." << std::endl;
int serverSocket = socket(AF_INET, SOCK_STREAM, 0); if (serverSocket == -1) { std::cerr << "Failed to create server socket." << std::endl; return; } sockaddr_in serverAddress{}; serverAddress.sin_family = AF_INET; serverAddress.sin_addr.s_addr = INADDR_ANY; serverAddress.sin_port = htons(PORT);
if (bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1) { std::cerr << "Failed to bind server socket." << std::endl; return; }
if (listen(serverSocket, 5) == -1) { std::cerr << "Failed to listen on server socket." << std::endl; return; }
while (true) { std::cout << "Waiting for connection..." << std::endl;
sockaddr_in clientAddress{}; socklen_t clientAddressLength = sizeof(clientAddress); int clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddress, &clientAddressLength); if (clientSocket == -1) { std::cerr << "Failed to accept client connection." << std::endl; continue; } std::cout << "Connected to client successfully!" << std::endl;
char data[23] = { "Send text from client:" }; SSIZE_T bytesSentabc = send(clientSocket, data, 22, 0); int count = 0; int index = 0; const int bufferSize = 1024; char buffer[bufferSize]; char bufferRe[bufferSize]; memset(buffer, 0, bufferSize); memset(bufferRe, 0, bufferSize); while (1) { SSIZE_T bytesRead = recv(clientSocket, bufferRe, bufferSize - 1, 0); if (bytesRead == -1) { std::cerr << "Failed to receive data from client." << std::endl; break; } else if (bytesRead == 0) { break; } else { if (bufferRe[bytesRead - 1] == '\n' || bufferRe[bytesRead - 1] == '\r') { index = 1; break; } if (bufferRe[bytesRead - 1] != '\n' || bufferRe[bytesRead - 1] != '\r') { buffer[count] = bufferRe[bytesRead - 1]; count++; } } memset(bufferRe, 0, bufferSize); if (index == 1) { break; } } char dataHash[23] = { "Send hash from client:" }; SSIZE_T bytesSentHash = send(clientSocket, dataHash, 22, 0); int countHash = 0; int indexHash = 0; char bufferHash[bufferSize]; char bufferReHash[bufferSize]; memset(bufferHash, 0, bufferSize); memset(bufferReHash, 0, bufferSize); while (1) { SSIZE_T bytesReadHash = recv(clientSocket, bufferReHash, bufferSize - 1, 0); if (bytesReadHash == -1) { std::cerr << "Failed to receive data from client." << std::endl; break; } else if (bytesReadHash == 0) { break; } else { if (bufferReHash[bytesReadHash - 1] == '\n' || bufferReHash[bytesReadHash - 1] == '\r') { indexHash = 1; break; } if (bufferReHash[bytesReadHash - 1] != '\n' || bufferReHash[bytesReadHash - 1] != '\r') { bufferHash[countHash] = bufferReHash[bytesReadHash - 1]; countHash++; } } memset(bufferReHash, 0, bufferSize); if (indexHash == 1) { break; } } unsigned char hash[SHA256_DIGEST_LENGTH + 1];
SHA256_CTX context; SHA256_Init(&context); SHA256_Update(&context, buffer, strlen(buffer)); SHA256_Final(hash, &context); hash[SHA256_DIGEST_LENGTH] = '\0';
std::stringstream ss; for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) { ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i]; } std::string hashString = ss.str();
std::cout << "Received data:" << buffer << std::endl; std::cout << "Received hash:" << bufferHash << std::endl; std::cout << "Expected Hash:" << hashString << std::endl; if (bufferHash == hashString) { std::cout << "Messages have not been tampered with and this system is safe!" << std::endl; char repeat[22] = {"Get hash from Server:"}; char repeatSafe[23] = { "So our system is safe!" }; char huiche[1] = { '\n' }; SSIZE_T bytesSenta = send(clientSocket, repeat, 21, 0); SSIZE_T bytesSent = send(clientSocket, hashString.c_str(), hashString.length(), 0); SSIZE_T bytesSenhui = send(clientSocket, huiche, 1, 0); SSIZE_T bytesSentab = send(clientSocket, repeatSafe, 22, 0); if (bytesSent == -1) { std::cerr << "Failed to send hash to client." << std::endl; } } else { std::cout << "Messages have been tampered with and this system is insecure!" << std::endl; char repeat[22] = {"Get hash from Server:"}; char repeatSafe[27] = { "So our system is insecure!" }; char huiche[1] = { '\n' }; SSIZE_T bytesSenta = send(clientSocket, repeat, 21, 0); SSIZE_T bytesSent = send(clientSocket, hashString.c_str(), hashString.length(), 0); SSIZE_T bytesSenhui = send(clientSocket, huiche, 1, 0); SSIZE_T bytesSentab = send(clientSocket, repeatSafe, 26, 0); if (bytesSent == -1) { std::cerr << "Failed to send hash to client." << std::endl; } } } }
|