共计 1423 个字符,预计需要花费 4 分钟才能阅读完成。
签名验证是为了保障接口平安和辨认调用方身份,同时还须要满足以下几点:
- 可变性:每次的签名必须是不一样的。
- 时效性:每次申请的时效性,过期作废。
- 唯一性:每次的签名是惟一的。
- 完整性:可能对传入数据进行验证,避免篡改。
签名规定大同小异,依据本人的业务状况进行制订即可。
签名过程中咱们会用到的几种算法,接下来分享一下每个算法的基准测试,可能会存在误差,供大家参考。
MD5 单向散列加密
func BenchmarkEncrypt(b *testing.B) {b.ResetTimer() | |
for i := 0; i < b.N; i++ {New().Encrypt("123456") | |
} | |
} | |
// 输入 | |
goos: darwin | |
goarch: amd64 | |
pkg: github.com/xinliangnote/go-gin-api/pkg/md5 | |
BenchmarkEncrypt-12 10000000 238 ns/op | |
PASS |
AES 对称加密
func BenchmarkEncryptAndDecrypt(b *testing.B) {b.ResetTimer() | |
aes := New(key, iv) | |
for i := 0; i < b.N; i++ {encryptString, _ := aes.Encrypt("123456") | |
aes.Decrypt(encryptString) | |
} | |
} | |
// 输入 | |
goos: darwin | |
goarch: amd64 | |
pkg: github.com/xinliangnote/go-gin-api/pkg/aes | |
BenchmarkEncryptAndDecrypt-12 1000000 1009 ns/op | |
PASS |
RSA 非对称加密
func BenchmarkEncryptAndDecrypt(b *testing.B) {b.ResetTimer() | |
rsaPublic := NewPublic(publicKey) | |
rsaPrivate := NewPrivate(privateKey) | |
for i := 0; i < b.N; i++ {encryptString, _ := rsaPublic.Encrypt("123456") | |
rsaPrivate.Decrypt(encryptString) | |
} | |
} | |
// 输入 | |
goos: darwin | |
goarch: amd64 | |
pkg: github.com/xinliangnote/go-gin-api/pkg/rsa | |
BenchmarkEncryptAndDecrypt-12 1000 1345384 ns/op | |
PASS |
最初
JWT 的签名验证也应用过,分享一下 JWT
的基准测试,应用的是 jwt.SigningMethodHS256
办法。
func BenchmarkSignAndParse(b *testing.B) {b.ResetTimer() | |
token := New(secret) | |
for i := 0; i < b.N; i++ {tokenString, _ := token.Sign(123456789, "xinliangnote") | |
token.Parse(tokenString) | |
} | |
} | |
// 输入 | |
goos: darwin | |
goarch: amd64 | |
pkg: github.com/xinliangnote/go-gin-api/pkg/token | |
BenchmarkSignAndParse-12 200000 11749 ns/op | |
PASS |
以上代码在 go-gin-api
我的项目中,地址:https://github.com/xinliangno…
正文完