-1

I'm trying to hash some text and later-on use it as a parameter in a route.

I'm using the Hash facade to hash the text like the following

$hash = Illuminate\Support\Facades\Hash::make($text);

Then I'm passing it as param like this

//web.php
Route::get('profile/{$hashedText}/info', [ProfileController::class, 'info'])->name('profile.info');
//index.blade.php
route('profile.info', $hashedText);

The problem I'm facing is that Hash::make function doesn't always generate a URL friendly result(ie: existence of '/', '?=', '&'...)

I've noticed that the Hash::make function is not constant (if I run it twice with the same text I get different results) so I think I can loop through results till I get a good result.

Is there a good approach to overcome this?

K-Galalem
  • 300
  • 1
  • 10
  • Does that answer your question https://stackoverflow.com/questions/45759995/laravel-hashmake-alternative-for-url-parameters – Monnomcjo Jun 27 '22 at 14:02

1 Answers1

1

You could just do this:

$thing = strtr(base64_encode($string), '+/=', '._-');

This will base64 encode a string and replace any +, / or = with ., _ or -.

Can obviously add other substitutions.

Could combine it with Laravel hashing lib.

Brian
  • 8,418
  • 2
  • 25
  • 32
  • This was very helpful. Further-more, As Base64 encoded strings may contain only the characters **a-z A-Z 0-9 + / =** and the equal symbol '=' could never be preceded by '?' we can just use ```strtr(base64_encode(Hash::make($text)), '+/', '-_');``` – K-Galalem Jun 27 '22 at 14:35
  • Yeah, I replaced the = due to a specific API I was sending data to didn't like certain chars, but yeah what suites you're use-case :) – Brian Jun 27 '22 at 14:52