1. //+CXXFLAGS:=-O2 -g -Wall -Wextra -Wshadow -std=c++11
  2. #include <string>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <openssl/sha.h>
  8. using namespace std;
  9. std::string calculate_flag(
  10. std::string &part1,
  11. int64_t part2,
  12. std::string &part4,
  13. uint64_t factor1,
  14. uint64_t factor2)
  15. {
  16. std::transform(part1.begin(), part1.end(), part1.begin(), ::tolower);
  17. std::transform(part4.begin(), part4.end(), part4.begin(), ::tolower);
  18. SHA_CTX ctx;
  19. SHA1_Init(&ctx);
  20. unsigned int mod = factor1 % factor2;
  21. for (unsigned int i = 0; i < mod; i+=2)
  22. {
  23. SHA1_Update(&ctx,
  24. reinterpret_cast<const unsigned char *>(part1.c_str()),
  25. part1.size());
  26. }
  27. while (part2-- > 0)
  28. {
  29. SHA1_Update(&ctx,
  30. reinterpret_cast<const unsigned char *>(part4.c_str()),
  31. part1.size());
  32. }
  33. unsigned char *hash = new unsigned char[SHA_DIGEST_LENGTH];
  34. SHA1_Final(hash, &ctx);
  35. std::string rv;
  36. for (unsigned int i = 0; i < SHA_DIGEST_LENGTH; i++)
  37. {
  38. char *buf;
  39. asprintf(&buf, "%02x", hash[i]);
  40. rv += buf;
  41. free(buf);
  42. }
  43. return rv;
  44. }
  45. int main(int argc, char **argv)
  46. {
  47. (void)argc; (void)argv; //unused
  48. string part1;
  49. cout << "Part1: Enter flag:" << endl;
  50. cin >> part1;
  51. int64_t part2;
  52. cout << "Part2: Input 51337:" << endl;
  53. cin >> part2;
  54. std::string part3;
  55. cout << "Part3: Watch this: https://www.youtube.com/watch?v=PBwAxmrE194" << endl;
  56. cin >> part3;
  57. std::string part4;
  58. cout << "Part4: C.R.E.A.M. Get da _____: " << endl;
  59. cin >> part4;
  60. uint64_t first, second;
  61. cout << "Part5: Input the two prime factors of the number 270031727027." << endl;
  62. cin >> first;
  63. cin >> second;
  64. uint64_t factor1, factor2;
  65. if (first < second)
  66. {
  67. factor1 = first;
  68. factor2 = second;
  69. }
  70. else
  71. {
  72. factor1 = second;
  73. factor2 = first;
  74. }
  75. std::string flag = calculate_flag(part1, part2, part4, factor1, factor2);
  76. cout << "flag{";
  77. cout << flag;
  78. cout << "}" << endl;
  79. return 0;
  80. }

CSAW2015 - for400 sharp.cpp