/****************************************************************************
 Function
     randomizePasswords
 Parameters
     void
 Returns
     void
 Description
     Randomly generates four passwords and assigns one of the four passwords 
     to be the correct password
 Notes
****************************************************************************/
void randomizePasswords(void) {
	srand(ADC0_InSeq3());
	generateRandomPassword(password1);
	generateRandomPassword(password2);
	generateRandomPassword(password3);
	generateRandomPassword(password4);
	// randomly selects which of the passwords is the correct one
	correctPassword = rand() % NUM_PASSWORDS + 1;
}
bool ClientConfiguration::toFile(QString const& filename) const {
	QString backupString;
	QString backupPassword = generateRandomPassword(20);
	try {
		backupString = toBackup(backupPassword);
	} catch (InternalErrorException& iex) {
		LOGGER()->critical("Failed to create a Backup from Identity: {}", iex.what());
		return false;
	}

	return toFile(filename, backupString, backupPassword);
}
示例#3
0
int main(int argc, char*argv[]){
	int sock;
	int n; // number of chars
	struct sockaddr_in serverAddr;
	struct sockaddr_in clientAddr;
	unsigned int clientAddrLength;
	uint16_t  serverPort;
	timesGuessedCorrectly = 0;
	timesGuessed = 0;

	// parse command line inputs
	if(argc < 3 || argc > 4){
		printf("Usage: <port> <number of characters> \n");
		exit(1);
	}
	serverPort = atoi(argv[1]);
	n = atoi(argv[2]);
	if(n > 8){
		printf("Number of characters exceeds maximum size, Rounding to 8\n");
	}
	char password[n+1];
	if(argc == 4){
		strcpy(password,argv[3]);
	}else{
		generateRandomPassword(n,password);
		password[n] = '\0';
	}
	printf("password = %s \n",password);
	// handle signal
	signal(SIGINT,handleInterupt);

	//initialize incoming list
	root = malloc(sizeof(root));
	strcpy(root->IP,"root");
	struct linkedAddress *current;
	current = root;
	current->next = NULL;

	// create and bind sockets
	if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
		err_n_die("Could not open socket\n");
	}

	memset(&serverAddr,0,sizeof(serverAddr));
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
	serverAddr.sin_port = htons(serverPort);

	if(bind(sock,(struct sockaddr *) &serverAddr,sizeof(serverAddr)) < 0){
		err_n_die("Could not bind to socket\n");
	}

	
	int returnCode; 
	for(;;){
		clientAddrLength = sizeof(clientAddr);
		int recvMsgSize,sentMsgSize;
		char buffer[20];
		// recv message from server
		if((recvMsgSize = recvfrom(sock,buffer,20,0,(struct sockaddr*) &clientAddr,
			&clientAddrLength)) < 0) {
			err_n_die("Could not receive message\n");
		} 
		// store IP in linked list
		char IP[INET_ADDRSTRLEN+1];
		inet_ntop(AF_INET, &(clientAddr.sin_addr),IP,INET_ADDRSTRLEN+1);
		if(IP != NULL){
			addToList(IP);
		} else{
			printf("IP address for client can not be resolved\n");
		}
		// if password is correct regenerate new password
		if(strcmp(buffer,password)== 0){
			returnCode = 0;
			generateRandomPassword(n,password);
			password[n] = '\0';
			timesGuessedCorrectly++;			
		}else{
			returnCode = 1;
		}
		timesGuessed++;
		// send return code
		if((sentMsgSize = sendto(sock,&returnCode,20,0,(struct sockaddr*) &clientAddr,
			clientAddrLength)) < 0){
			err_n_die("Could not send message");
		}

	}

}