Utility.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace crmeb\services\wechat\orderShipping;
  3. use crmeb\exceptions\AdminException;
  4. class Utility
  5. {
  6. /**
  7. * @param int $padding
  8. */
  9. private static function paddingModeLimitedCheck(int $padding): void
  10. {
  11. if (!($padding === OPENSSL_PKCS1_OAEP_PADDING || $padding === OPENSSL_PKCS1_PADDING)) {
  12. throw new AdminException(sprintf("Doesn't supported padding mode(%d), here only support OPENSSL_PKCS1_OAEP_PADDING or OPENSSL_PKCS1_PADDING.", $padding));
  13. }
  14. }
  15. /**
  16. * 加密数据
  17. * @param string $plaintext
  18. * @param int $padding
  19. * @return string
  20. */
  21. public function encryptor(string $plaintext, int $padding = OPENSSL_PKCS1_OAEP_PADDING)
  22. {
  23. self::paddingModeLimitedCheck($padding);
  24. if (!openssl_public_encrypt($plaintext, $encrypted, $this->getPublicKey(), $padding)) {
  25. throw new AdminException('Encrypting the input $plaintext failed, please checking your $publicKey whether or nor correct.');
  26. }
  27. return base64_encode($encrypted);
  28. }
  29. public static function encryptTel($tel)
  30. {
  31. $new_tel = substr_replace($tel, '****', 3, 4);
  32. return $new_tel;
  33. }
  34. }