forked from tobischo/gokeepasslib
/
signature.go
62 lines (50 loc) · 1.57 KB
/
signature.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
package gokeepasslib
import (
"encoding/binary"
"errors"
"fmt"
"io"
)
//BaseSignature is the valid base signature for kdbx files
var BaseSignature = [...]byte{0x03, 0xd9, 0xa2, 0x9a}
//VersionSignature is the valid version signature for kdbx files
var VersionSignature = [...]byte{0x67, 0xfb, 0x4b, 0xb5}
//FileVersion is the most recent valid file version signature for kdbx files
var FileVersion = [...]byte{0x01, 0x00, 0x03, 0x00}
//A full valid default signature struct for new databases
var DefaultSig = FileSignature{BaseSignature, VersionSignature, FileVersion}
// FileSignature holds the Keepass File Signature.
// The first 4 Bytes are the Base Signature,
// followed by 4 Bytes for the Version of the Format
// which is followed by 4 Bytes for the File Version
type FileSignature struct {
BaseSignature [4]byte
VersionSignature [4]byte
FileVersion [4]byte
}
func (s FileSignature) String() string {
return fmt.Sprintf("Base: %x, Version: %x, FileVersion: %x",
s.BaseSignature,
s.VersionSignature,
s.FileVersion,
)
}
func ReadSignature(r io.Reader) (*FileSignature, error) {
sig := new(FileSignature)
if err := binary.Read(r, binary.LittleEndian, sig); err != nil {
return nil, err
}
if sig.BaseSignature != BaseSignature {
return nil, errors.New("BaseSignature not valid")
}
if sig.VersionSignature != VersionSignature {
return nil, errors.New("VersionSignature not valid")
}
return sig, nil
}
func (s *FileSignature) WriteSignature(w io.Writer) error {
if err := binary.Write(w, binary.LittleEndian, s); err != nil {
return err
}
return nil
}