tourze/tls-key-format

TLS密钥格式处理工具,支持PEM/DER格式转换

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/tourze/tls-key-format

0.0.1 2025-05-24 17:24 UTC

This package is auto-updated.

Last update: 2025-10-31 07:51:42 UTC


README

PHP Version License Build Status Coverage Status

English | 中文

A PHP library for handling TLS key format conversions between PEM and DER formats.

Features

  • Convert between PEM and DER formats
  • Validate PEM and DER format data
  • Extract key and certificate information from PEM files
  • Handle certificates and private keys
  • Comprehensive error handling with exceptions

Installation

composer require tourze/tls-key-format

Usage

Basic Usage

<?php
use Tourze\TLSKeyFormat\PemDerFormat;
use Tourze\TLSKeyFormat\KeyHandler;
use Tourze\TLSKeyFormat\CertificateHandler;

// Initialize the format handler
$formatter = new PemDerFormat();

// Convert PEM to DER
$pemData = file_get_contents('certificate.pem');
$derData = $formatter->pemToDer($pemData);

// Convert DER to PEM
$pemData = $formatter->derToPem($derData, 'CERTIFICATE');

// Validate formats
if ($formatter->isValidPem($pemData)) {
    echo "Valid PEM format\n";
}

if ($formatter->isValidDer($derData)) {
    echo "Valid DER format\n";
}

// Extract information from PEM
$info = $formatter->extractFromPem($pemData);
echo "Type: " . $info['type'] . "\n";

Key Handling

<?php
use Tourze\TLSKeyFormat\KeyHandler;

$keyHandler = new KeyHandler();

// Generate a new key pair
$keyPair = $keyHandler->generateKeyPair();

// Work with keys
$privateKey = $keyPair['private'];
$publicKey = $keyPair['public'];

Certificate Handling

<?php
use Tourze\TLSKeyFormat\CertificateHandler;

$certHandler = new CertificateHandler();

// Parse certificate
$certInfo = $certHandler->parseCertificate($pemData);

// Validate certificate
if ($certHandler->validateCertificate($pemData)) {
    echo "Certificate is valid\n";
}

Configuration

No configuration is required. The library works out of the box with default settings.

Examples

Complete Example

<?php
require_once 'vendor/autoload.php';

use Tourze\TLSKeyFormat\PemDerFormat;
use Tourze\TLSKeyFormat\Exception\KeyFormatException;

try {
    $formatter = new PemDerFormat();
    
    // Load a PEM certificate
    $pemData = file_get_contents('example.pem');
    
    // Convert to DER
    $derData = $formatter->pemToDer($pemData);
    
    // Convert back to PEM
    $pemResult = $formatter->derToPem($derData, 'CERTIFICATE');
    
    // Extract information
    $info = $formatter->extractFromPem($pemData);
    
    echo "Certificate type: " . $info['type'] . "\n";
    echo "Data length: " . strlen($info['data']) . " bytes\n";
    
} catch (KeyFormatException $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

References

License

This project is licensed under the MIT License - see the LICENSE file for details.