paddle_encrypt_tool.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdio.h>
  15. #include <gflags/gflags.h>
  16. #include <iostream>
  17. #include <string>
  18. #include "encryption/include/model_code.h"
  19. #include "encryption/include/paddle_model_encrypt.h"
  20. #include "encryption/util/include/io_utils.h"
  21. DEFINE_string(model_filename, "", "Path of model");
  22. DEFINE_string(params_filename, "", "Path of params");
  23. DEFINE_string(cfg_file, "", "Path of yaml file");
  24. DEFINE_string(save_dir, "", "Path of save");
  25. DEFINE_string(key, "", "encrypt key");
  26. int main(int argc, char** argv) {
  27. // Parsing command-line
  28. google::ParseCommandLineFlags(&argc, &argv, true);
  29. if ("" == FLAGS_key) {
  30. FLAGS_key = paddle_generate_random_key();
  31. }
  32. std::cout << "key is " << FLAGS_key << std::endl;
  33. if ("" == FLAGS_save_dir) {
  34. std::cerr << "Please input a save path" << std::endl;
  35. return -1;
  36. }
  37. int ret = ioutil::dir_exist_or_mkdir(FLAGS_save_dir.c_str());
  38. if (FLAGS_save_dir[FLAGS_save_dir.length() - 1] != '/') {
  39. FLAGS_save_dir.append("/");
  40. }
  41. std::string save_name[] = {"encrypted.yml",
  42. "encrypted.pdmodel",
  43. "encrypted.pdparams"};
  44. std::string input_files[] = {FLAGS_cfg_file,
  45. FLAGS_model_filename,
  46. FLAGS_params_filename};
  47. std::string outfile;
  48. for (auto i = 0; i < 3; ++i) {
  49. outfile = FLAGS_save_dir + save_name[i];
  50. ret = paddle_encrypt_model(FLAGS_key.c_str(),
  51. input_files[i].c_str(),
  52. outfile.c_str());
  53. if (ret != 0) {
  54. std::cerr << ret << ", Failed encrypt "
  55. << input_files[i] << std::endl;
  56. return -1;
  57. }
  58. }
  59. std::cout << "save to " << FLAGS_save_dir << std::endl;
  60. return 0;
  61. }