<?php

namespace Buckhill\License\tests\units\Encryption;

use Buckhill\License\Model\EncryptedLicense;
use Buckhill\License\Model\Seal;
use mageekguy\atoum;
use Buckhill\License\Model\License;
use Buckhill\License\Encryption\Decrypter as Decrypt;

class Decrypter extends atoum
{
    public function testCreateFromPublickey()
    {
        $this
            ->given($decrypter = Decrypt::createFromString($this->getPublicKeyString()))
            ->then
                ->object($decrypter)->isInstanceOf(Decrypt::class)
            ;

        $decrypter->unseal($this->getSealedString());
    }

    public function testUnsealAndDecrypt()
    {
        $this
            ->given($decrypter = Decrypt::createFromString($this->getPublicKeyString()))
            ->and($seal = $this->getSealedString())
            ->then
                ->object($decrypter)->isInstanceOf(Decrypt::class)
                ->object($license = $decrypter->unseal($seal))->isInstanceOf(EncryptedLicense::class)
                ->object($license->decrypt())->isInstanceOf(License::class)
            ;

    }



    /**
     * @return string
     */
    protected function getSealedString()
    {
        return '0ljmNUvWAoNB2ANKl7OYSIXi1I07BwxzvLTyRcrc8vTEZCrjVyT9KzVxzuQaBWdYPMGnA7LyHWbz4+YSFiMp4DpbOQLxYmgUxqk/fPzOn9cDQCCF5JkOUNw5kMIy/Pm8lZjcO4nYpa0twYEFZR83PofD/OOAH9+gU4evAfbbRo4LNkpemIV3BTXu0KWniVliPre6DGBVK6u2yXmx56+CrBPDC8R1FNNBxh189eLZZRgJzqW3rDIg712xWh31AEaQy9hE4mRGHb05A8uc7eWweSp0x2zRC+q40ytcEtn9DnTgtXteMnavisV5WC7FuBYE/mfI2BZBmC1pLG6Ulenb6Q==.uGKxVjvFnm+VGG+zresNba4V6Xo8sy/h4IMV4G062+aCd0BqdW3IckwfL36mtFPal4cRzaQWX4n7SERnkSKZzYy2m3JDbWXMe8Z5LguRXbYQ/uQlhNIPjBddF+kzCUCnqgfNC+IiWca3oTguzebgO2LAg6J149h9HDU77Xozy1rkeb8kmyZeep88C+RfJPnu6DN8JtWiaW6XKK/99t4Ga5MxVE0KA/a8AF+zlTsINXbMIJzAEIR72QjZJwK882yDYhDxq7yZBVKFQZEYcgTuUbHURu2mqdASOz7uSSSwzazdcD9499Guf+fIFo/6jKsBzZgqw8QDCeaWg5627Y47hLSE5TNQe7fM6fdbs8+0W5aBjj1/SjhN2w3goIeywtAoO8gGKwlzvRKbKqlKP/UKXYmpdxMUHFtVJdPDw73OUjz+cPyvjK6dr96n/z7//U8JtDqDcCpHnDovu9gNR+2K7J76bRQYLjQGZgEVFeU3DMT/AYfs+Xqo14j5377b7yOO+avmU6WlFXYCmHsd3QSQGKNjV62HxDCI6DcdBnYF9vm4qYJyHtSabXOPgFNcAqIfvokCoIc2/hnVEWiJP+z0C7bkDG0vitHm5QJrsvy5QOmYiXiP1EapZ9I58e7pjM+ENz7ZVGy4CE1ss28e.iMtT2W62Btfa04dXcENwsZSlmmOoHRVE0xOkVAewggA';
    }

    /**
     * @return mixed
     */
    protected function getPublicKeyString()
    {
        $key = @openssl_pkey_get_private(file_get_contents('.keys/testing.pem'), '1234');

        $details = @openssl_pkey_get_details($key);

        return $details['key'];
    }
}
