Hashids是一个非常小巧的跨语言的开源库,它用来把数字编码成一个随机字符串。它不同于md5这种算法这种单向映射,Hashids除了编码还会解码。 我们来看看它怎么使用 首先安装一下 pip install hashids 我们看到hashids不仅可以编码一个整数,还可以一次编码多个整数。 现在我们试试随便提供字符串,对它进行解码会怎样 我们看到这些字符串都是非法,所以hashids无法解码出对应的整数。 鉴于hashids是如此的小巧,笔者随后对它的源代码进行了一番研究。首先看看它的构造器 我们发现可以设置编码后字符串的最小长度。 算法的作者不保证安全性,不建议将hashids用在安全领域。
本文再推荐一款唯一 id 生成器:Hashids。它具有以下特性: 只能把指定的整数(且不支持负数)转换为唯一 id,具有一定的局限性。 不会发生冲突。 另外,对应源码短小精悍,且提供了多种版本的实现,包括但不限于JavaScript、Java、C等语言,有兴趣的同学可以到官网 hashids.org 学习使用。
composer require hashids/hashids 然后你可以将类导入到你的应用程序中: use Hashids\Hashids; $hashids = new Hashids(); 快速示例 use Hashids\Hashids; $hashids = new Hashids(); $id = $hashids->encode(1, 2, 3); // o2fXhV $numbers ID 唯一: use Hashids\Hashids; $hashids = new Hashids('My Project'); $hashids->encode(1, 2, 3); // Z4UrtW use Hashids\Hashids; $hashids = new Hashids(); // 无填充 $hashids->encode(1); // jR $hashids = new Hashids (即使你只编码了一个数字): use Hashids\Hashids; $hashids = new Hashids(); $id = $hashids->encode(1); $hashids-
,如图: 然后,在打开的NuGet程序包管理界面输入关键字Hashids.net,在搜索到的结果中选中Hashids.net类库组件并安装,如图: Hashids.net的使用 导入Hashids.net var Hashids = new Hashids("this is my salt"); var hash = hashids.Encode(12345); 运行结果为:NkK9 如果要转换一个Int64 new Hashids("this is my salt"); numbers = hashids.Decode("NkK9"); 运行结果为:[ 12345 ] var hashids = new ("NkK9"); 运行结果为:[] 编码多个数字 var hashids = new Hashids("this is my salt"); var hash = hashids.Encode(683 (1); // => NV hashids.Encode(2); // => 6m hashids.Encode(3); // => yD hashids.Encode(4); // => 2l hashids.Encode
hashids_t; void hashids_free(hashids_t *hashids); hashids_t * hashids_init(const char *salt); size_t hashids_encode(hashids_t *hashids, char *buffer, size_t numbers_count, unsigned long long *numbers ); size_t hashids_decode(hashids_t *hashids, const char *str, unsigned long long *numbers, size_t numbers_max); size_t hashids_estimate_encoded_size(hashids_t *hashids, size_t numbers_count, unsigned long long *numbers); ]] local id = 123456789 local C = ffi.load("hashids") local hashids = C.hashids_init
在这里,通过编写一个自定义JsonConverter来实现: public class HashIdJsonConverter : JsonConverter<int> { Hashids hashids = new Hashids("愚公公众号");//加盐 public override int Read(ref Utf8JsonReader reader, Type typeToConvert hashids = new Hashids("公众号My IO");//加盐 public Task BindModelAsync(ModelBindingContext bindingContext = new Hashids("愚公公众号", minHashLength: 6); return hashids.Encode(id); } 短链接跳转接口: [HttpGet("{shortUrl }")] public IActionResult GetUrl(string shortUrl) { var hashids = new Hashids("公众号My IO", minHashLength
created_time` datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) 这样insert数据的时候,id会自增,获取到这个自增的整数值,然后使用hashids 这个库将这个id转换为一个短字符串: https://hashids.org/python/ 将这个短字符串作为短网址域名的路径就可以使用了。 腾讯云scf如何使用第三方库 由于使用到了hashids这个第三方库,就需要将这个库也打包一起上传到腾讯云,这里需要注意的要点就是安装hashids库的命令: pip install hashids - 将依赖库安装在云函数的代码所在的目录,这样在使用scf命令打包上传时,依赖库也会被一起上传 直接贴代码 # -*- coding: utf8 -*- from os import getenv from hashids import Hashids import json import pymysql from pymysql.err import OperationalError mysql_conn = None
官网地址:https://hashids.org/ 支持多语言,包很小,使用也非常简单。 下面给大家分享在 Go 中使用的。 首先:import "github.com/speps/go-hashids" // 加密 func Encrypt(salt string, minLength int, params []int) string { hd := hashids.NewData() hd.Salt = salt hd.MinLength = minLength h, err := hashids.NewWithData return e } } return "" } // 解密 func Decrypt(salt string, minLength int, hash string) []int { hd := hashids.NewData () hd.Salt = salt hd.MinLength = minLength h, err := hashids.NewWithData(hd) if err == nil { e,
主要需要注意的内容有: 腾讯云函数的API调用中如何读取path中的参数 腾讯云函数中的集成响应和透传响应的区别 从请求的短链接获取真实url 如前文所属,我们生成的短链接是使用hashids库从整数id 生成的,所以获取真实url的过程也非常简单,只需要调用hashids的decode方法就可以获取到原始的整数id,然后用这个作为参数查询数据库获取到真实url 直接贴代码 # -*- coding: utf8 -*- from os import getenv from hashids import Hashids import json from serverless_db_sdk import database requestContext"] # 云函数的API调用中的path参数是这么读取的 hash = event['pathParameters']['hash'] hashid = Hashids
今天给大家推荐的工具是hashids。该工具可以将一个正整数转换成长度较短、唯一且不连续的ID值。一般适用于生成用户ID,但又不想用有规律的ID的场景。 该hashids包的原理也是一样,是基于62进制进行转换的。 该包特点: 对非负整数都可以生成唯一短id 可以设置不同的盐,具有保密性 递增的输入产生的输出无法预测 代码较短,且不依赖于第三方库 基本使用 hd := hashids.NewData() hd.Salt = "my salt" h, _ := hashids.NewWithData(hd) id, _ := h.Encode([]int{1}) //只要这里的ID或salt值不一样 ,最终的id就不一样 fmt.Println(id) // 最终输出 OL 设置生成最短ID的位数,如下设置最小长度是8位: hd := hashids.NewData() hd.Salt
有点类似之前介绍的 · Hashids· 生成的短链 使用Hashids来保护你的Dotnet应用程序 功能特性 • 无冲突(Collision-free): 生成的 ID 是唯一的,并能解码回原始数字
官方文档: https://matplotlib.org/contents.html# 密码学 cryptography 官方文档: https://cryptography.io/en/latest/ hashids 官方文档: https://pypi.org/project/hashids/ paramiko 官方文档: http://www.paramiko.org/ passlib 官方文档: https:
实用工具箱 -> Hashids ? 程序中对传递 id 的场景,均使用密文进行传递。 实用工具箱 -> 调用日志 ? 展示最新的 100 条调用日志。 实用工具箱 -> 接口文档 ?
Hashids requires either theBC MathorGMPextension in order to work.
我用了hashids这个库,将int类型的id转换成了一个hashids,好事者猜不到这个字符串,也就无法遍历我的文章了。
另外一个比较常用的加密递增ID方法是hashid,它可以转换数字比如347为字符串yr8,并且还可以反解出来,提供了很多语言的实现,比如go-hashids、hashids-java、hashids.c
应用场景分别为:用户的唯一标识(文章加密id防止爬虫)、生成有规律的code、登录密码的加密 使用hashids加密demo(用户的唯一标识、文章加密id防止爬虫) (链接:https://juejin.im
Localization.NuoCheView.NeedInputTip)); return; } try { var encodedPhone = new Hashids 管理用户输入和UI状态 二维码生成:调用核心生成逻辑并显示结果 文件保存:将生成的二维码保存到用户指定位置 拖拽支持:允许用户直接拖拽二维码图像到其他应用 错误处理:提供友好的错误提示 值得一提的是,我们使用了Hashids string.IsNullOrWhiteSpace(P)) { _decodePhone = new Hashids("codewf").DecodeLong(P
比如:https://example.com/Lqj8a0 先前它叫做Hashids, 但是由于商标问题,我不得不更改名称。
此外,生成的 ID 最好别直接用,不然别人可以反解出其中的数据,比如你有多少台服务器等等,解决办法是在应用层用 hashids 编码及解码,如此一来,数据库里保存的还是原始的 ID(Bigint),但是用户看到的却是