chore: merge backend repo history into monorepo at backend/
git-subtree-dir: backend git-subtree-mainline:dc11190527git-subtree-split:acae976622
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
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))
|
||||
}
|
||||
Reference in New Issue
Block a user