-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencrypt.go
81 lines (71 loc) · 2.21 KB
/
encrypt.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 main
import (
"archive/zip"
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/yoshi389111/git-caesar/caesar"
"github.com/yoshi389111/git-caesar/caesar/aes"
"github.com/yoshi389111/git-caesar/iolib"
)
func encrypt(peerPubKeys []caesar.PublicKey, prvKey caesar.PrivateKey, plaintext []byte) ([]byte, error) {
// generate shared key (for AES-256-CBC)
shareKey := make([]byte, 32)
_, err := rand.Read(shareKey)
if err != nil {
return nil, fmt.Errorf("Failed to get random number (shared key).\n\t%w", err)
}
// encrypt shared key per public key
var envelopes []interface{}
for _, peerPubKey := range peerPubKeys {
envelope, err := peerPubKey.NewEnvelope(shareKey)
if err != nil {
return nil, fmt.Errorf("Failed to create envelope.\n\t%w", err)
}
envelopes = append(envelopes, envelope)
}
// encrypt the plaintext (by AES-256-CBC)
ciphertext, err := aes.Encrypt(shareKey, plaintext)
if err != nil {
return nil, fmt.Errorf("AES encryption failed.\n\t%w", err)
}
// sign the ciphertext
sig, err := prvKey.Sign(ciphertext)
if err != nil {
return nil, fmt.Errorf("Failed to sign.\n\t%w", err)
}
// create `caesar.json`
selfAuthKey, err := prvKey.GetAuthKey()
if err != nil {
return nil, fmt.Errorf("The authentication key could not be obtained from the private key during encryption.\n\t%w", err)
}
caesarJson := &CaesarJson{
Version: "1",
Signature: base64.StdEncoding.EncodeToString(sig),
Signer: selfAuthKey,
Envelopes: envelopes,
}
jsonBytes, err := json.Marshal(caesarJson)
if err != nil {
return nil, fmt.Errorf("Failed to marshal `caesar.json`.\n\t%w", err)
}
// create zip data
zipBuf := new(bytes.Buffer)
zipWriter := zip.NewWriter(zipBuf)
err = iolib.AppendZieEntry(zipWriter, CAESAR_JSON, jsonBytes)
if err != nil {
return nil, fmt.Errorf("Failed to add `caesar.json` entry to ZIP file.\n\t%w", err)
}
err = iolib.AppendZieEntry(zipWriter, CAESAR_CIPHER, ciphertext)
if err != nil {
return nil, fmt.Errorf("Failed to add `caesar.cipher` entry to ZIP file.\n\t%w", err)
}
err = zipWriter.Close()
if err != nil {
return nil, fmt.Errorf("Failed to create ZIP file.\n\t%w", err)
}
zipBytes := zipBuf.Bytes()
return zipBytes, nil
}