Skip to content

Bump requirements to PHP 8.0 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ build:
file: build/logs/clover.xml
environment:
php:
version: 7.2
version: 8.0
filter:
excluded_paths:
- test/
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: php
php:
- "7.2"
- "7.3"
- "8.0"
- "8.1"
before_script:
- "composer install"
- "composer require php-coveralls/php-coveralls"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ textual encodings of cryptographic structures _(PEM)_.

## Requirements

- PHP >=7.2
- PHP >=8.0

## Installation

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
}
],
"require": {
"php": ">=7.2"
"php": "^8.0"
},
"require-dev": {
"phpunit/phpunit": "^8.1"
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
"Sop\\CryptoEncoding\\": "lib/CryptoEncoding/"
}
}
}
}
77 changes: 48 additions & 29 deletions lib/CryptoEncoding/PEM.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Sop\CryptoEncoding;

use RuntimeException;
use UnexpectedValueException;

use function base_decode;
use function base_encode;
use function chunk_split;
use function file_get_contents;
use function is_readable;
use function preg_match;
use function trim;

/**
* Implements PEM file encoding and decoding.
*
Expand All @@ -12,44 +23,45 @@
class PEM
{
// well-known PEM types
const TYPE_CERTIFICATE = 'CERTIFICATE';
const TYPE_CRL = 'X509 CRL';
const TYPE_CERTIFICATE_REQUEST = 'CERTIFICATE REQUEST';
const TYPE_ATTRIBUTE_CERTIFICATE = 'ATTRIBUTE CERTIFICATE';
const TYPE_PRIVATE_KEY = 'PRIVATE KEY';
const TYPE_PUBLIC_KEY = 'PUBLIC KEY';
const TYPE_ENCRYPTED_PRIVATE_KEY = 'ENCRYPTED PRIVATE KEY';
const TYPE_RSA_PRIVATE_KEY = 'RSA PRIVATE KEY';
const TYPE_RSA_PUBLIC_KEY = 'RSA PUBLIC KEY';
const TYPE_EC_PRIVATE_KEY = 'EC PRIVATE KEY';
const TYPE_PKCS7 = 'PKCS7';
const TYPE_CMS = 'CMS';
public const TYPE_CERTIFICATE = 'CERTIFICATE';
public const TYPE_CRL = 'X509 CRL';
public const TYPE_CERTIFICATE_REQUEST = 'CERTIFICATE REQUEST';
public const TYPE_ATTRIBUTE_CERTIFICATE = 'ATTRIBUTE CERTIFICATE';
public const TYPE_PRIVATE_KEY = 'PRIVATE KEY';
public const TYPE_PUBLIC_KEY = 'PUBLIC KEY';
public const TYPE_ENCRYPTED_PRIVATE_KEY = 'ENCRYPTED PRIVATE KEY';
public const TYPE_RSA_PRIVATE_KEY = 'RSA PRIVATE KEY';
public const TYPE_RSA_PUBLIC_KEY = 'RSA PUBLIC KEY';
public const TYPE_EC_PRIVATE_KEY = 'EC PRIVATE KEY';
public const TYPE_PKCS7 = 'PKCS7';
public const TYPE_CMS = 'CMS';

/**
* Regular expression to match PEM block.
*
* @var string
*/
const PEM_REGEX = '/' .
public const PEM_REGEX =
'/' .
/* line start */ '(?:^|[\r\n])' .
/* header */ '-----BEGIN (.+?)-----[\r\n]+' .
/* payload */ '(.+?)' .
/* trailer */ '[\r\n]+-----END \\1-----' .
'/ms';
'/ms';

/**
* Content type.
*
* @var string
*/
protected $_type;
protected $type;

/**
* Payload.
*
* @var string
*/
protected $_data;
protected $data;

/**
* Constructor.
Expand All @@ -59,8 +71,8 @@ class PEM
*/
public function __construct(string $type, string $data)
{
$this->_type = $type;
$this->_data = $data;
$this->type = $type;
$this->data = $data;
}

/**
Expand All @@ -83,12 +95,12 @@ public function __toString(): string
public static function fromString(string $str): self
{
if (!preg_match(self::PEM_REGEX, $str, $match)) {
throw new \UnexpectedValueException('Not a PEM formatted string.');
throw new UnexpectedValueException('Not a PEM formatted string.');
}
$payload = preg_replace('/\s+/', '', $match[2]);
$data = base64_decode($payload, true);
if (false === $data) {
throw new \UnexpectedValueException('Failed to decode PEM data.');
throw new UnexpectedValueException('Failed to decode PEM data.');
}
return new self($match[1], $data);
}
Expand All @@ -104,9 +116,11 @@ public static function fromString(string $str): self
*/
public static function fromFile(string $filename): self
{
if (!is_readable($filename) ||
false === ($str = file_get_contents($filename))) {
throw new \RuntimeException("Failed to read {$filename}.");
if (
!is_readable($filename) ||
false === ($str = file_get_contents($filename))
) {
throw new RuntimeException("Failed to read {$filename}.");
}
return self::fromString($str);
}
Expand All @@ -118,7 +132,7 @@ public static function fromFile(string $filename): self
*/
public function type(): string
{
return $this->_type;
return $this->type;
}

/**
Expand All @@ -128,7 +142,7 @@ public function type(): string
*/
public function data(): string
{
return $this->_data;
return $this->data;
}

/**
Expand All @@ -138,8 +152,13 @@ public function data(): string
*/
public function string(): string
{
return "-----BEGIN {$this->_type}-----\n" .
trim(chunk_split(base64_encode($this->_data), 64, "\n")) . "\n" .
"-----END {$this->_type}-----";
return sprintf(
"-----BEGIN %s-----\n' .
'%s\n' .
'-----END %s-----",
$this->type,
trim(chunk_split(base64_encode($this->data), 64, "\n")),
$this->type
);
}
}
Loading