gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

length.go (2321B)


      1 package passwordvalidator
      2 
      3 const (
      4 	seqNums      = "0123456789"
      5 	seqKeyboard0 = "qwertyuiop"
      6 	seqKeyboard1 = "asdfghjkl"
      7 	seqKeyboard2 = "zxcvbnm"
      8 	seqAlphabet  = "abcdefghijklmnopqrstuvwxyz"
      9 )
     10 
     11 func removeMoreThanTwoFromSequence(s, seq string) string {
     12 	seqRunes := []rune(seq)
     13 	runes := []rune(s)
     14 	matches := 0
     15 	for i := 0; i < len(runes); i++ {
     16 		for j := 0; j < len(seqRunes); j++ {
     17 			if i >= len(runes) {
     18 				break
     19 			}
     20 			r := runes[i]
     21 			r2 := seqRunes[j]
     22 			if r != r2 {
     23 				matches = 0
     24 				continue
     25 			}
     26 			// found a match, advance the counter
     27 			matches++
     28 			if matches > 2 {
     29 				runes = deleteRuneAt(runes, i)
     30 			} else {
     31 				i++
     32 			}
     33 		}
     34 	}
     35 	return string(runes)
     36 }
     37 
     38 func deleteRuneAt(runes []rune, i int) []rune {
     39 	if i >= len(runes) ||
     40 		i < 0 {
     41 		return runes
     42 	}
     43 	copy(runes[i:], runes[i+1:])
     44 	runes[len(runes)-1] = 0
     45 	runes = runes[:len(runes)-1]
     46 	return runes
     47 }
     48 
     49 func getReversedString(s string) string {
     50 	n := 0
     51 	rune := make([]rune, len(s))
     52 	for _, r := range s {
     53 		rune[n] = r
     54 		n++
     55 	}
     56 	rune = rune[0:n]
     57 	// Reverse
     58 	for i := 0; i < n/2; i++ {
     59 		rune[i], rune[n-1-i] = rune[n-1-i], rune[i]
     60 	}
     61 	// Convert back to UTF-8.
     62 	return string(rune)
     63 }
     64 
     65 func removeMoreThanTwoRepeatingChars(s string) string {
     66 	var prevPrev rune
     67 	var prev rune
     68 	runes := []rune(s)
     69 	for i := 0; i < len(runes); i++ {
     70 		r := runes[i]
     71 		if r == prev && r == prevPrev {
     72 			runes = deleteRuneAt(runes, i)
     73 			i--
     74 		}
     75 		prevPrev = prev
     76 		prev = r
     77 	}
     78 	return string(runes)
     79 }
     80 
     81 func getLength(password string) int {
     82 	password = removeMoreThanTwoRepeatingChars(password)
     83 	password = removeMoreThanTwoFromSequence(password, seqNums)
     84 	password = removeMoreThanTwoFromSequence(password, seqKeyboard0)
     85 	password = removeMoreThanTwoFromSequence(password, seqKeyboard1)
     86 	password = removeMoreThanTwoFromSequence(password, seqKeyboard2)
     87 	password = removeMoreThanTwoFromSequence(password, seqAlphabet)
     88 	password = removeMoreThanTwoFromSequence(password, getReversedString(seqNums))
     89 	password = removeMoreThanTwoFromSequence(password, getReversedString(seqKeyboard0))
     90 	password = removeMoreThanTwoFromSequence(password, getReversedString(seqKeyboard1))
     91 	password = removeMoreThanTwoFromSequence(password, getReversedString(seqKeyboard2))
     92 	password = removeMoreThanTwoFromSequence(password, getReversedString(seqAlphabet))
     93 	return len(password)
     94 }