feat: 为用户设备添加 DeviceNo 字段并实现 ID 混淆,同时在迁移脚本中新增 ID 重建逻辑。
Build docker and publish / build (20.15.1) (push) Has been cancelled
Build docker and publish / build (20.15.1) (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package tool
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const deviceHashSalt uint32 = 0x5A3C7E9B
|
||||
|
||||
// DeviceIdToHash encodes a device id to an 8-char uppercase hex string.
|
||||
// Algorithm mirrors frontend: id XOR salt → hex.
|
||||
// e.g. 1 → "5A3C7E9A", 42 → "5A3C7EA1"
|
||||
func DeviceIdToHash(id int64) string {
|
||||
return strings.ToUpper(fmt.Sprintf("%08x", uint32(id)^deviceHashSalt))
|
||||
}
|
||||
|
||||
// HashToDeviceId decodes an 8-char hex hash back to a device id.
|
||||
func HashToDeviceId(hash string) (int64, error) {
|
||||
var n uint32
|
||||
_, err := fmt.Sscanf(strings.ToLower(hash), "%08x", &n)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int64(n ^ deviceHashSalt), nil
|
||||
}
|
||||
Reference in New Issue
Block a user