使用包哈希,我可以从数字中获取哈希(并编码y解码)
var Hashids = require("hashids"),
hashids = new Hashids("this is my salt", 8);
var id = hashids.encode(1);
Run Code Online (Sandbox Code Playgroud)
存在一些类似的包来从字符串获取哈希?(带编码/解码)
我有一组ASCII字符串,假设它们是文件路径。它们既可以很短也可以很长。
我正在寻找一种可以计算此类字符串的哈希的算法,该哈希也将是字符串,但具有固定的长度,例如youtube video id:
https://www.youtube.com/watch?v=-F-3E8pyjFo
^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
MD5似乎是我所需要的,但是对我来说,拥有短的哈希字符串至关重要。
是否有可以执行此操作的shell命令或python库?
我正在从一个数据库中获取一组任务作为一个雄辩的集合,然后我将该集合发送到我的视图,我在那里做了一个foreach.这里没问题.除此之外,我需要id在我的视图中引用任务(网址操作等).但我显然不希望这个在源代码中,所以我使用这个库来散列id.但在视图中这样做似乎是错误的.
有没有办法在模型或控制器中散列id?
这是我在控制器中调用集合的方式:
$tasks = Auth::user()->tasks()->orderBy('created_at', 'desc')->get();
Run Code Online (Sandbox Code Playgroud)
这就是我目前在我的视图中对id进行哈希的方法:
<a href="{{ route('tasks.markascompleted', Hashids::encode($task->id)) }}">
Run Code Online (Sandbox Code Playgroud)
有帮助吗?
我在我的项目中使用Hashids(http://hashids.org/python/)Django。
我想创建固定长度的哈希。
但Hashids仅支持min_length:
hash_id = Hashids(
salt=os.environ.get("SALT"),
min_length=10,
)
Run Code Online (Sandbox Code Playgroud)
如何设置 的固定长度(例如 10 个字符)hash_id?
运行https://www.npmjs.com/package/hashids教程中的代码
<script type="text/javascript" src="js/hashids.min.js"></script>
<script type="text/javascript">
var hashids = new Hashids();
console.log(hashids.encode(1));
</script>
Run Code Online (Sandbox Code Playgroud)
得到错误:Uncaught TypeError: Hashids is not a constructor.
我在一个没有模块的普通 js 应用程序中运行它。只是 html 和 js 文件。如果我使用reqire(hasids),则会弹出错误:require is not defined。我需要在我的应用程序上运行它,该应用程序是用常规浏览器 JavaScript 编码的。请帮我运行这个。谢谢你!
我为我的ID制作了一个自定义类型:
type ID uint
func (id ID) MarshalJSON() ([]byte, error) {
e, _ := HashIDs.Encode([]int{int(id)})
fmt.Println(e) /// 34gj
return []byte(e), nil
}
func (id *ID) Scan(value interface{}) error {
*id = ID(value.(int64))
return nil
}
Run Code Online (Sandbox Code Playgroud)
我使用HashIDs包对我的id进行编码,以便用户无法在客户端读取它们.但是我收到了这个错误:
json:为类型types.ID调用MarshalJSON时出错:顶级值后无效字符'g'