Skip to content

Commit e2d4af6

Browse files
authored
Refactor encrypt method to use buildPayload for consistent data formatting
1 parent 8c58d37 commit e2d4af6

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/AESCryptoServiceProvider.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,30 @@ public function encrypt(string $plainText, bool $legacy = false): string
119119
$this->tag
120120
);
121121

122-
if ($legacy) {
123-
return base64_encode($this->iv . $this->tag . $encryptedBytes);
124-
}
122+
return base64_encode($this->buildPayload(
123+
iv: $this->iv,
124+
tag: $this->tag,
125+
encryptedData: $encryptedBytes,
126+
legacy: $legacy
127+
));
128+
}
125129

126-
return base64_encode($this->iv . $encryptedBytes . $this->tag);
130+
/**
131+
* Build payload for encrypted data
132+
*
133+
* @param string $iv
134+
* @param string $tag
135+
* @param string $encryptedData
136+
* @param bool $legacy
137+
* If true, returns IV-TAG-EncryptedData format
138+
* If false, returns IV-EncryptedData-TAG format
139+
* @return string
140+
*/
141+
protected function buildPayload(string $iv, string $tag, string $encryptedData, bool $legacy): string
142+
{
143+
return $legacy
144+
? $iv . $tag . $encryptedData // IV-TAG-EncryptedData
145+
: $iv . $encryptedData . $tag; // IV-EncryptedData-TAG
127146
}
128147

129148
/**

0 commit comments

Comments
 (0)