Skip to content

Commit

Permalink
Merge pull request #113 from negz/dieutildie
Browse files Browse the repository at this point in the history
Remove pkg/util
  • Loading branch information
negz authored Feb 1, 2020
2 parents fa31e7a + d31744b commit a6bb086
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 176 deletions.
57 changes: 57 additions & 0 deletions pkg/password/password.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2019 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package password contains a simple password generator.
package password

import (
"crypto/rand"
"math/big"
)

// Settings for password generation.
type Settings struct {
// CharacterSet of allowed password characters.
CharacterSet string

// Length of generated passwords.
Length int
}

// Default password generation settings.
var Default = Settings{
CharacterSet: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
Length: 27,
}

// Generate a random, 27 character password that may consist of lowercase
// letters, uppercase letters, or numbers.
func Generate() (string, error) {
return Default.Generate()
}

// Generate a password.
func (s Settings) Generate() (string, error) {
pw := make([]byte, s.Length)
for i := 0; i < s.Length; i++ {
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(s.CharacterSet))))
if err != nil {
return "", err
}
pw[i] = s.CharacterSet[n.Int64()]
}
return string(pw), nil
}
20 changes: 20 additions & 0 deletions pkg/password/password_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package password

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestGenerate(t *testing.T) {
// ¯\_(ツ)_/¯

want := "aaa"
got, err := Settings{CharacterSet: "a", Length: 3}.Generate()
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Generate(): -want, +got:\n%s", diff)
}
if err != nil {
t.Errorf("Generate: %s\n", err)
}
}
19 changes: 0 additions & 19 deletions pkg/util/doc.go

This file was deleted.

57 changes: 0 additions & 57 deletions pkg/util/strings.go

This file was deleted.

100 changes: 0 additions & 100 deletions pkg/util/strings_test.go

This file was deleted.

0 comments on commit a6bb086

Please sign in to comment.