/ app / Models / GpgKey.php
GpgKey.php
 1  <?php
 2  
 3  namespace App\Models;
 4  
 5  use Illuminate\Database\Eloquent\Casts\Attribute;
 6  use Illuminate\Database\Eloquent\Factories\HasFactory;
 7  use Illuminate\Database\Eloquent\Model;
 8  use Illuminate\Database\Eloquent\Relations\MorphTo;
 9  use Illuminate\Database\Eloquent\SoftDeletes;
10  
11  class GpgKey extends Model
12  {
13      use HasFactory, SoftDeletes;
14  
15      protected $fillable = [
16          'key_id',
17          'key_algorithm',
18          'key_length',
19          'key_name',
20          'key_email',
21          'key_comment',
22          'key_fingerprint',
23          'key_created_at',
24          'key_expiration_at',
25          'key_passphrase',
26          'entity_type',
27          'entity_id'
28      ];
29  
30      /**
31       * The attributes that should be cast.
32       *
33       * @var array
34       */
35      protected $casts = [
36          'key_expiration_at' => 'datetime:d-m-Y H:i:s',
37          'key_created_at' => 'datetime:d-m-Y H:i:s',
38          'key_passphrase' => 'encrypted',
39      ];
40  
41      protected $hidden = [
42          'key_passphrase',
43      ];
44  
45      /**
46       * Get the parent entity model
47       */
48      public function entity(): MorphTo
49      {
50          return $this->morphTo();
51      }
52  
53      public function algorithmName(): Attribute
54      {
55          return new Attribute(function () {
56              return self::getAlgorithmName($this->key_algorithm);
57          });
58      }
59  
60      public static function getAlgorithmName(int $algorithm)
61      {
62          $algorithms = [
63              1 => 'RSA (Encrypt or Sign) [HAC]',
64              2 => 'RSA Encrypt-Only [HAC]',
65              3 => 'RSA Sign-Only [HAC]',
66              16 => 'Elgamal (Encrypt-Only) [ELGAMAL] [HAC]',
67              17 => 'DSA (Digital Signature Algorithm) [FIPS186] [HAC]',
68              18 => 'ECDH public key algorithm',
69              19 => 'ECDSA public key algorithm [FIPS186]',
70              20 => 'Reserved (formerly Elgamal Encrypt or Sign)',
71              21 => 'Reserved for Diffie-Hellman (X9.42, as defined for IETF-S/MIME)',
72              22 => 'EdDSA [I-D.irtf-cfrg-eddsa]',
73          ];
74  
75          return $algorithms[$algorithm];
76      }
77  }