<?php

namespace Hura8\System\Security;

use Firebase\JWT\JWT;

class Encryption
{

    // list of algorithm supported in JWT::$supported_algs
    const JWT_ALG = 'HS256';


    public static function jwtDecode($jwt_code, $secret_key) {
        try {
            $decoded = (array) JWT::decode($jwt_code, $secret_key, array(Encryption::JWT_ALG));
            return (array) $decoded['data'];
        }catch (\Exception $exception) {
            // nothing
        }

        return false;
    }


    public static function jwtEncode(array $data, $secret_key, $expire_timestamp = 0) {
        $payload = array(
            "iss" => $_SERVER['HTTP_HOST'],
            "data" => $data,
            "iat" => time(),
        );

        if($expire_timestamp) $payload['exp'] = $expire_timestamp;

        return JWT::encode($payload, $secret_key, Encryption::JWT_ALG);
    }

}
