add password generator

Signed-off-by: Christian Richter <crichter@owncloud.com>
This commit is contained in:
Christian Richter
2022-04-27 10:58:27 +02:00
parent d106c87c51
commit c5d0791f53
3 changed files with 46 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
package generators_test
import (
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestGenerators(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Generators Suite")
}

View File

@@ -0,0 +1,13 @@
package generators_test
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/owncloud/ocis/ocis-pkg/generators"
)
var _ = Describe("Generators", func() {
It("Returns an error ", func() {})
PIt("Returns expected passwords", func() {})
})

View File

@@ -0,0 +1,20 @@
package generators
import (
"crypto/rand"
"math/big"
)
func GenerateRandomPassword(length int) (string, error) {
const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-=+!@#$%^&*."
ret := make([]byte, length)
for i := 0; i < length; i++ {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(chars))))
if err != nil {
return "", err
}
ret[i] = chars[num.Int64()]
}
return string(ret), nil
}