Something went wrong. Try again.
Laravel AT Protocol Client (alpha & unstable)
Something went wrong. Try again.
1.5 kB · 75 lines
PHP
at dev
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576<?php
namespace SocialDept\AtpClient\Crypto;
use phpseclib3\Crypt\EC;use phpseclib3\Crypt\EC\PrivateKey;use phpseclib3\Crypt\EC\PublicKey;use phpseclib3\Crypt\PublicKeyLoader;
abstract class AbstractKeypair{ const CURVE = '';
const ALG = '';
const MULTIBASE_PREFIX = '';
protected PrivateKey|PublicKey $key;
/** * Create a new keypair */ public static function create(): static { $self = new static; $self->key = EC::createKey(static::CURVE);
return $self; }
/** * Load keypair from base64-encoded private key */ public static function load(?string $private = null): static { $self = new static; $self->key = PublicKeyLoader::load( base64_decode(strtr($private, '-_', '+/'), strict: true) );
return $self; }
/** * Get the key instance */ public function key(): PrivateKey|PublicKey { return $this->key; }
/** * Export private key as base64-encoded string */ public function privateB64(): string { return strtr(base64_encode($this->key->toString('PKCS8')), '+/', '-_'); }
/** * Export private key as PEM string */ public function toPEM(): string { return $this->key->toString('PKCS8'); }
/** * Convert to JSON Web Key format */ public function toJWK(): JsonWebKey { return new JsonWebKey($this->key); }}