-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientside.go
68 lines (56 loc) · 1.69 KB
/
clientside.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
package capka
// #include <sodium.h>
// #include "capka.h"
import "C"
import (
"encoding/base64"
"encoding/json"
"unsafe"
"github.com/go-faster/errors"
"github.com/jamesruan/sodium"
)
func MakeKP(username, password, domain string) (sodium.SignKP, error) {
return MakeKP_Ex(
[]byte(username),
[]byte(password),
[]byte(domain),
C.crypto_pwhash_OPSLIMIT_INTERACTIVE,
C.crypto_pwhash_MEMLIMIT_INTERACTIVE,
)
}
func MakeKP_Ex(username, password, domain []byte, ops, mem C.int) (sodium.SignKP, error) {
kp := sodium.SignKP{}
saltData := append(username, domain...)
pk := make([]byte, C.crypto_sign_PUBLICKEYBYTES)
sk := make([]byte, C.crypto_sign_SECRETKEYBYTES)
if C.capka_makeKeypair(
(*C.char)(unsafe.Pointer(&password[0])),
(*C.char)(unsafe.Pointer(&saltData[0])),
ops,
mem,
(*C.uchar)(unsafe.Pointer(&pk[0])),
(*C.uchar)(unsafe.Pointer(&sk[0])),
) != 0 {
return kp, errors.New("CAPKA: internal failure")
}
kp.PublicKey = sodium.SignPublicKey{Bytes: pk}
kp.SecretKey = sodium.SignSecretKey{Bytes: sk}
return kp, nil
}
func (data *LoginData) Sign(key sodium.SignSecretKey) sodium.Signature {
return data.MakeSigInput().SignDetached(key)
}
func (data *LoginData) Encode(key sodium.SignSecretKey) LoginRequest {
return LoginRequest{
User: data.User,
Nonce: base64.StdEncoding.EncodeToString(data.Nonce),
EphKey: base64.StdEncoding.EncodeToString(data.EphKey),
Signature: base64.StdEncoding.EncodeToString(data.Sign(key).Bytes),
}
}
func (data *LoginData) EncodeJSON(key sodium.SignSecretKey) ([]byte, error) {
return json.Marshal(data.Encode(key))
}
func Decrypt(data sodium.Bytes, kp sodium.BoxKP) (sodium.Bytes, error) {
return data.SealedBoxOpen(kp)
}