97 lines
1.4 KiB
Go
97 lines
1.4 KiB
Go
|
package utils
|
||
|
|
||
|
import (
|
||
|
"strings"
|
||
|
|
||
|
"github.com/tjarratt/babble"
|
||
|
)
|
||
|
|
||
|
type Passphrase string
|
||
|
|
||
|
func NewPassphrase() Passphrase {
|
||
|
var phrase string
|
||
|
var retries int
|
||
|
var word string
|
||
|
var words int = 3
|
||
|
|
||
|
for words > 0 {
|
||
|
retries = 20
|
||
|
for {
|
||
|
word = getCleanWord()
|
||
|
if isAccecpable(word) {
|
||
|
phrase = phrase + word + " "
|
||
|
break
|
||
|
}
|
||
|
if retries == 1 { //this is our last try, we take any word
|
||
|
phrase = phrase + word + " "
|
||
|
}
|
||
|
retries -= 1
|
||
|
}
|
||
|
words -= 1
|
||
|
}
|
||
|
return Passphrase(strings.TrimSpace(phrase))
|
||
|
}
|
||
|
|
||
|
func (p Passphrase) String() string {
|
||
|
return string(p)
|
||
|
}
|
||
|
|
||
|
func isAccecpable(s string) bool {
|
||
|
l := len(s)
|
||
|
if l > 8 || l < 3 {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
for _, rune := range s {
|
||
|
if !isEnglishLetter(rune) {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
func isProfanity(s string) bool {
|
||
|
contains := []string{
|
||
|
"nigg",
|
||
|
"fagg",
|
||
|
}
|
||
|
startsWith := []string{
|
||
|
"spic",
|
||
|
"chin",
|
||
|
"cunt",
|
||
|
}
|
||
|
|
||
|
for _, word := range contains {
|
||
|
if strings.Contains(s, word) {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
for _, word := range startsWith {
|
||
|
if strings.HasPrefix(s, word) {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func isEnglishLetter(r rune) bool {
|
||
|
if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {
|
||
|
return false
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
func getCleanWord() string {
|
||
|
var word string
|
||
|
|
||
|
babbler := babble.NewBabbler()
|
||
|
babbler.Count = 1
|
||
|
|
||
|
for {
|
||
|
word = babbler.Babble()
|
||
|
if !isProfanity(word) {
|
||
|
return word
|
||
|
}
|
||
|
}
|
||
|
}
|