-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstructs.go
81 lines (70 loc) · 1.79 KB
/
structs.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
package seof
import (
"bytes"
"encoding/binary"
"errors"
"github.com/kuking/seof/crypto"
)
const HeaderMagic uint64 = 0xb0a713c
const HeaderLength int = 128
type Header struct {
Magic uint64
ScriptSalt [96]byte
ScriptN uint32
ScriptR uint32
ScriptP uint32
DiskBlockSize uint32
TailOfZeros [8]byte
}
func (h *Header) Verify() error {
if h.Magic != HeaderMagic {
return errors.New("header: invalid magic")
}
if h.DiskBlockSize < 1112 || h.DiskBlockSize > 196608 {
return errors.New("header: invalid disk_block_size")
}
for i := 0; i < len(h.ScriptSalt); i++ {
if h.ScriptSalt[i] != 0 {
break
}
if i == len(h.ScriptSalt)-1 {
return errors.New("header: zero salt")
}
}
if h.ScriptN > crypto.MaxSCryptParameters.N || h.ScriptN < crypto.MinSCryptParameters.N ||
h.ScriptR > crypto.MaxSCryptParameters.R || h.ScriptR < crypto.MinSCryptParameters.R ||
h.ScriptP > crypto.MaxSCryptParameters.P || h.ScriptP < crypto.MinSCryptParameters.P {
return errors.New("header: invalid scrypt parameters")
}
for i := 0; i < len(h.TailOfZeros); i++ {
if h.TailOfZeros[i] != 0 {
return errors.New("header: tail of zeros not correct")
}
}
return nil
}
type BlockEnvelop struct {
Nonce [nonceSize]byte
CipherTextLen uint32
CipherText []byte
}
type BlockZero struct {
BEncBlockSize uint32 //BEnc as 'Before Encryption'
DiskBlockSize uint32
BEncFileSize uint64 //reported file size
BlocksWritten uint64
}
func (z *BlockZero) Bytes() []byte {
buf := new(bytes.Buffer)
_ = binary.Write(buf, binary.LittleEndian, z)
return buf.Bytes()
}
func BlockZeroFromBytes(b []byte) (*BlockZero, error) {
r := bytes.NewReader(b)
bz := BlockZero{}
err := binary.Read(r, binary.LittleEndian, &bz)
if err != nil {
return nil, err
}
return &bz, nil
}