fork(1) download
  1. #include <iostream>
  2.  
  3. bool password_equals(const std::string &s1,const std::string &s2){
  4. // originally i wanted this as a 3rd parameter, but then it could easily
  5. // be misused by the caller, and leak the password length
  6. // (if the caller did max_length = MAX(s1.length(),s2.length()) on the hacker's input)
  7. const int max_length=500;
  8. // TODO: should probably throw a runtime_error or something if either input.length() > max_length
  9. char s1c[max_length]={0};
  10. char s2c[max_length]={0};
  11. s1.copy(&s1c[0],max_length);
  12. s2.copy(&s2c[0],max_length);
  13. int result=0;
  14. for (int i = 0; i < max_length; ++i) {
  15. result |= s1c[i] ^ s2c[i];
  16. }
  17. return (result==0);
  18. }
  19.  
  20. int main() {
  21. // your code goes here
  22. std::cout << password_equals("yup","yup") << " - " << password_equals("yup","nope");
  23. return 0;
  24. }
Success #stdin #stdout 0s 4324KB
stdin
Standard input is empty
stdout
1 - 0