Initial commit: dating app backend (Go/Gin/GORM/Postgres)
Phone+OTP auth, staged onboarding, admin approval workflow, and a full discovery/swipe/match/chat system with photo upload and approximate distance (haversine) based on user location. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
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))
|
||||
}
|
||||
Reference in New Issue
Block a user