2023-06-28 17:51:36 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2024-05-09 20:29:48 +00:00
|
|
|
"unicode"
|
2023-06-28 17:51:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Passphrase string
|
|
|
|
|
|
|
|
func NewPassphrase() Passphrase {
|
|
|
|
var phrase string
|
|
|
|
var word string
|
2024-04-30 21:55:27 +00:00
|
|
|
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 {
|
2024-04-30 21:55:27 +00:00
|
|
|
word = getCleanWord()
|
|
|
|
phrase = phrase + word + " "
|
2023-06-28 17:51:36 +00:00
|
|
|
words -= 1
|
|
|
|
}
|
|
|
|
return Passphrase(strings.TrimSpace(phrase))
|
|
|
|
}
|
|
|
|
|
2024-05-07 21:31:25 +00:00
|
|
|
func NewPassphraseFromString(s string) Passphrase {
|
|
|
|
return Passphrase(s)
|
|
|
|
}
|
|
|
|
|
2024-05-09 20:29:48 +00:00
|
|
|
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)
|
|
|
|
}
|