1

so, for pset2 crack task i've generate a password, then encrypt this password and a salt to some result by

result = crypt(pass, salt);

After i want to compare result with hash, so i implemented a bool function, that compares them, and if it is equal - print password and return 'true', otherwise - return 'false'

bool cmp(string res, string h, string p)
{
    if (strcmp(res, h)==0) 
    {
        printf("%s\n", p);
        return true;
    }
    return false;
}

and then use it as:

if (cmp(result, hash, pass)) 
   return 0;

to end program in case if result(crypted password by salt) equals to hash, as requested in task.
Question is - is it possible to exit by return 0; right from bool function in case if 'true', so then use of the function can looks like just

cmp(result, hash, pass);

perhaps?Can such solution be done?
Thanks.

1

This is a really interesting question. Is it possible? Yes. Is it desirable? In my humble opinion, it's something to be avoided.

Yes, it's possible. There is a function for this: exit(<number>) where number is your choice of an integer. It will immediately terminate the program and will release and clean up as much as it can, like file pointers, etc. Based on my quick google, there are some things it won't clean up, and there are debates on what will and will not be cleaned up. It also depends on whether you are in c or c++ and who's version. Here's a link to an interesting discussion on the topic. http://www.cplusplus.com/forum/beginner/4589/

Is it a good practice? IMHO, not really. A better practice is to walk the function calls back to main and to exit from there, preferably using a return statement. This will cause a clean exit from the program. Yes, there may be times where a quick and dirty exit is necessary, but generally, the graceful and clean return is the better practice.

This question requires more information and research, so I'll leave it to you to explore return, exit, and "exiting a program from a function call" at your leisure. These google searches should produce a lot more info. It's a broader discussion.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Also, I invite other comments, opinions and info from everyone on this one. If you have supporting or dissenting opinion or comments, please post. ;-) – Cliff B Oct 30 '17 at 22:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .