You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

rc4_cryption_impl.h 2.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * \file src/decryption/rc4/rc4_cryption_impl.h
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #pragma once
  12. #include "rc4_cryption_base.h"
  13. #include <memory>
  14. #include <vector>
  15. namespace lite {
  16. class RC4Impl {
  17. struct RC4State {
  18. rc4::RC4RandStream enc_stream;
  19. rc4::RC4RandStream hash_stream;
  20. } m_state;
  21. public:
  22. RC4Impl(const void* model_mem, size_t size, const std::vector<uint8_t>& key)
  23. : m_model_mem(model_mem), m_model_length(size) {
  24. const uint8_t* data = key.data();
  25. m_hash_key = *reinterpret_cast<const uint64_t*>(data);
  26. m_enc_key = *reinterpret_cast<const uint64_t*>(data + 8);
  27. }
  28. std::vector<uint8_t> encrypt_model();
  29. std::vector<uint8_t> decrypt_model();
  30. /*! \brief Read the input stream once in order to initialize the decryption
  31. * state.
  32. */
  33. void init_rc4_state();
  34. private:
  35. const void* m_model_mem;
  36. size_t m_model_length;
  37. uint64_t m_hash_key;
  38. uint64_t m_enc_key;
  39. };
  40. class SimpleFastRC4Impl {
  41. struct SFRC4State {
  42. rc4::RC4RandStream enc_stream;
  43. rc4::RC4RandStream hash_stream;
  44. } m_state;
  45. public:
  46. SimpleFastRC4Impl(const void* model_mem, size_t size,
  47. const std::vector<uint8_t>& key)
  48. : m_model_mem(model_mem), m_model_length(size) {
  49. const uint8_t* data = key.data();
  50. m_hash_key = *reinterpret_cast<const uint64_t*>(data);
  51. m_enc_key = *reinterpret_cast<const uint64_t*>(data + 8);
  52. }
  53. std::vector<uint8_t> encrypt_model();
  54. std::vector<uint8_t> decrypt_model();
  55. /*! \brief Read the input stream once in order to initialize the decryption
  56. * state.
  57. */
  58. void init_sfrc4_state();
  59. private:
  60. const void* m_model_mem;
  61. size_t m_model_length;
  62. uint64_t m_hash_key;
  63. uint64_t m_enc_key;
  64. };
  65. } // namespace lite
  66. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台