我在我的Django项目中使用Hashids(http://hashids.org/python/)。
我想创建固定长度的散列。
但是Hashids只支持min_length
hash_id = Hashids(
salt=os.environ.get("SALT"),
min_length=10,
)如何设置hash_id的固定长度(比如10个字符)?
发布于 2016-10-13 14:40:58
虽然我没有使用python版本的库,但我仍然觉得我可以回答,因为我维护的是.NET版本,而且它们大多共享相同的算法。
仅从逻辑上考虑这一点,固定散列的长度(或设置最大长度)与允许用户定义字母表和盐相结合,限制了散列的可能变化,因此还限制了可以编码的数字。
我猜这就是为什么今天的库不可能做到这一点。
发布于 2019-06-06 19:06:24
您可以很容易地设置hashid的min_length,但是设置max_length会变得更加棘手,因为这需要在传递的整数上设置minimum length。请避免在生产环境中这样做,因为这可能会对您的系统产生负面影响。下面的示例代码说明了如果使用不同的语言,如何设置min_length for PHP ,请根据您使用的语言检查hashid实现。
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>";发布于 2018-09-17 20:36:42
你可以在Hashids中设置"min_length“
例如:
hashids = Hashids(min_length=16, salt="my salt")
hashid = hashids.encode(1) # '4q2VolejRejNmGQB'有关更多详细信息,请单击here
https://stackoverflow.com/questions/39938876
复制相似问题