Files
arenax-server/utility/encrypt/phone.go

78 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utility
import (
"golang.org/x/crypto/bcrypt"
"regexp"
"strings"
)
// ValidatePhoneNumber 校验手机号格式是否为中国大陆合法手机号。
// 去除空格后匹配以 1 开头的 11 位数字,第二位为 3~9。
//
// 参数:
// - phone: 需要验证的手机号字符串。
//
// 返回:
// - true 表示格式合法false 表示格式不合法。
func ValidatePhoneNumber(phone string) bool {
phone = strings.ReplaceAll(phone, " ", "")
pattern := `^1[3-9]\d{9}$`
matched, err := regexp.MatchString(pattern, phone)
if err != nil {
return false
}
return matched
}
// EncryptPhoneNumber 加密手机号字符串。
// 使用 bcrypt 进行加密,适合需要不可逆存储的场景。
// ⚠️ 注意:加密结果无法解密,仅能做比对用途。
//
// 参数:
// - phone: 待加密的手机号(需合法)。
//
// 返回:
// - 加密后的哈希字符串;如果手机号无效或加密失败,返回空字符串。
func EncryptPhoneNumber(phone string) string {
if !ValidatePhoneNumber(phone) {
return ""
}
hashedPhone, err := bcrypt.GenerateFromPassword([]byte(phone), bcrypt.DefaultCost)
if err != nil {
return ""
}
return string(hashedPhone)
}
// MaskPhoneNumber 对手机号进行脱敏处理。
// 中间四位将被替换为星号,以保护用户隐私。
//
// 参数:
// - phone: 原始手机号字符串(需合法)。
//
// 返回:
// - 脱敏后的手机号,如 138****1234如果手机号无效则返回原始输入。
func MaskPhoneNumber(phone string) string {
if !ValidatePhoneNumber(phone) {
return phone
}
return phone[:3] + "****" + phone[7:]
}
// ComparePhoneNumber 比较明文手机号与加密后的哈希是否匹配。
// 用于验证用户输入的手机号是否与加密存储的值一致。
//
// 参数:
// - hashedPhone: 使用 bcrypt 加密后的手机号哈希。
// - phone: 用户输入的明文手机号(需合法)。
//
// 返回:
// - true 表示匹配成功false 表示不匹配或格式错误。
func ComparePhoneNumber(hashedPhone, phone string) bool {
if !ValidatePhoneNumber(phone) {
return false
}
err := bcrypt.CompareHashAndPassword([]byte(hashedPhone), []byte(phone))
return err == nil
}