-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcpabe_test.go
59 lines (56 loc) · 1.33 KB
/
cpabe_test.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
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"hash/fnv"
"io/ioutil"
"os"
"testing"
)
func TestAA(t *testing.T) {
//pubkey, prikey, _ := Setup()
f, _ := os.Open("pk.txt")
pkdata, _ := ioutil.ReadAll(f)
f.Close()
f, _ = os.Open("sk.txt")
skdata, _ := ioutil.ReadAll(f)
f.Close()
pubkey := string(pkdata)
prikey := string(skdata)
pub, _ := hex.DecodeString(pubkey)
pri, _ := hex.DecodeString(prikey)
text := "sdnfsdfjsfjlsadflsdfdjsflksjdflkdsjfklsjfklsdjfklsdjflksjdkldsjs"
attrs := []string{"depart_1000", "loc_212", "user_10000"}
//encrypt
//input: policy, text, attrs(verify necessity)
pk, _ := DecodePubkey(pub)
departAttr := "depart_1000"
policy := fmt.Sprintf("%d", Hash(departAttr))
ciphertext, _ := Encrypt(text, policy, pk)
fmt.Println(ciphertext)
//fmt.Println(ciphertext)
//decrypt
//input: attrs
attrInt := make([]int, len(attrs))
for k, v := range attrs {
f := fnv.New32()
f.Write([]byte(v))
attrInt[k] = int(f.Sum32())
}
cp, _ := DecodeCipher(ciphertext)
sk, _ := DecodePrikey(pri)
ak, _ := Keygen(attrInt, sk)
decryptedText, err := Decrypt(cp, ak, pk)
fmt.Println(err, decryptedText)
}
func TestMM(t *testing.T) {
var arr []Behaviour
_ = json.Unmarshal(nil, &arr)
arr =append(arr, Behaviour{
Id: "sadf",
Result: false,
})
d, _ := json.Marshal(arr)
fmt.Println(string(d))
}