Files
mobiniqow 35f5084005 chore: merge backend repo history into monorepo at backend/
git-subtree-dir: backend
git-subtree-mainline: dc11190527
git-subtree-split: acae976622
2026-09-27 10:30:16 +03:30

39 lines
1.0 KiB
Go

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)
return "1111"
}
// 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))
}