codec_util.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef COS_CPP_SDK_V5_INCLUDE_UTIL_CODEC_UTIL_H_
  2. #define COS_CPP_SDK_V5_INCLUDE_UTIL_CODEC_UTIL_H_
  3. #include <stdint.h>
  4. #include <string>
  5. namespace qcloud_cos {
  6. class CodecUtil {
  7. public:
  8. /**
  9. * @brief 将字符x转成十六进制 (x的值[0, 15])
  10. *
  11. * @param x
  12. *
  13. * @return 十六进制字符
  14. */
  15. static unsigned char ToHex(const unsigned char& x);
  16. /**
  17. * @brief 将二进制数据转成十六进制 (x的值[0, 15]),上层调用保证hex的大小足够
  18. *
  19. * @param bin
  20. *
  21. * @param binLen
  22. *
  23. * @param hex 存放结果的数据块
  24. *
  25. * @return 无
  26. */
  27. static void BinToHex(const unsigned char* bin, unsigned int binLen,
  28. char* hex);
  29. static bool IsalnumAscii(int c);
  30. static std::string EncodeKey(const std::string& key);
  31. /**
  32. * @brief 对字符串进行URL编码
  33. *
  34. * @param str 带编码的字符串
  35. *
  36. * @return 经过URL编码的字符串
  37. */
  38. static std::string UrlEncode(const std::string& str);
  39. /**
  40. * @brief 对字符串进行base64编码
  41. *
  42. * @param plainText 待编码的字符串
  43. *
  44. * @return 编码后的字符串
  45. */
  46. static std::string Base64Encode(const std::string& plainText);
  47. /**
  48. * @brief 获取hmacSha1值
  49. *
  50. * @param plainText 明文
  51. * @param key 秘钥
  52. *
  53. * @return 获取的hmacsha1值
  54. */
  55. static std::string HmacSha1(const std::string& plainText,
  56. const std::string& key);
  57. /**
  58. * @brief 获取hmacSha1的16进制值
  59. *
  60. * @param plainText 明文
  61. * @param key 秘钥
  62. *
  63. * @return 获取的hmacsha1的16进制值
  64. */
  65. static std::string HmacSha1Hex(const std::string& plainText,
  66. const std::string& key);
  67. static std::string RawMd5(const std::string& plainText);
  68. static std::string RawMd51(const std::string& plainText);
  69. static std::string HexToBin(const std::string& strHex);
  70. static std::string DigestToHex(const unsigned char *digest, size_t len);
  71. };
  72. } // namespace qcloud_cos
  73. #endif // COS_CPP_SDK_V5_INCLUDE_UTIL_CODEC_UTIL_H_