mirror of
https://github.com/yuaotian/go-cursor-help.git
synced 2025-06-08 04:22:06 +08:00
feat: enhance GenerateMachineID to include auth0|user_ prefix
- Updated the GenerateMachineID function to generate a machine ID with the prefix "auth0|user_". - The random part of the ID is now 25 bytes, producing a 50-character hexadecimal string. - This change improves the uniqueness and format of generated machine IDs, aligning with authentication standards.
This commit is contained in:
parent
12468bc363
commit
15d8210bd4
@ -35,10 +35,25 @@ func (g *Generator) generateRandomHex(length int) (string, error) {
|
|||||||
// Public methods
|
// Public methods
|
||||||
// -------------
|
// -------------
|
||||||
|
|
||||||
// GenerateMachineID generates a new 32-byte machine ID
|
// GenerateMachineID generates a new machine ID with auth0|user_ prefix
|
||||||
func (g *Generator) GenerateMachineID() (string, error) {
|
func (g *Generator) GenerateMachineID() (string, error) {
|
||||||
g.simulateWork()
|
g.simulateWork()
|
||||||
return g.generateRandomHex(32)
|
|
||||||
|
// 生成随机部分 (25字节,将产生50个十六进制字符)
|
||||||
|
randomPart, err := g.generateRandomHex(25)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建完整的ID: "auth0|user_" + random
|
||||||
|
prefix := "auth0|user_"
|
||||||
|
fullID := fmt.Sprintf("%x%x%s",
|
||||||
|
[]byte(prefix), // 转换前缀为十六进制
|
||||||
|
[]byte("0"), // 添加一个字符
|
||||||
|
randomPart, // 随机部分
|
||||||
|
)
|
||||||
|
|
||||||
|
return fullID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateMacMachineID generates a new 64-byte MAC machine ID
|
// GenerateMacMachineID generates a new 64-byte MAC machine ID
|
||||||
|
47
pkg/idgen/generator_test.go
Normal file
47
pkg/idgen/generator_test.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package idgen
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGenerateMachineID(t *testing.T) {
|
||||||
|
g := NewGenerator()
|
||||||
|
|
||||||
|
fmt.Println("\n=== 开始测试 MachineID 生成 ===")
|
||||||
|
|
||||||
|
// 运行多次测试以确保一致性
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
id, err := g.GenerateMachineID()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("生成ID时发生错误: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("\n第 %d 次测试:\n", i+1)
|
||||||
|
fmt.Printf("生成的 ID: %s\n", id)
|
||||||
|
fmt.Printf("ID 长度: %d\n", len(id))
|
||||||
|
fmt.Printf("前缀部分: %s\n", id[:20])
|
||||||
|
fmt.Printf("随机部分: %s\n", id[20:])
|
||||||
|
|
||||||
|
// 测试1: 验证总长度
|
||||||
|
if len(id) != 74 {
|
||||||
|
t.Errorf("ID长度不正确. 期望: 74, 实际: %d", len(id))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试2: 验证前缀
|
||||||
|
expectedPrefix := "61757468307c757365725f" // "auth0|user_" 的十六进制
|
||||||
|
if !strings.HasPrefix(id, expectedPrefix) {
|
||||||
|
t.Errorf("ID前缀不正确.\n期望前缀: %s\n实际ID: %s", expectedPrefix, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试3: 验证十六进制格式
|
||||||
|
for _, c := range id {
|
||||||
|
if !strings.ContainsRune("0123456789abcdef", c) {
|
||||||
|
t.Errorf("ID包含非十六进制字符: %c", c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("\n=== 测试完成 ===")
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user