<?php

namespace CookieSession\Encrypter;

class Encrypter extends AbstractEncrypter
{

    /**
     * @param $string
     *
     * @return string
     */
    public function encrypt($string)
    {
        return openssl_encrypt($string, $this->method, $this->secret, true, $this->iv);
    }


    /**
     * @param $string
     *
     * @return string
     * @throws \Exception
     */
    public function decrypt($string)
    {
        $result = openssl_decrypt($string, $this->method, $this->secret, true, $this->iv);

        if(false === $result)
        {
            throw new \Exception("Error occurred while decrypting. Error reported: ". openssl_error_string());
        }

        return $result;
    }


    /**
     * @param $string
     *
     * @return mixed
     * @throws \Exception
     */
    public function sign($string)
    {
        $result = openssl_sign($string, $signature, $this->getPrivateKey(), $this->signature_algo);

        if(false === $result)
        {
            throw new \Exception("Unable to sign the given string. Error reported: ". openssl_error_string());
        }

        return $signature;
    }


    /**
     * @param $data
     * @param $signature
     *
     * @return bool
     * @throws \Exception
     */
    public function verify($data, $signature)
    {
        $result = openssl_verify($data, $signature, $this->getPublicKey(), $this->signature_algo);

        if(-1 === $result)
        {
            throw new \Exception("Unable to verify the signature. Error reported: ". openssl_error_string());
        }

        return 1 === $result;
    }

    /**
     * @param $string
     *
     * @return mixed
     * @throws \Exception
     */
    public function encryptPublic($string)
    {
        $result = openssl_public_encrypt($string, $encrypted, $this->getPublicKey());

        if(false === $result)
        {
            throw new \Exception("Unable to encrypt using public key. Error reported: ". openssl_error_string());
        }

        return $encrypted;
    }


    /**
     * @param $string
     *
     * @return mixed
     * @throws \Exception
     */
    public function decryptPublic($string)
    {
        $result = openssl_public_decrypt($string, $decrypted, $this->getPublicKey());

        if(false === $result)
        {
            throw new \Exception("Unable to decrypt using public key. Error reported: ". openssl_error_string());
        }

        return $decrypted;
    }


    /**
     * @param $string
     *
     * @return mixed
     * @throws \Exception
     */
    public function encryptPrivate($string)
    {
        $result = openssl_private_encrypt($string, $encrypted, $this->getPrivateKey());

        if(false === $result)
        {
            throw new \Exception("Unable to encrypt using private key. Error reported: ". openssl_error_string());
        }

        return $encrypted;
    }


    /**
     * @param $string
     *
     * @return mixed
     * @throws \Exception
     */
    public function decryptPrivate($string)
    {
        $result = openssl_private_decrypt($string, $decrypted, $this->getPrivateKey());

        if(false === $result)
        {
            throw new \Exception("Unable to decrypt using private key. Error reported: ". openssl_error_string());
        }

        return $decrypted;
    }
}