Вход Регистрация
Файл: concrete5.7.5.6/concrete/vendor/zendframework/zend-crypt/src/PublicKey/Rsa/PublicKey.php
Строк: 180
<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace ZendCryptPublicKeyRsa;

/**
 * RSA public key
 */
class PublicKey extends AbstractKey
{
    const 
CERT_START '-----BEGIN CERTIFICATE-----';

    
/**
     * @var string
     */
    
protected $certificateString null;

    
/**
     * Create public key instance public key from PEM formatted key file
     * or X.509 certificate file
     *
     * @param  string      $pemOrCertificateFile
     * @return PublicKey
     * @throws ExceptionInvalidArgumentException
     */
    
public static function fromFile($pemOrCertificateFile)
    {
        if (!
is_readable($pemOrCertificateFile)) {
            throw new 
ExceptionInvalidArgumentException(
                
"File '{$pemOrCertificateFile}' is not readable"
            
);
        }

        return new static(
file_get_contents($pemOrCertificateFile));
    }

    
/**
     * Construct public key with PEM formatted string or X.509 certificate
     *
     * @param  string $pemStringOrCertificate
     * @throws ExceptionRuntimeException
     */
    
public function __construct($pemStringOrCertificate)
    {
        
$result openssl_pkey_get_public($pemStringOrCertificate);
        if (
false === $result) {
            throw new 
ExceptionRuntimeException(
                
'Unable to load public key; openssl ' openssl_error_string()
            );
        }

        if (
strpos($pemStringOrCertificateself::CERT_START) !== false) {
            
$this->certificateString $pemStringOrCertificate;
        } else {
            
$this->pemString $pemStringOrCertificate;
        }

        
$this->opensslKeyResource $result;
        
$this->details            openssl_pkey_get_details($this->opensslKeyResource);
    }

    
/**
     * Encrypt using this key
     *
     * @param  string $data
     * @throws ExceptionInvalidArgumentException
     * @throws ExceptionRuntimeException
     * @return string
     */
    
public function encrypt($data)
    {
        if (empty(
$data)) {
            throw new 
ExceptionInvalidArgumentException('The data to encrypt cannot be empty');
        }

        
$encrypted '';
        
$result openssl_public_encrypt($data$encrypted$this->getOpensslKeyResource());
        if (
false === $result) {
            throw new 
ExceptionRuntimeException(
                
'Can not encrypt; openssl ' openssl_error_string()
            );
        }

        return 
$encrypted;
    }


    
/**
     * Decrypt using this key
     *
     * @param  string $data
     * @throws ExceptionInvalidArgumentException
     * @throws ExceptionRuntimeException
     * @return string
     */
    
public function decrypt($data)
    {
        if (!
is_string($data)) {
            throw new 
ExceptionInvalidArgumentException('The data to decrypt must be a string');
        }
        if (
'' === $data) {
            throw new 
ExceptionInvalidArgumentException('The data to decrypt cannot be empty');
        }

        
$decrypted '';
        
$result openssl_public_decrypt($data$decrypted$this->getOpensslKeyResource());
        if (
false === $result) {
            throw new 
ExceptionRuntimeException(
                
'Can not decrypt; openssl ' openssl_error_string()
            );
        }

        return 
$decrypted;
    }

    
/**
     * Get certificate string
     *
     * @return string
     */
    
public function getCertificate()
    {
        return 
$this->certificateString;
    }

    
/**
     * To string
     *
     * @return string
     * @throws ExceptionRuntimeException
     */
    
public function toString()
    {
        if (!empty(
$this->certificateString)) {
            return 
$this->certificateString;
        } elseif (!empty(
$this->pemString)) {
            return 
$this->pemString;
        }
        throw new 
ExceptionRuntimeException('No public key string representation is available');
    }
}
Онлайн: 3
Реклама