-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrapi.go
64 lines (51 loc) · 1.53 KB
/
rapi.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
package restic
import (
"fmt"
"io"
"os"
"github.com/rubiojr/rapi/crypto"
)
func (id *ID) DirectoryPrefix() string {
return id.String()[:2]
}
// DecryptAndCheck decrypts the blob contents, optionally checking if the content
// is valid.
func (blob *Blob) DecryptAndCheck(reader io.ReaderAt, key *crypto.Key, check bool) ([]byte, error) {
// load blob from pack
buf := make([]byte, blob.Length)
n, err := reader.ReadAt(buf, int64(blob.Offset))
if err != nil {
return nil, err
}
if uint(n) != blob.Length {
return nil, fmt.Errorf("error loading blob %v: wrong length returned, want %d, got %d",
blob.ID.Str(), blob.Length, uint(n))
}
// decrypt
nonce, ciphertext := buf[:key.NonceSize()], buf[key.NonceSize():]
plaintext, err := key.Open(ciphertext[:0], nonce, ciphertext, nil)
if err != nil {
return nil, fmt.Errorf("decrypting blob %v failed: %v", blob.ID, err)
}
if check && !Hash(plaintext).Equal(blob.ID) {
return nil, fmt.Errorf("blob %v returned invalid hash", blob.ID)
}
return plaintext, nil
}
// DecryptAndCheck decrypts the blob contents.
//
// Does not check content validity.
func (blob *Blob) Decrypt(reader io.ReaderAt, key *crypto.Key) ([]byte, error) {
return blob.DecryptAndCheck(reader, key, false)
}
// DecryptAndCheck decrypts the blob contents.
//
// Does not check content validity.
func (blob *Blob) DecryptFromPack(path string, key *crypto.Key) ([]byte, error) {
pack, err := os.Open(path)
if err != nil {
return nil, err
}
defer pack.Close()
return blob.DecryptAndCheck(pack, key, false)
}