system_utils.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <sys/timeb.h>
  2. #include <string.h>
  3. #include <model_code.h>
  4. #include <algorithm>
  5. #include <iterator>
  6. #include "system_utils.h"
  7. #include "crypto/basic.h"
  8. #include "crypto/sha256_utils.h"
  9. #include "io_utils.h"
  10. #include "log.h"
  11. #include "../constant/constant_model.h"
  12. const char alphabet[] = "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(){}[]<>?~";
  13. namespace util {
  14. int SystemUtils::intN(int n) {
  15. return rand() % n;
  16. }
  17. std::string SystemUtils::random_key_iv(int len) {
  18. unsigned char* tmp = (unsigned char*) malloc(sizeof(unsigned char) * len);
  19. int ret = util::crypto::Basic::random(tmp, len);
  20. std::string tmp_str(reinterpret_cast<const char*>(tmp), len);
  21. free(tmp);
  22. return tmp_str;
  23. }
  24. std::string SystemUtils::random_str(int len) {
  25. unsigned char* tmp = (unsigned char*) malloc(sizeof(unsigned char) * len);
  26. int ret = util::crypto::Basic::random(tmp, len);
  27. std::string tmp_str(reinterpret_cast<const char*>(tmp), len);
  28. free(tmp);
  29. return tmp_str;
  30. }
  31. int SystemUtils::check_key_match(const char* key, const char* filepath) {
  32. std::string aes_key_iv(key);
  33. std::string sha256_aes_key_iv = util::crypto::SHA256Utils::sha256_string(aes_key_iv);
  34. unsigned char* data_pos = (unsigned char*) malloc(sizeof(unsigned char) * 64);
  35. int ret =
  36. ioutil::read_with_pos_and_length(filepath, data_pos, constant::MAGIC_NUMBER_LEN + constant::VERSION_LEN, 64);
  37. if (ret != CODE_OK) {
  38. LOGD("[M]read file failed when check key");
  39. return ret;
  40. }
  41. std::string check_str((char*) data_pos, 64);
  42. free(data_pos);
  43. if (strcmp(sha256_aes_key_iv.c_str(), check_str.c_str()) != 0) {
  44. return CODE_KEY_NOT_MATCH;
  45. }
  46. return CODE_OK;
  47. }
  48. int SystemUtils::check_key_match(const std::string &key, std::istream &cipher_stream) {
  49. cipher_stream.seekg(0, std::ios::beg);
  50. std::string sha256_aes_key_iv = util::crypto::SHA256Utils::sha256_string(key);
  51. int check_len = 64;
  52. std::string data_pos_str;
  53. cipher_stream.seekg(constant::MAGIC_NUMBER_LEN + constant::VERSION_LEN);
  54. std::copy_n(std::istreambuf_iterator<char>(cipher_stream), check_len, std::back_inserter(data_pos_str));
  55. if (data_pos_str.size() != check_len){
  56. LOGD("[M]read file failed when check key");
  57. return CODE_OPEN_FAILED;
  58. }
  59. if (data_pos_str == sha256_aes_key_iv){
  60. return CODE_OK;
  61. }
  62. return CODE_KEY_NOT_MATCH;
  63. }
  64. /**
  65. *
  66. * @param filepath
  67. * @return 0 - file encrypted 1 - file unencrypted
  68. */
  69. int SystemUtils::check_file_encrypted(const char* filepath) {
  70. size_t read_len = constant::MAGIC_NUMBER_LEN + constant::VERSION_LEN;
  71. unsigned char* data_pos = (unsigned char*) malloc(sizeof(unsigned char) * read_len);
  72. if (ioutil::read_with_pos_and_length(filepath, data_pos, 0, read_len) != CODE_OK) {
  73. LOGD("check file failed when read %s(file)", filepath);
  74. return CODE_OPEN_FAILED;
  75. }
  76. std::string tag(constant::MAGIC_NUMBER);
  77. tag.append(constant::VERSION);
  78. int ret_cmp = strcmp(tag.c_str(), (const char*) data_pos) == 0 ? 0 : 1;
  79. free(data_pos);
  80. return ret_cmp;
  81. }
  82. int SystemUtils::check_file_encrypted(std::istream &cipher_stream) {
  83. cipher_stream.seekg(0, std::ios::beg);
  84. size_t read_len = constant::MAGIC_NUMBER_LEN + constant::VERSION_LEN;
  85. std::string data_pos_str;
  86. std::copy_n(std::istreambuf_iterator<char>(cipher_stream), read_len, std::back_inserter(data_pos_str));
  87. if (data_pos_str.size() != read_len){
  88. LOGD("check file failed when read cipher stream");
  89. return CODE_OPEN_FAILED;
  90. }
  91. std::string tag(constant::MAGIC_NUMBER);
  92. tag.append(constant::VERSION);
  93. if (data_pos_str == tag){
  94. return 0;
  95. }
  96. return 1;
  97. }
  98. int SystemUtils::check_pattern_exist(const std::vector<std::string>& vecs, const std::string& pattern) {
  99. if (std::find(vecs.begin(), vecs.end(), pattern) == vecs.end()) {
  100. return -1; // not exist
  101. } else {
  102. return 0; // exist
  103. }
  104. }
  105. }