|
|
|
|
@ -1,27 +1,39 @@
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/base62"
|
|
|
|
|
"golang.org/x/crypto/blake2b"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func NewPrivateId(prefix string) (string, error) {
|
|
|
|
|
return newId(prefix)
|
|
|
|
|
func NewPrivateId(prefix string, opt ...Option) (string, error) {
|
|
|
|
|
return newId(prefix, opt...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewPublicId creates a new public id with the prefix
|
|
|
|
|
func NewPublicId(prefix string) (string, error) {
|
|
|
|
|
return newId(prefix)
|
|
|
|
|
func NewPublicId(prefix string, opt ...Option) (string, error) {
|
|
|
|
|
return newId(prefix, opt...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newId(prefix string) (string, error) {
|
|
|
|
|
func newId(prefix string, opt ...Option) (string, error) {
|
|
|
|
|
const op = "db.newId"
|
|
|
|
|
if prefix == "" {
|
|
|
|
|
return "", errors.New(errors.InvalidParameter, op, "missing prefix")
|
|
|
|
|
}
|
|
|
|
|
publicId, err := base62.Random(10)
|
|
|
|
|
var publicId string
|
|
|
|
|
var err error
|
|
|
|
|
opts := GetOpts(opt...)
|
|
|
|
|
if len(opts.withPrngValues) > 0 {
|
|
|
|
|
sum := blake2b.Sum256([]byte(strings.Join(opts.withPrngValues, "|")))
|
|
|
|
|
reader := bytes.NewReader(sum[0:])
|
|
|
|
|
publicId, err = base62.RandomWithReader(10, reader)
|
|
|
|
|
} else {
|
|
|
|
|
publicId, err = base62.Random(10)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", errors.Wrap(err, op, errors.WithMsg("unable to generate id"), errors.WithCode(errors.Io))
|
|
|
|
|
}
|
|
|
|
|
|