forked from mutecomm/mute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.go
78 lines (74 loc) · 1.81 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
// Copyright (c) 2015 Mute Communications Ltd.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cryptengine
import (
"fmt"
"io"
"math"
"os"
"github.com/mutecomm/mute/cipher"
"github.com/mutecomm/mute/log"
"github.com/mutecomm/mute/msg"
"github.com/mutecomm/mute/uid/identity"
)
// encrypt reads data from r, encrypts it for identity to (with identity from
// as sender), and writes it to w.
func (ce *CryptEngine) encrypt(
w io.Writer,
from, to string,
sign bool,
nymAddress string,
r io.Reader,
statusfp *os.File,
) error {
// map pseudonyms
fromID, fromDomain, err := identity.MapPlus(from)
if err != nil {
return err
}
toID, err := identity.Map(to)
if err != nil {
return err
}
// get fromUID from keyDB
fromUID, _, err := ce.keyDB.GetPrivateUID(fromID, true)
if err != nil {
return err
}
// get toUID from keyDB
toUID, _, found, err := ce.keyDB.GetPublicUID(toID, math.MaxInt64) // TODO: use simpler API
if err != nil {
return err
}
if !found {
return log.Errorf("not UID for '%s' found", toID)
}
// encrypt message
senderLastKeychainHash, err := ce.keyDB.GetLastHashChainEntry(fromDomain)
if err != nil {
return err
}
var privateSigKey *[64]byte
if sign {
privateSigKey = fromUID.PrivateSigKey64()
}
args := &msg.EncryptArgs{
Writer: w,
From: fromUID,
To: toUID,
NymAddress: nymAddress,
SenderLastKeychainHash: senderLastKeychainHash,
PrivateSigKey: privateSigKey,
Reader: r,
Rand: cipher.RandReader,
KeyStore: ce,
}
nymAddress, err = msg.Encrypt(args)
if err != nil {
return err
}
// show nymaddress on status-fd
fmt.Fprintf(statusfp, "NYMADDRESS:\t%s\n", nymAddress)
return nil
}