-
Notifications
You must be signed in to change notification settings - Fork 1
/
bliss.go
92 lines (51 loc) · 1.53 KB
/
bliss.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
82
83
84
85
86
87
88
89
90
91
92
/*
Links:
https://github.com/LoCCS/bliss/search?q=entropy
https://github.com/LoCCS/bliss
*/
package main
import (
"github.com/LoCCS/bliss/sampler"
"github.com/LoCCS/bliss"
"encoding/hex"
"math/rand"
"strconv"
"time"
"C"
)
//export genBLISS
func genBLISS() *C.char {
rand.Seed(time.Now().UnixNano())
seed := make([]byte, sampler.SHA_512_DIGEST_LENGTH)
rand.Read(seed)
entropy, _ := sampler.NewEntropy(seed)
prv, _ := bliss.GeneratePrivateKey(0, entropy)
pub := prv.PublicKey()
return C.CString(hex.EncodeToString(pub.Encode()) + ":" + hex.EncodeToString(seed))
}
//export signBLISS
func signBLISS(message *C.char, seedStr *C.char) *C.char {
//Decode msg an seed => entropy => privateKey
msg := []byte(C.GoString(message))
sid, _ := hex.DecodeString(C.GoString(seedStr))
seed := []byte(sid) //uint8/byte array
entropy, _ := sampler.NewEntropy(seed)
key, _ := bliss.GeneratePrivateKey(0, entropy)
//Gen signature
sig, _ := key.Sign(msg, entropy)
return C.CString(hex.EncodeToString(sig.Encode()))
}
//export verifyBLISS
func verifyBLISS(message *C.char, pubKey *C.char, sig *C.char) *C.char {
//Decode msg an publicKey
msg := []byte(C.GoString(message))
pk, _ := hex.DecodeString(C.GoString(pubKey))
publicKey, _ := bliss.DecodePublicKey(pk)
//Decode signature
sg, _ := hex.DecodeString(C.GoString(sig))
signature, _ := bliss.DecodeSignature(sg)
//Verification itself
_, err := publicKey.Verify(msg, signature)
return C.CString(strconv.FormatBool(err == nil))
}
func main() {}