mchess-server/utils/passphrase.go

58 lines
1019 B
Go
Raw Permalink Normal View History

2023-06-28 17:51:36 +00:00
package utils
import (
"strings"
"unicode"
2023-06-28 17:51:36 +00:00
)
type Passphrase string
func NewPassphrase() Passphrase {
var phrase string
var word string
var words = 3
2023-07-05 19:15:01 +00:00
//TODO make sure passphrases are unique
2023-06-28 17:51:36 +00:00
for words > 0 {
word = getCleanWord()
phrase = phrase + word + " "
2023-06-28 17:51:36 +00:00
words -= 1
}
return Passphrase(strings.TrimSpace(phrase))
}
func NewPassphraseFromString(s string) Passphrase {
return Passphrase(s)
}
func (p Passphrase) AsURLParam() string {
var result string
phraseAsString := p.String()
segments := strings.Split(phraseAsString, " ")
for _, segment := range segments {
runes := []rune(segment)
runes[0] = unicode.ToUpper(runes[0])
result += string(runes)
}
return result
}
func ConvertToPassphraseWithSpaces(s string) Passphrase {
result := ""
for _, rune := range s {
if unicode.IsUpper(rune) {
result += " "
}
result += strings.ToLower(string(rune))
}
return NewPassphraseFromString(strings.Trim(result, " "))
}
2023-06-28 17:51:36 +00:00
func (p Passphrase) String() string {
return string(p)
}