<?php

namespace App\Utils;

class Hmac {
    private $key;
    private $basePath = __DIR__ . '/../../cache/img/';
    private $filePath;
    private $hashPath;

    public function __construct($key) {
        $this->key = $key;
    }

    public function makeHmac($url) {
        return hash_hmac('sha1', $url, $this->key);
    }

    private function makeHashPath($hashUrl) {
        $this->hashPath = substr($hashUrl, 0, 4) . '/';
        return $this->hashPath;
    }

    public function makeFilePath($hmac) {
        $this->filePath = $this->basePath . $this->makeHashPath($hmac);
        return $this->filePath;
    }

    public function checkHmac($receiveHmac, $url) {
        if ($receiveHmac === $this->makeHmac($url, $this->key)) {
            return true;
        } else {
            return false;
        }
    }
}