示例#1
0
func randInt(n int64) int64 {
	big, err := rand.Int(rand.Reader, big.NewInt(n))
	if err != nil {
		panic(err)
	}
	return big.Int64()
}
示例#2
0
func randomTLD() (string, error) {
	tlds := []string{"com", "org", "net"}
	big, err := rand.Int(rand.Reader, big.NewInt(int64(len(tlds))))
	if err != nil {
		return "", err
	}
	return tlds[big.Int64()], nil
}
示例#3
0
文件: idgen.go 项目: jfairbairn/gokv
func (idgen *IdGen) OnKey(key string) {
	results := keyRegex.FindAllStringSubmatch(key, 1)
	if len(results) != 1 {
		return
	}
	if len(results[0]) != 3 {
		return
	}
	prefix := results[0][1]

	big := big.NewInt(0)
	big.SetString(results[0][2], 10)

	id := uint64(big.Int64())
	oldid := idgen.ids[prefix]
	if id > oldid {
		idgen.ids[prefix] = id
	}
}
示例#4
0
// generate generates a new password.
func (obj *PasswordRes) generate() (string, error) {
	max := len(alphabet) - 1 // last index
	output := ""

	// FIXME: have someone verify this is cryptographically secure & correct
	for i := uint16(0); i < obj.Length; i++ {
		big, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
		if err != nil {
			return "", errwrap.Wrapf(err, "could not generate password")
		}
		ix := big.Int64()
		output += string(alphabet[ix])
	}

	if output == "" { // safety against empty passwords
		return "", fmt.Errorf("password is empty")
	}

	if uint16(len(output)) != obj.Length { // safety against weird bugs
		return "", fmt.Errorf("password length is too short") // bug!
	}

	return output, nil
}