31 lines
910 B
Go
31 lines
910 B
Go
package keymgr
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"math/big"
|
|
)
|
|
|
|
// charset avoids visually ambiguous characters (0/O, 1/l/I) since this
|
|
// password is normally read aloud or typed by hand on the recipient side.
|
|
const passwordCharset = "23456789ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz"
|
|
|
|
// GenerateBatchPassword returns a cryptographically random password shared
|
|
// by every archive produced in one export batch. It is shown to the admin
|
|
// exactly once (see the export result screen) and must be relayed to each
|
|
// recipient over a channel separate from the archive itself.
|
|
func GenerateBatchPassword(length int) (string, error) {
|
|
if length <= 0 {
|
|
length = 20
|
|
}
|
|
out := make([]byte, length)
|
|
max := big.NewInt(int64(len(passwordCharset)))
|
|
for i := range out {
|
|
n, err := rand.Int(rand.Reader, max)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
out[i] = passwordCharset[n.Int64()]
|
|
}
|
|
return string(out), nil
|
|
}
|