Encryptor.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * This file is part of the overtrue/wechat.
  9. *
  10. * (c) overtrue <i@overtrue.me>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. namespace ByteDance\Kernel;
  16. use ByteDance\Kernel\Exceptions\RuntimeException;
  17. use ByteDance\Kernel\Support\AES;
  18. use Throwable;
  19. use function ByteDance\Kernel\Support\str_random;
  20. /**
  21. * Class Encryptor.
  22. *
  23. * @author overtrue <i@overtrue.me>
  24. */
  25. class Encryptor
  26. {
  27. const ERROR_INVALID_SIGNATURE = -40001; // Signature verification failed
  28. const ERROR_CALC_SIGNATURE = -40003; // Calculating the signature failed
  29. const ERROR_INVALID_AES_KEY = -40004; // Invalid AESKey
  30. const ERROR_INVALID_APP_ID = -40005; // Check AppID failed
  31. const ERROR_ENCRYPT_AES = -40006; // AES EncryptionInterface failed
  32. const ERROR_DECRYPT_AES = -40007; // AES decryption failed
  33. const ERROR_BASE64_ENCODE = -40009; // Base64 encoding failed
  34. const ERROR_BASE64_DECODE = -40010; // Base64 decoding failed
  35. const ILLEGAL_BUFFER = -41003; // Illegal buffer
  36. /**
  37. * @var string
  38. */
  39. protected $aesKey;
  40. /**
  41. * Block size.
  42. *
  43. * @var int
  44. */
  45. protected $blockSize = 32;
  46. /**
  47. * Constructor.
  48. *
  49. * @param string $appId
  50. * @param string|null $token
  51. * @param string|null $aesKey
  52. */
  53. public function __construct(string $aesKey = null)
  54. {
  55. $this->aesKey = base64_decode($aesKey.'=', true);
  56. }
  57. /**
  58. * Get SHA1.
  59. *
  60. * @return string
  61. *
  62. * @throws self
  63. */
  64. public function signature(): string
  65. {
  66. $array = func_get_args();
  67. sort($array, SORT_STRING);
  68. return sha1(implode($array));
  69. }
  70. /**
  71. * PKCS#7 pad.
  72. *
  73. * @param string $text
  74. * @param int $blockSize
  75. *
  76. * @return string
  77. *
  78. * @throws \ByteDance\Kernel\Exceptions\RuntimeException
  79. */
  80. public function pkcs7Pad(string $text, int $blockSize): string
  81. {
  82. if ($blockSize > 256) {
  83. throw new RuntimeException('$blockSize may not be more than 256');
  84. }
  85. $padding = $blockSize - (strlen($text) % $blockSize);
  86. $pattern = chr($padding);
  87. return $text.str_repeat($pattern, $padding);
  88. }
  89. /**
  90. * PKCS#7 unpad.
  91. *
  92. * @param string $text
  93. *
  94. * @return string
  95. */
  96. public function pkcs7Unpad(string $text): string
  97. {
  98. $pad = ord(substr($text, -1));
  99. if ($pad < 1 || $pad > $this->blockSize) {
  100. $pad = 0;
  101. }
  102. return substr($text, 0, (strlen($text) - $pad));
  103. }
  104. }