// Given a raw slice of bytes, construct a Vote // @@TODO: A custom splitter that checks for errors as it goes might be faster than splitting the whole thing and then looping the results to check for errors. func NewVote(rawVote []byte) (Vote, error) { if len(rawVote) > maxVoteSize { return Vote{}, ErrVoteTooBig } vote := Vote(strings.Split(string(rawVote), "\n")) if len(vote) > MaxVoteOptions { return Vote{}, errors.Wrapf(ErrVoteTooManyOptions, "A vote may have a maximum of %i option lines", MaxVoteOptions) } for i, voteItem := range vote { if len(voteItem) > MaxVoteBytes { return Vote{}, errors.Wrapf(ErrVoteOptionTooBig, "Vote item as position %i is too large. Each vote-item line may have a maximum of %i bytes", i, MaxVoteBytes) } } return vote, nil }
// Create a new PublicKey from a base64 encoded item, as we would get in a PUT or POST request // This function also performs error checking to make sure the key is valid. func NewPublicKey(base64PublicKey []byte) (PublicKey, error) { decodedLen := base64.StdEncoding.DecodedLen(len(base64PublicKey)) dbuf := make([]byte, decodedLen) n, err := base64.StdEncoding.Decode(dbuf, base64PublicKey) if err != nil { return nil, errors.Wrap(err, ErrPublicKeyBase64) } pk := PublicKey(dbuf[:n]) // Check the key length keylen, err := pk.KeyLength() if err != nil { return nil, err } if MinPublicKeySize < absoluteMinPublicKeySize { panic("MinPublicKeySize has been set less than the allowed absoluteMinPublicKeySize of 2048") } if keylen < MinPublicKeySize { return nil, errors.Wrapf(ErrPubicMinKeySize, "Please use at least %s bits for public-key", MinPublicKeySize) } return pk, nil }
func FmtWrappingFoo() error { return errors.Wrapf(ErrFoo, "%s", Strger) }