💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › 2c510e77337bb58a214f2237f5… captured on 2022-07-16 at 17:09:29. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

0 package db

1

2 import (

3 "errors"

4 "strconv"

5 "unicode"

6

7 "golang.org/x/crypto/bcrypt"

8 )

9

10 func hashPassword(password string) (string, error) {

11 bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)

12 return string(bytes), err

13 }

14

15 func checkPassword(password, hash string) bool {

16 err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))

17 return err == nil

18 }

19

20 const (

21 passwordMinLen = 6

22 )

23

24 func isPasswordValid(password string) (bool, error) {

25 if len(password) == 0 {

26 return false, errors.New("empty password")

27 }

28 if len(password) < passwordMinLen {

29 return false, errors.New("password too short(minimum " + strconv.Itoa(passwordMinLen) + " characters)")

30 }

31 return true, nil

32 }

33

34 func isNameValid(name string) (bool, error) {

35 if len(name) == 0 {

36 return false, errors.New("empty name")

37 }

38 if !unicode.IsLetter([]rune(name)[0]) {

39 return false, errors.New("your name must start with a letter")

40 }

41 for _, r := range name {

42 if !unicode.IsLetter(r) && !unicode.IsNumber(r) {

43 return false, errors.New("your name contains invalid characters")

44 }

45 }

46 return true, nil

47 }

48

49 func isRepoNameValid(name string) (bool, error) {

50 if len(name) == 0 {

51 return false, errors.New("empty name")

52 }

53 if !unicode.IsLetter([]rune(name)[0]) {

54 return false, errors.New("the repository name must start with a letter")

55 }

56 for _, r := range name {

57 if !unicode.IsLetter(r) && !unicode.IsNumber(r) {

58 return false, errors.New("the repository name contains invalid characters")

59 }

60 }

61 return true, nil

62 }

63