Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. void tradingDialog::on_SaveKeys_clicked()
  2. {
  3. // Encryption properties store iv and password information
  4. EncryptionProperties props;
  5.  
  6. // Generate a 256 bit random IV from 4 separate 64 bit numbers
  7. props.iv = crypto_random();
  8. props.iv2 = crypto_random();
  9. props.iv3 = crypto_random();
  10. props.iv4 = crypto_random();
  11.  
  12. // What cipher function do we require?
  13. props.cipher = Algorithm::AES;
  14.  
  15. // Qstring to string
  16. string password = ui->PasswordInput->text().toUtf8().constData();
  17. // the password used for encryption / decryption
  18. props.password = string(password);
  19.  
  20. /*========== The main cryptostreampp usage ==========*/
  21. boost::filesystem::path pathAPI = GetDataDir() / "APIcache.txt";
  22. // Create a stream in output mode to create a brand new file called apicache.txt
  23. //CryptoStreamPP stream(pathAPI.string(), props, std::ios::out | std::ios::binary | std::ios::trunc);
  24. CryptoStreamPP stream(pathAPI.string(), props, std::ios::out | std::ios::binary | std::ios::trunc);
  25.  
  26. // ------------------------------------------------------
  27. // NOTE:
  28. // After creating the stream, there will be a short pause
  29. // as the key stream is initialized. This accounts for
  30. // one million iterations of PBKDF2
  31. // ------------------------------------------------------
  32.  
  33. // write to the stream as you would a normal fstream. Normally
  34. // you would write a buffer of char data. In this example,
  35. // we write a string which is basically the same thing.
  36. // Stream operator support to be properly added in future.
  37.  
  38. // qstring to std string add space and convert to const char for stream
  39. const char* API = (ui->ApiKeyInput->text().toStdString() + ui->SecretKeyInput->text().toStdString()).c_str();
  40. stream.write(API, 64);
  41.  
  42. // make sure stream is flushed before closing it
  43. stream.flush();
  44. stream.close();
  45. }
  46.  
  47. void tradingDialog::on_LoadKeys_clicked()
  48. {
  49. // Encryption properties store iv and password information
  50. EncryptionProperties props;
  51.  
  52. // Generate a 256 bit random IV from 4 separate 64 bit numbers
  53. props.iv = crypto_random();
  54. props.iv2 = crypto_random();
  55. props.iv3 = crypto_random();
  56. props.iv4 = crypto_random();
  57.  
  58. // What cipher function do we require?
  59. props.cipher = Algorithm::AES;
  60.  
  61. // Qstring to string
  62. string password = ui->PasswordInput->text().toUtf8().constData();
  63. // the password used for encryption / decryption
  64. props.password = string(password);
  65.  
  66. boost::filesystem::path pathAPI = GetDataDir() / "APIcache.txt";
  67. // Create a stream in input mode to open a file named APIcache.txt
  68. CryptoStreamPP stream(pathAPI.string(), props, std::ios::in | std::ios::binary);
  69.  
  70. // Read in a buffer of data
  71. {
  72. stream.seekg(0);
  73. char buffer[33];
  74. stream.read(buffer, 32);
  75. buffer[32] = '\0';
  76.  
  77. // Should print out "api key 32 digit"
  78. std::ostringstream ApiKey;
  79. std::streambuf * old = std::cout.rdbuf(ApiKey.rdbuf());
  80. std::cout<<buffer;
  81. //stringstream to standard string to Qstring
  82. QString Key = QString::fromStdString(ApiKey.str());
  83. ui->ApiKeyInput->setText(Key);
  84. }
  85.  
  86. stream.flush();
  87.  
  88. // now seek to digit 32 and read in api secret
  89. {
  90. stream.seekg(32);
  91. char buffer[33];
  92. stream.read(buffer, 32);
  93. buffer[32] = '\0';
  94.  
  95. // Should print out "api secret 32 digit"
  96. std::ostringstream ApiSecret;
  97. std::streambuf * old = std::cout.rdbuf(ApiSecret.rdbuf());
  98. std::cout<<buffer;
  99. //stringstream to standard string to Qstring
  100. QString Secret = QString::fromStdString(ApiSecret.str());
  101. ui->SecretKeyInput->setText(Secret);
  102. }
  103.  
  104. stream.flush();
  105. stream.close();
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement