1010use Jose \Component \Core \Util \BigInteger ;
1111use Jose \Component \Core \Util \Hash ;
1212use Jose \Component \Core \Util \RSAKey ;
13+ use LogicException ;
1314use function ord ;
1415use RuntimeException ;
1516use const STR_PAD_LEFT ;
@@ -31,20 +32,34 @@ final class RSACrypt
3132
3233 public static function encrypt (RSAKey $ key , string $ data , int $ mode , ?string $ hash = null ): string
3334 {
34- return match ($ mode ) {
35- self ::ENCRYPTION_OAEP => self ::encryptWithRSAOAEP ($ key , $ data , $ hash ),
36- self ::ENCRYPTION_PKCS1 => self ::encryptWithRSA15 ($ key , $ data ),
37- default => throw new InvalidArgumentException ('Unsupported mode. ' ),
38- };
35+ switch ($ mode ) {
36+ case self ::ENCRYPTION_OAEP :
37+ if ($ hash === null ) {
38+ throw new LogicException ('Hash shall be defined for RSA OAEP cyphering ' );
39+ }
40+
41+ return self ::encryptWithRSAOAEP ($ key , $ data , $ hash );
42+ case self ::ENCRYPTION_PKCS1 :
43+ return self ::encryptWithRSA15 ($ key , $ data );
44+ default :
45+ throw new InvalidArgumentException ('Unsupported mode. ' );
46+ }
3947 }
4048
4149 public static function decrypt (RSAKey $ key , string $ plaintext , int $ mode , ?string $ hash = null ): string
4250 {
43- return match ($ mode ) {
44- self ::ENCRYPTION_OAEP => self ::decryptWithRSAOAEP ($ key , $ plaintext , $ hash ),
45- self ::ENCRYPTION_PKCS1 => self ::decryptWithRSA15 ($ key , $ plaintext ),
46- default => throw new InvalidArgumentException ('Unsupported mode. ' ),
47- };
51+ switch ($ mode ) {
52+ case self ::ENCRYPTION_OAEP :
53+ if ($ hash === null ) {
54+ throw new LogicException ('Hash shall be defined for RSA OAEP cyphering ' );
55+ }
56+
57+ return self ::decryptWithRSAOAEP ($ key , $ plaintext , $ hash );
58+ case self ::ENCRYPTION_PKCS1 :
59+ return self ::decryptWithRSA15 ($ key , $ plaintext );
60+ default :
61+ throw new InvalidArgumentException ('Unsupported mode. ' );
62+ }
4863 }
4964
5065 public static function encryptWithRSA15 (RSAKey $ key , string $ data ): string
@@ -64,8 +79,8 @@ public static function encryptWithRSA15(RSAKey $key, string $data): string
6479 $ type = 2 ;
6580 $ data = chr (0 ) . chr ($ type ) . $ ps . chr (0 ) . $ data ;
6681
67- $ data = BigInteger::createFromBinaryString ($ data );
68- $ c = self ::getRSAEP ($ key , $ data );
82+ $ binaryData = BigInteger::createFromBinaryString ($ data );
83+ $ c = self ::getRSAEP ($ key , $ binaryData );
6984
7085 return self ::convertIntegerToOctetString ($ c , $ key ->getModulusLength ());
7186 }
0 commit comments