-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.go
56 lines (51 loc) · 1.3 KB
/
crypto.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
package main
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"log"
"github.com/pc-1827/distributed-file-system/crypto"
)
func GenerateCID(r io.Reader) string {
// Generate CID by hashing the data
hash := sha1.New()
if _, err := io.Copy(hash, r); err != nil {
log.Fatal(err)
return ""
}
return hex.EncodeToString(hash.Sum(nil))
}
func encrypt(src io.Reader, dst io.Writer) (int, error) {
if network.EncType == "AES" {
n, err := crypto.EncryptAES(network.EncKey, src, dst)
if err != nil {
return 0, fmt.Errorf("failed to encrypt data: %s", err)
}
return n, nil
} else if network.EncType == "CC20" {
//nonce := crypto.GenerateNonce()
n, err := crypto.ChaCha20Encrypt(network.EncKey, network.Nonce, src, dst)
if err != nil {
return 0, fmt.Errorf("failed to encrypt data: %s", err)
}
return n, nil
}
return 0, nil
}
func decrypt(src io.Reader, dst io.Writer) (int, error) {
if network.EncType == "AES" {
n, err := crypto.DecryptAES(network.EncKey, src, dst)
if err != nil {
return 0, fmt.Errorf("failed to decrypt data: %s", err)
}
return n, nil
} else if network.EncType == "CC20" {
n, err := crypto.ChaCha20Decrypt(network.EncKey, network.Nonce, src, dst)
if err != nil {
return 0, fmt.Errorf("failed to decrypt data: %s", err)
}
return n, nil
}
return 0, nil
}