Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
35 / 35 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| Algorithm | |
100.00% |
35 / 35 |
|
100.00% |
8 / 8 |
10 | |
100.00% |
1 / 1 |
| isSymmetric | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isAsymmetric | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isRSA | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| isECDSA | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| label | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
| labelList | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| toArrayWithLabels | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace DevToolbelt\Enums\Security; |
| 6 | |
| 7 | use DevToolbelt\Enums\EnumInterface; |
| 8 | |
| 9 | enum Algorithm: string implements EnumInterface |
| 10 | { |
| 11 | // HMAC algorithms |
| 12 | case HS256 = 'HS256'; |
| 13 | case HS384 = 'HS384'; |
| 14 | case HS512 = 'HS512'; |
| 15 | |
| 16 | // RSA algorithms |
| 17 | case RS256 = 'RS256'; |
| 18 | case RS384 = 'RS384'; |
| 19 | case RS512 = 'RS512'; |
| 20 | |
| 21 | // ECDSA algorithms |
| 22 | case ES256 = 'ES256'; |
| 23 | case ES384 = 'ES384'; |
| 24 | case ES512 = 'ES512'; |
| 25 | |
| 26 | // RSA-PSS algorithms |
| 27 | case PS256 = 'PS256'; |
| 28 | case PS384 = 'PS384'; |
| 29 | case PS512 = 'PS512'; |
| 30 | |
| 31 | // EdDSA algorithm |
| 32 | case EdDSA = 'EdDSA'; |
| 33 | |
| 34 | public function isSymmetric(): bool |
| 35 | { |
| 36 | return in_array($this, [self::HS256, self::HS384, self::HS512], true); |
| 37 | } |
| 38 | |
| 39 | public function isAsymmetric(): bool |
| 40 | { |
| 41 | return !$this->isSymmetric(); |
| 42 | } |
| 43 | |
| 44 | public function isRSA(): bool |
| 45 | { |
| 46 | return in_array($this, [ |
| 47 | self::RS256, |
| 48 | self::RS384, |
| 49 | self::RS512, |
| 50 | self::PS256, |
| 51 | self::PS384, |
| 52 | self::PS512 |
| 53 | ], true); |
| 54 | } |
| 55 | |
| 56 | public function isECDSA(): bool |
| 57 | { |
| 58 | return in_array($this, [self::ES256, self::ES384, self::ES512], true); |
| 59 | } |
| 60 | |
| 61 | public function label(): string |
| 62 | { |
| 63 | return match ($this) { |
| 64 | self::HS256 => 'HMAC using SHA-256', |
| 65 | self::HS384 => 'HMAC using SHA-384', |
| 66 | self::HS512 => 'HMAC using SHA-512', |
| 67 | self::RS256 => 'RSASSA-PKCS1-v1_5 using SHA-256', |
| 68 | self::RS384 => 'RSASSA-PKCS1-v1_5 using SHA-384', |
| 69 | self::RS512 => 'RSASSA-PKCS1-v1_5 using SHA-512', |
| 70 | self::ES256 => 'ECDSA using P-256 and SHA-256', |
| 71 | self::ES384 => 'ECDSA using P-384 and SHA-384', |
| 72 | self::ES512 => 'ECDSA using P-521 and SHA-512', |
| 73 | self::PS256 => 'RSASSA-PSS using SHA-256 and MGF1 with SHA-256', |
| 74 | self::PS384 => 'RSASSA-PSS using SHA-384 and MGF1 with SHA-384', |
| 75 | self::PS512 => 'RSASSA-PSS using SHA-512 and MGF1 with SHA-512', |
| 76 | self::EdDSA => 'Edwards-curve Digital Signature Algorithm', |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @return string[] |
| 82 | */ |
| 83 | public function labelList(): array |
| 84 | { |
| 85 | return array_map(fn (self $case) => $case->label(), self::cases()); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @return array<string, string> |
| 90 | */ |
| 91 | public static function toArray(): array |
| 92 | { |
| 93 | $result = []; |
| 94 | |
| 95 | foreach (self::cases() as $algorithm) { |
| 96 | $result[$algorithm->value] = $algorithm->value; |
| 97 | } |
| 98 | |
| 99 | return $result; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @return array<string, string> |
| 104 | */ |
| 105 | public static function toArrayWithLabels(): array |
| 106 | { |
| 107 | $result = []; |
| 108 | |
| 109 | foreach (self::cases() as $algorithm) { |
| 110 | $result[$algorithm->value] = $algorithm->label(); |
| 111 | } |
| 112 | |
| 113 | return $result; |
| 114 | } |
| 115 | } |