src/ECPublicKeyTrait.php
public function getPublicKeyPem(): string
    {
        $key = $this->getPublicKeyBinary();

        // Described in RFC 5480
        // Just use an OID calculator to figure out *that* encoding
        $der = hex2bin(
            '3059' // SEQUENCE, length 89
                .'3013' // SEQUENCE, length 19
                    .'0607' // OID, length 7
                        .'2a8648ce3d0201' // 1.2.840.10045.2.1 = EC Public Key
                    .'0608' // OID, length 8
                        .'2a8648ce3d030107' // 1.2.840.10045.3.1.7 = P-256 Curve
                .'0342' // BIT STRING, length 66
                    .'00' // prepend with NUL - pubkey will follow
        );

        $der .= $key;
        $pem  = "-----BEGIN PUBLIC KEY-----\r\n";
        $pem .= chunk_split(base64_encode($der), 64);
        $pem .= "-----END PUBLIC KEY-----";
        return $pem;
    }
public function getPublicKeyPem(): string
    {
        $key = $this->getPublicKeyBinary();

        // Described in RFC 5480
        // Just use an OID calculator to figure out *that* encoding
        $der = hex2bin(
            '3059' // SEQUENCE, length 89
                .'3013' // SEQUENCE, length 19
                    .'0607' // OID, length 7
                        .'2a8648ce3d0201' // 1.2.840.10045.2.1 = EC Public Key
                    .'0608' // OID, length 8
                        .'2a8648ce3d030107' // 1.2.840.10045.3.1.7 = P-256 Curve
                .'0342' // BIT STRING, length 66
                    .'00' // prepend with NUL - pubkey will follow
        );

        $der .= $key;
        $pem  = "-----BEGIN PUBLIC KEY-----\r\n";
        $pem .= chunk_split(base64_encode($der), 64);
        $pem .= "-----END PUBLIC KEY-----";
        return $pem;
    }
public function getPublicKeyPem ( ) : string { $ key = $ this -> getPublicKeyBinary ( ) ; // Described in RFC 5480 // Just use an OID calculator to figure out *that* encoding $ der = hex2bin ( '3059' // SEQUENCE, length 89 . '3013' // SEQUENCE, length 19 . '0607' // OID, length 7 . '2a8648ce3d0201' // 1.2.840.10045.2.1 = EC Public Key . '0608' // OID, length 8 . '2a8648ce3d030107' // 1.2.840.10045.3.1.7 = P-256 Curve . '0342' // BIT STRING, length 66 . '00' // prepend with NUL - pubkey will follow ) ; $ der .= $ key ; $ pem = "-----BEGIN PUBLIC KEY-----\r\n" ; $ pem .= chunk_split ( base64_encode ( $ der ) , 64 ) ; $ pem .= "-----END PUBLIC KEY-----" ; return $ pem ; }
public key component
public key component