-
Notifications
You must be signed in to change notification settings - Fork 0
/
randgen_methods.go
68 lines (60 loc) · 1.26 KB
/
randgen_methods.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package randgen
import (
"bytes"
"errors"
"io"
"os"
"runtime"
"github.com/shirou/gopsutil/v4/mem"
)
var (
errVerificationFailed = errors.New("the data integrity check failed")
)
func WriteRand(w io.Writer, size int, secure bool) error {
reader, err := NewRandReader(size, secure)
if err != nil {
return err
}
if _, err = io.Copy(w, reader); err != nil {
return err
}
return nil
}
func CreateFile(file string, size int, secure bool) error {
dst, err := os.Create(file)
if err != nil {
return err
}
defer dst.Close()
return WriteRand(dst, size, secure)
}
func VerifyFile(file string) (string, error) {
src, err := os.Open(file)
if err != nil {
return "", err
}
defer src.Close()
return Verify(src)
}
func GetData(size int, secure bool) ([]byte, error) {
if dataLimit > 0 && size > dataLimit {
return nil, errDataLimitExceeded
}
virtMem, err := mem.VirtualMemory()
if err != nil {
return nil, err
}
usable := virtMem.Available / uint64(divFactorPerCPU*runtime.NumCPU())
if size > int(usable) {
return nil, errDataLimitExceeded
}
reader, err := NewRandReader(size, secure)
if err != nil {
return nil, err
}
return io.ReadAll(reader)
}
func VerifyData(data []byte) (string, error) {
reader := bytes.NewReader(data)
return Verify(reader)
}