2FA: Add generator for random 16-digit app passwords #808

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2023-11-27 19:43:53 +01:00
parent f48ff16ef5
commit ab491c3032
4 changed files with 35 additions and 25 deletions

14
pkg/rnd/passcode.go Normal file
View file

@ -0,0 +1,14 @@
package rnd
// GeneratePasscode returns a random 16-digit passcode that can, for example, be used as an app password.
// It is separated by 3 dashes for better readability, resulting in a total length of 19 characters.
func GeneratePasscode() string {
code := make([]byte, 0, 19)
code = append(code, Base62(4)...)
for n := 0; n < 3; n++ {
code = append(code, "-"+Base62(4)...)
}
return string(code)
}

21
pkg/rnd/passcode_test.go Normal file
View file

@ -0,0 +1,21 @@
package rnd
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGeneratePasscode(t *testing.T) {
for n := 0; n < 10; n++ {
code := GeneratePasscode()
t.Logf("Passcode %d: %s", n, code)
assert.Equal(t, 19, len(code))
}
}
func BenchmarkGeneratePasscode(b *testing.B) {
for n := 0; n < b.N; n++ {
GeneratePasscode()
}
}

View file

@ -1,6 +0,0 @@
package rnd
// GeneratePasswd returns a random password with 8 characters as string.
func GeneratePasswd() string {
return GenerateToken(8)
}

View file

@ -1,19 +0,0 @@
package rnd
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGeneratePasswd(t *testing.T) {
pw := GeneratePasswd()
t.Logf("password: %s", pw)
assert.Equal(t, 8, len(pw))
}
func BenchmarkGeneratePasswd(b *testing.B) {
for n := 0; n < b.N; n++ {
GeneratePasswd()
}
}