forked from michaljemala/cf-service-broker
/
password.go
39 lines (32 loc) · 857 Bytes
/
password.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright 2014, The cf-service-broker Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
package broker
import (
"crypto/rand"
"encoding/base64"
"errors"
"io"
)
type PasswordGenerator interface {
GeneratePassword() (string, error)
}
var RandomPasswordGenerator = &randomGenerator{16, base64.URLEncoding}
type randomGenerator struct {
strength int
encoding *base64.Encoding
}
func (g *randomGenerator) randomKey() []byte {
k := make([]byte, g.strength)
if _, err := io.ReadFull(rand.Reader, k); err != nil {
return nil
}
return k
}
func (g *randomGenerator) GeneratePassword() (string, error) {
if key := g.randomKey(); key == nil {
return "", errors.New("Failed to generate random key.")
} else {
return g.encoding.EncodeToString(key), nil
}
}