basic.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (c) 2021 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. #pragma once
  15. #ifndef PADDLE_MODEL_PROTECT_UTIL_BASIC_H
  16. #define PADDLE_MODEL_PROTECT_UTIL_BASIC_H
  17. #include <iomanip>
  18. #include <iostream>
  19. #include <random>
  20. #include <string>
  21. #include <sstream>
  22. namespace util {
  23. namespace crypto {
  24. class Basic {
  25. public:
  26. /**
  27. * \brief byte to hex
  28. *
  29. * \note byte to hex.
  30. *
  31. *
  32. * \param in_byte byte array(in)
  33. * \param len byte array length(in)
  34. * \param out_hex the hex string(in)
  35. *
  36. *
  37. * \return return 0 if successful
  38. */
  39. static int byte_to_hex(const unsigned char* in_byte, int len,
  40. std::string& out_hex); // NOLINT
  41. /**
  42. * \brief hex to byte
  43. *
  44. * \note hex to byte.
  45. *
  46. *
  47. * \param in_hex the hex string(in)
  48. * \param out_byte byte array(out)
  49. *
  50. * \return return 0 if successful
  51. * -1 invalid in_hex
  52. */
  53. static int hex_to_byte(const std::string& in_hex, unsigned char* out_byte);
  54. /**
  55. * \brief get random char for length
  56. *
  57. * \note get random char for length
  58. *
  59. *
  60. * \param array to be random(out)
  61. * \param len array length(in)
  62. *
  63. * \return return 0 if successful
  64. * -1 invalid parameters
  65. */
  66. static int random(unsigned char* random, int len);
  67. };
  68. } // namespace crypto
  69. } // namespace util
  70. #endif // PADDLE_MODEL_PROTECT_UTIL_BASIC_H