Quantcast
Channel: User Hamfri - Stack Overflow
Viewing all articles
Browse latest Browse all 42

Answer by Hamfri for How can I set max length of Hashids?

$
0
0

You can easily set the min_length of a hashid but it gets trickier setting the max_length as this would require setting a minimum length on the integer passed. Please avoid doing so in production environments as this could impact your system negatively. The sample code below indicates how to set min_length for PHP Laravel if using a different language kindly check for hashid implementation based on the language you are using.

namespace App\Hashing;use Hashids\Hashids;    class Hash {        private $salt_key;        private $min_length;        private $hashid;        public function __construct(){            $this->salt_key = '5OtYLj/PtkLOpQewWdEj+jklT+oMjlJY7=';            $this->min_length = 15;            $this->hashid = new Hashids($this->salt_key, $this->min_length);        }        public function encodeId($id){            $hashed_id = $this->hashid->encode($id);            return $hashed_id;        }        public function decodeId($hashed_id){            $id = $this->hashid->decode($hashed_id);            return $id;        }    }    $hash = new Hash();    $hashed_id = $hash->encodeId(1);    echo '<pre>';    print_r($hashed_id);    echo '</pre>';    echo "<pre>";    $id = $hash->decodeId($hashed_id);    print_r($id[0]);    echo "</pre>";

Viewing all articles
Browse latest Browse all 42

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>