fusenapi/utils/auth/confirmation_link_test.go

165 lines
3.6 KiB
Go
Raw Normal View History

2023-07-27 02:18:49 +00:00
package auth
import (
2023-07-28 04:15:56 +00:00
"crypto/aes"
"crypto/cipher"
"encoding/hex"
2023-07-27 02:18:49 +00:00
"log"
"net/url"
"testing"
"time"
"github.com/golang-jwt/jwt"
)
func BenchmarkConfirmationLink(b *testing.B) {
type Register struct {
Id int64
Password string
platform string
Expired time.Time
}
key := "21321321"
2023-07-28 04:15:56 +00:00
cl := NewConfirmationLink[Register](key, "http://localhost:9900/api/auth/oauth2/register")
2023-07-27 02:18:49 +00:00
for i := 0; i < b.N; i++ {
2023-08-24 10:28:01 +00:00
uri, _ := cl.Generate(&Register{Id: 39, Password: "21dsadsad", platform: "google", Expired: time.Now().UTC()})
2023-07-27 02:18:49 +00:00
u, _ := url.Parse(uri)
token := u.Query()["token"]
cl.Decrypt(token[0])
}
}
2023-07-28 04:15:56 +00:00
func BenchmarkAesXor(b *testing.B) {
// 生成一个256位的密钥
key := []byte("abcdefghijklmnopqrstuvwxyz123456")
// // 初始向量IV必须是唯一的,但不需要保密
iv := []byte("1234567890123456")
// // 要加密的明文
plaintext := []byte("hello world sadsadsadssadadsada ")
// // 使用AES加密,返回一个Block接口
// block, err := aes.NewCipher(key)
// if err != nil {
// panic(err)
// }
// // 使用CTR模式
// stream := cipher.NewCTR(block, iv)
// // 加密明文
// ciphertext := make([]byte, len(plaintext))
// stream.XORKeyStream(ciphertext, plaintext)
// // 转为hex编码打印出来
// // fmt.Println(hex.EncodeToString(ciphertext))
// ciphertextHex := hex.EncodeToString(ciphertext)
// // 将hex解码成[]byte
// ciphertext, _ = hex.DecodeString(ciphertextHex)
// // 生成Block接口
// block, err = aes.NewCipher(key)
// if err != nil {
// panic(err)
// }
// // 生成CTR模式
// stream = cipher.NewCTR(block, iv)
// // 解密密文
// plaintext = make([]byte, len(ciphertext))
// stream.XORKeyStream(plaintext, ciphertext)
// // 打印明文
// fmt.Println(string(plaintext))
for i := 0; i < b.N; i++ {
// 使用AES加密,返回一个Block接口
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// 使用CTR模式
stream := cipher.NewCTR(block, iv)
// 加密明文
ciphertext := make([]byte, len(plaintext))
stream.XORKeyStream(ciphertext, plaintext)
// 转为hex编码打印出来
// fmt.Println(hex.EncodeToString(ciphertext))
ciphertextHex := hex.EncodeToString(ciphertext)
// 将hex解码成[]byte
ciphertext, _ = hex.DecodeString(ciphertextHex)
// 生成Block接口
block, err = aes.NewCipher(key)
if err != nil {
panic(err)
}
// 生成CTR模式
stream = cipher.NewCTR(block, iv)
// 解密密文
plaintext = make([]byte, len(ciphertext))
stream.XORKeyStream(plaintext, ciphertext)
}
}
2023-07-27 02:18:49 +00:00
func TestConfirmationLink(t *testing.T) {
type Register struct {
Id int64
Password string
platform string
Expired time.Time
}
key := "21321321"
2023-07-28 04:15:56 +00:00
cl := NewConfirmationLink[Register](key, "http://localhost:9900/api/auth/oauth2/register")
2023-08-24 10:28:01 +00:00
uri, _ := cl.Generate(&Register{Id: 39, Password: "21dsadsad", platform: "google", Expired: time.Now().UTC()})
2023-07-27 02:18:49 +00:00
log.Println(uri)
u, _ := url.Parse(uri)
token := u.Query()["token"]
log.Println(cl.Decrypt(token[0]))
}
const secret = "your-256-bit-secret"
func BenchmarkJWT(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
claims := &jwt.StandardClaims{
ExpiresAt: time.Now().Unix() + 1020213021,
Issuer: "test",
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
ss, err := token.SignedString([]byte(secret))
if err != nil {
b.Fatal(err)
}
_, err = jwt.Parse(ss, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, jwt.ErrSignatureInvalid
}
return []byte(secret), nil
})
if err != nil {
b.Fatal(err)
}
}
}