forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrand.go
36 lines (30 loc) · 954 Bytes
/
rand.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package kbfscrypto
import (
"crypto/rand"
"github.com/pkg/errors"
)
// UnexpectedShortCryptoRandRead indicates that fewer bytes were read
// from crypto.rand.Read() than expected.
type UnexpectedShortCryptoRandRead struct {
}
// Error implements the error interface for UnexpectedShortRandRead.
func (e UnexpectedShortCryptoRandRead) Error() string {
return "Unexpected short read from crypto.rand.Read()"
}
// RandRead is a belt-and-suspenders wrapper around
// crypto.rand.Read().
func RandRead(buf []byte) error {
n, err := rand.Read(buf)
if err != nil {
return errors.WithStack(err)
}
// This is truly unexpected, as rand.Read() is supposed to
// return an error on a short read already!
if n != len(buf) {
return errors.WithStack(UnexpectedShortCryptoRandRead{})
}
return nil
}