h2non / imaginary

Fast, simple, scalable, Docker-ready HTTP microservice for high-level image processing
https://fly.io/docs/app-guides/run-a-global-image-service/
MIT License
5.55k stars 456 forks source link

Invalid URL signature :( #339

Closed arvai closed 3 years ago

arvai commented 3 years ago

Hello Guys, I'm trying to generate an url signature with PHP, with no success. Could you tell me what I'm doing wrong? :)

This is how I generate the Url path for my IMG tag, and the string for creating the signature: ` $operation = '/watermark';

$operation_config = 'text=xyz'. '&url='.urlencode($src).'';

$path = $operation . '?' .$operation_config;

$toSign = $operation . ''. $operation_config;

`

Then My Img tag's src looks like this: src="http://X.X.X.X:9000<?= $path ?>&sign=<?= signature($toSign) ?> And finally, my signature function is this: `

function signature($fullUrlPath = '')

{

    return base64_encode(hash_hmac('sha256', $fullUrlPath, 'mykey'));

}

`

And I get: {"message":"Invalid URL signature","status":400}

Thank you very much for your help :)

arvai commented 3 years ago

I've tried to set the 3rd param of hash_hmac to TRUE (raw_output) , but still no success

arvai commented 3 years ago

Ok, I just realized that base64 and base64url are not the same thing. So I'm using this function to create the signature:

https://base64.guru/developers/php/examples/base64url

But still doesn't work

arvai commented 3 years ago

I enabled the raw_output for the hash_hmac() again, and it works fine (with the previous base64url function)

altendorfme commented 1 year ago

Hi @arvai,

How did you resolve?

I have following code which always returns Invalid URL signature

<?php

function imaginary_signature()
{
    $signKey = "keykeykeykeykeykeykeymin32key";
    $urlPath = "/resize";
    $url = urlencode("https://raw.githubusercontent.com/h2non/imaginary/master/testdata/smart-crop.jpg");
    $urlQuery = "type=jpeg&url=" . $url . "&width=200";

    $h = hash_hmac("sha256", $urlPath . $urlQuery, $signKey, true);
    $buf = base64_encode($h);

    return $urlPath . "?" . $urlQuery . "&sign=" . rawurlencode($buf);
}

?>
<img src="http://localhost:8088<?php echo imaginary_signature(); ?>">