This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
/
nokey_k.go
99 lines (83 loc) · 1.95 KB
/
nokey_k.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package filecrypt
import (
"errors"
"fmt"
"os"
)
// Implements No Key functionality
// Header size : 2 Bytes
// Format
// version [1 Byte]
// type [1 Byte]
type NoKeyFc struct {
version int
keytype int
keyIn []byte
keyOut []byte
}
// Hdr format
const (
FC_NOKEYHDR_END_OFFSET = 2
FC_NOKEYHDR_NPARAMS = 2
)
// Init Hdr Struct
func (hdr *NoKeyFc) fillHdr(KeyIn []byte, params ...int) error {
if len(params) != FC_NOKEYHDR_NPARAMS {
return fmt.Errorf("fillHdr : Incorrect arguments")
}
Version := params[0]
Keytype := params[1]
// check errors
if Version >= FC_HDR_NVERSION ||
Keytype >= FC_KEY_NTYPE {
return errors.New("Invalid arguments")
}
hdr.version = Version
hdr.keytype = Keytype
hdr.keyIn = nil
hdr.keyOut = nil
return nil
}
// from bytes to Hdr struct
func (hdr *NoKeyFc) fromBytes(hdrBytes []byte) {
hdr.version = int(hdrBytes[FC_HDR_VERSION_OFFSET])
hdr.keytype = int(hdrBytes[FC_HDR_FCTYPE_OFFSET])
hdr.keyIn = nil
hdr.keyOut = nil
}
// From HDR struct to bytes
func (hdr NoKeyFc) toBytes() ([]byte, error) {
header := make([]byte, FC_NOKEYHDR_END_OFFSET)
header[FC_HDR_VERSION_OFFSET] = byte(hdr.version)
header[FC_HDR_FCTYPE_OFFSET] = byte(hdr.keytype)
return header, nil
}
func (hdr *NoKeyFc) retrieveKey(keyIn, d []byte) ([]byte, error) {
return nil, nil
}
func (hdr *NoKeyFc) retrieveKHdr(d []byte, f *os.File) ([]byte, error) {
return d, nil
}
func (hdr *NoKeyFc) generateKey(fname string) ([]byte, error) {
// in Tx mode, out key is not available
if hdr.keyOut == nil {
// Generate key
fhdr, err := hdr.toBytes()
if err != nil {
return nil, fmt.Errorf("toBytes : %w", err)
}
// Open file
file, err := openFileA(fname)
if err != nil {
return nil, fmt.Errorf("Open file : %w", err)
}
defer file.Close()
// write header to file
_, err = file.Write(fhdr)
if err != nil {
return nil, fmt.Errorf("Write file : %w", err)
}
hdr.keyOut = make([]byte, 1)
}
return nil, nil
}