CoreFiles by mySites.guru
DERData.php80 lines, 1,938 bytes, from Joomla_5.1.0-alpha4-Alpha-Full_Package.zip
Raw History Compare with 5.1.0-alpha3
md5
3d981331616052c011af7b718332dcf1
sha1
4e585f953488078f9f51d734c0cadb600e37e8c2
sha256
d21c38cfdd6212ea87b58a795cf6e2fe187fd8256e458fa7dfd599be0a439a64
<?php

declare(strict_types=1);

namespace SpomkyLabs\Pki\ASN1;

use BadMethodCallException;
use function mb_strlen;
use SpomkyLabs\Pki\ASN1\Component\Identifier;
use SpomkyLabs\Pki\ASN1\Component\Length;
use SpomkyLabs\Pki\ASN1\Feature\ElementBase;

/**
 * Container for raw DER encoded data.
 *
 * May be inserted into structure without decoding first.
 */
final class DERData extends Element
{
    /**
     * DER encoded data.
     */
    private readonly string $der;

    /**
     * Identifier of the underlying type.
     */
    private readonly Identifier $identifier;

    /**
     * Offset to the content in DER data.
     */
    private int $contentOffset = 0;

    /**
     * @param string $data DER encoded data
     */
    private function __construct(string $data)
    {
        $this->identifier = Identifier::fromDER($data, $this->contentOffset);
        // check that length encoding is valid
        Length::expectFromDER($data, $this->contentOffset);
        $this->der = $data;
        parent::__construct($this->identifier->intTag());
    }

    public static function create(string $data): self
    {
        return new self($data);
    }

    public function typeClass(): int
    {
        return $this->identifier->typeClass();
    }

    public function isConstructed(): bool
    {
        return $this->identifier->isConstructed();
    }

    public function toDER(): string
    {
        return $this->der;
    }

    protected function encodedAsDER(): string
    {
        // if there's no content payload
        if (mb_strlen($this->der, '8bit') === $this->contentOffset) {
            return '';
        }
        return mb_substr($this->der, $this->contentOffset, null, '8bit');
    }

    protected static function decodeFromDER(Identifier $identifier, string $data, int &$offset): ElementBase
    {
        throw new BadMethodCallException(__METHOD__ . ' must be implemented in derived class.');
    }
}

The same content is in 22 releases, Joomla 5.0.0-alpha1 to 5.1.0-alpha4. See them all, or every version of this file.