<?php

/*
 * This file is part of the AuthStack project.
 *
 * (c) Buckhill Ltd <development@buckhill.co.uk>
 *
 * This source file is subject to the Buckhill license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Buckhill\License;

use Buckhill\License\Model\License;
use Buckhill\License\Encryption\Decrypter;
use Buckhill\License\Encryption\Encrypter;

class LicenseManager
{
    /**
     * @param array $model
     * @param string $alg
     * @return License
     */
    public function generate(array $model, $alg = 'AES-256-CTR')
    {
        return License::generate($model, $alg);
    }

    /**
     * @param array $model
     * @return License
     */
    public function load(array $model)
    {
        return License::load($model);
    }

    /**
     * @param License $license
     * @param $privateKey
     * @param string $passphrase
     * @return string
     */
    public function seal(License $license, $privateKey, $passphrase = '')
    {
        $encrypter  = Encrypter::createFromString($privateKey, $passphrase);
        $encLicense = $license->encrypt(true);

        return $encrypter->seal($encLicense);
    }

    /**
     * @param $sealed
     * @param $publicKey
     * @return License
     */
    public function unseal($sealed, $publicKey)
    {
        $decrypter = Decrypter::createFromString($publicKey);

        $encLicense = $decrypter->unseal($sealed);

        return $encLicense->decrypt();
    }
}