From c5d0791f53feb638599c07b46b26f16da9aaa61d Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Wed, 27 Apr 2022 10:58:27 +0200 Subject: [PATCH] add password generator Signed-off-by: Christian Richter --- ocis-pkg/generators/generators_suite_test.go | 13 +++++++++++++ ocis-pkg/generators/generators_test.go | 13 +++++++++++++ ocis-pkg/generators/password.go | 20 ++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 ocis-pkg/generators/generators_suite_test.go create mode 100644 ocis-pkg/generators/generators_test.go create mode 100644 ocis-pkg/generators/password.go diff --git a/ocis-pkg/generators/generators_suite_test.go b/ocis-pkg/generators/generators_suite_test.go new file mode 100644 index 000000000..ef690d593 --- /dev/null +++ b/ocis-pkg/generators/generators_suite_test.go @@ -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") +} diff --git a/ocis-pkg/generators/generators_test.go b/ocis-pkg/generators/generators_test.go new file mode 100644 index 000000000..4d89d5943 --- /dev/null +++ b/ocis-pkg/generators/generators_test.go @@ -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() {}) +}) diff --git a/ocis-pkg/generators/password.go b/ocis-pkg/generators/password.go new file mode 100644 index 000000000..3c2d571fa --- /dev/null +++ b/ocis-pkg/generators/password.go @@ -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 +}