-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathrand.go
More file actions
37 lines (30 loc) · 953 Bytes
/
Copy pathrand.go
File metadata and controls
37 lines (30 loc) · 953 Bytes
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
37
// SPDX-FileCopyrightText: 2026 Thales Group and the crypto11 Contributors
// SPDX-FileCopyrightText: 2026 The Eclipse Foundation KeyPont project maintainers
// SPDX-License-Identifier: MIT
package crypto11
import (
"io"
)
// NewRandomReader returns a reader for the random number generator on the token.
func (c *Context) NewRandomReader() (io.Reader, error) {
if c.closed.Get() {
return nil, errClosed
}
return pkcs11RandReader{c}, nil
}
// pkcs11RandReader is a random number reader that uses PKCS#11.
type pkcs11RandReader struct {
context *Context
}
// This implements the Reader interface for pkcs11RandReader.
func (r pkcs11RandReader) Read(data []byte) (n int, err error) {
var result []byte
if err = r.context.withSession(func(session *pkcs11Session) error {
result, err = r.context.ctx.GenerateRandom(session.handle, len(data))
return err
}); err != nil {
return 0, err
}
copy(data, result)
return len(result), err
}