2019-08-06 11:44:08 +00:00
|
|
|
// Package random holds a few functions for working with random numbers
|
|
|
|
package random
|
|
|
|
|
2019-08-25 07:39:31 +00:00
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"math/rand"
|
2019-08-06 11:44:08 +00:00
|
|
|
|
2019-08-25 07:39:31 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// String create a random string for test purposes.
|
|
|
|
//
|
|
|
|
// Do not use these for passwords.
|
2019-08-06 11:44:08 +00:00
|
|
|
func String(n int) string {
|
|
|
|
const (
|
|
|
|
vowel = "aeiou"
|
|
|
|
consonant = "bcdfghjklmnpqrstvwxyz"
|
|
|
|
digit = "0123456789"
|
|
|
|
)
|
|
|
|
pattern := []string{consonant, vowel, consonant, vowel, consonant, vowel, consonant, digit}
|
|
|
|
out := make([]byte, n)
|
|
|
|
p := 0
|
|
|
|
for i := range out {
|
|
|
|
source := pattern[p]
|
|
|
|
p = (p + 1) % len(pattern)
|
|
|
|
out[i] = source[rand.Intn(len(source))]
|
|
|
|
}
|
|
|
|
return string(out)
|
|
|
|
}
|
2019-08-25 07:39:31 +00:00
|
|
|
|
|
|
|
// Password creates a crypto strong password which is just about
|
|
|
|
// memorable. The password is composed of printable ASCII characters
|
|
|
|
// from the base64 alphabet.
|
|
|
|
//
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 00:17:24 +00:00
|
|
|
// Requires password strength in bits.
|
2019-08-25 07:39:31 +00:00
|
|
|
// 64 is just about memorable
|
|
|
|
// 128 is secure
|
|
|
|
func Password(bits int) (password string, err error) {
|
|
|
|
bytes := bits / 8
|
|
|
|
if bits%8 != 0 {
|
|
|
|
bytes++
|
|
|
|
}
|
|
|
|
var pw = make([]byte, bytes)
|
|
|
|
n, err := rand.Read(pw)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "password read failed")
|
|
|
|
}
|
|
|
|
if n != bytes {
|
|
|
|
return "", errors.Errorf("password short read: %d", n)
|
|
|
|
}
|
|
|
|
password = base64.RawURLEncoding.EncodeToString(pw)
|
|
|
|
return password, nil
|
|
|
|
}
|