CryptTrait.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Http\Traits;
  3. use phpseclib3\Crypt\AES;
  4. use phpseclib3\Crypt\PublicKeyLoader;
  5. use phpseclib3\Crypt\RSA;
  6. trait CryptTrait
  7. {
  8. protected string $base64PublicKey = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAoVdxG0Qi9pip46Jw9ImSlPVD8+L2mM47ey6EZna7D7utgNdh8Tzkjrm1Yl4h6kPJrhdWvMIJGS51+6dh041IXcJEoUquNblUEqAUXBYwQM8PdfnS12SjlvZrP4q6whBE7IV1SEIBJP0gSK5/8Iu+uld2ctJiU4p8uswL2bCPGWdvVPltxAg6hfAG/ImRUKPRewQsFhkFvqIDCpO6aeaR10q6wwENZltlJeeRnl02VWSneRmPqqypqCxz0Y+yWCYtsA+ngfZmwRMaFkXcWjaWnvSqqV33OAsrQkvuBHWoEEkvQ0P08+h9Fy2+FhY9TeuukQ2CVFz5YyOhp25QtWyQI+IaDKk+hLxJ1APR0c3tmV0ANEIjO6HhJIdu2KQKtgFppvqSrZp2OKtI8EZgVbWuho50xvlaPGzWoMi9HSCb+8ARamlOpesxHH3O0cTRUnft2Zk1FHQb2Pidb2z5onMEnzP2xpTqAIVQyb6nMac9tof5NFxwR/c4pmci+1n8GFJIFN18j2XGad1mNyio/R8LabqnzNwJC6VPnZJz5/pDUIk9yKNOY0KJe64SRiL0a4SNMohtyj6QlA/3SGxaEXb8UHpophv4G9wN1CgfyUamsRqp8zo5qDxBvlaIlfkqJvYPkltj7/23FHDjPi8q8UkSiAeu7IV5FTfB5KsiN8+sGSMCAwEAAQ==";
  9. protected function generateSessionId(): string
  10. {
  11. $data = random_bytes(16);
  12. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  13. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  14. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  15. }
  16. protected function generateRandomBytes(int $length): string
  17. {
  18. return base64_encode(random_bytes($length));
  19. }
  20. protected function generateSessionKey(string $sessionId, string $secretKey, string $iv): string
  21. {
  22. $sessionAesKey = "AES_GCM$" . $secretKey . "$" . $iv;
  23. $encryptedSessionAesKey = $this->encryptSessionAesKey($sessionAesKey);
  24. return "v1$" . $sessionId . "$" . $encryptedSessionAesKey;
  25. }
  26. protected function encryptSessionAesKey(string $sessionAesKey): string
  27. {
  28. $rsa = PublicKeyLoader::load($this->base64PublicKey)->withPadding(RSA::ENCRYPTION_OAEP)->withHash('sha1')->withMGFHash('sha1');
  29. return base64_encode($rsa->encrypt($sessionAesKey));
  30. }
  31. protected function encryptData(string $sessionId, string $secretKey, string $iv, string $data): string
  32. {
  33. $cipher = new AES('gcm');
  34. $cipher->setKey(base64_decode($secretKey));
  35. $cipher->setNonce(base64_decode($iv));
  36. $cipher->setAAD(base64_decode($secretKey));
  37. $encrypted = $cipher->encrypt($data);
  38. $combined = base64_encode($encrypted . $cipher->getTag());
  39. return 'v1$' . $sessionId . '$' . $combined;
  40. }
  41. protected function decryptData(string $secretKey, string $iv, string $encryptedData): string
  42. {
  43. $parsed = base64_decode(explode('$', $encryptedData)[2]);
  44. $encrypted = substr($parsed, 0, strlen($parsed) - 16);
  45. $tag = substr($parsed, strlen($parsed) - 16);
  46. $cipher = new AES('gcm');
  47. $cipher->setKey(base64_decode($secretKey));
  48. $cipher->setNonce(base64_decode($iv));
  49. $cipher->setAAD(base64_decode($secretKey));
  50. $cipher->setTag($tag);
  51. return $cipher->decrypt($encrypted);
  52. }
  53. }