package utils import ( "crypto/hmac" "crypto/sha256" "encoding/hex" "errors" "math/rand" "regexp" ) var phoneRegex = regexp.MustCompile(`^09\d{9}$`) // ValidatePhone فقط شماره موبایل ایرانی با فرمت 09XXXXXXXXX را قبول می‌کند func ValidatePhone(phone string) error { if !phoneRegex.MatchString(phone) { return errors.New("شماره موبایل باید ۱۱ رقم باشد و با 09 شروع شود") } return nil } // GenerateOTP یک کد عددی تصادفی با طول مشخص می‌سازد func GenerateOTP(length int) string { digits := "0123456789" b := make([]byte, length) for i := range b { b[i] = digits[rand.Intn(len(digits))] } return string(b) } // HashOTP کد OTP را با HMAC-SHA256 هش می‌کند تا در دیتابیس به‌صورت خام ذخیره نشود func HashOTP(otp, secretKey string) string { h := hmac.New(sha256.New, []byte(secretKey)) h.Write([]byte(otp)) return hex.EncodeToString(h.Sum(nil)) }