ソースを参照

add function for re-encrypt publish message and test Re-encrypt message

master
kidjung 4年前
コミット
daf926d655
2個のファイルの変更155行の追加42行の削除
  1. 61
    38
      src/broker/modules/secure.go
  2. 94
    4
      src/broker/modules/secure_test.go

+ 61
- 38
src/broker/modules/secure.go ファイルの表示

@@ -17,11 +17,12 @@ func NewSecurity() *Security {
17 17
 
18 18
 type SecurityManager interface {
19 19
 	RegKey(ksm KeyShareMsg)
20
-	GetNodeKey(message Message) int64
20
+	GetNodeKey(nodeName string) int64
21 21
 	ReEncrypt(fromKey int64, toKey int64, target []int64) []int64
22
-	CompareTopic(topic1 []int64, topic2 []int64) int
23
-	CompareDigit(topic1 int64, topic2 int64) int
24
-	CompareAlpha(topic1 []int64, topic2 []int64) int
22
+	ReEncPubMsg(fromPubMsg PublishMsg, nodeName string) *PublishMsg
23
+	//CompareTopic(topic1 []int64, topic2 []int64) int
24
+	//CompareDigit(topic1 int64, topic2 int64) int
25
+	//CompareAlpha(topic1 []int64, topic2 []int64) int
25 26
 }
26 27
 
27 28
 /**
@@ -34,9 +35,9 @@ func (sc Security) RegKey(ksm KeyShareMsg) {
34 35
 /**
35 36
 각 노드의 키를 주소를 이용하여 맵에서 가져옴
36 37
 */
37
-func (sc Security) GetNodeKey(message Message) int64 {
38
+func (sc Security) GetNodeKey(nodeName string) int64 {
38 39
 
39
-	messageStringKey := sc.KeyMap[message.From()]
40
+	messageStringKey := sc.KeyMap[nodeName]
40 41
 	mKey, err := strconv.ParseInt(messageStringKey, 10, 64)
41 42
 	if err != nil {
42 43
 		fmt.Println("reEncrypt Error: key string to int64 parsing error.")
@@ -55,44 +56,66 @@ func (sc Security) ReEncrypt(fromKey int64, toKey int64, target []int64) []int64
55 56
 	return target
56 57
 }
57 58
 
58
-/**
59
-Compare 함수들은 같으면 0 다르면 -1 (비교가 필요한 경우 오름차순 1 내림차순 -1)
60
-*/
61
-func (sc Security) CompareTopic(topic1 []int64, topic2 []int64) int {
62
-	// 길이 같은지 체크
63
-	if len(topic1) != len(topic2) {
64
-		return -1
65
-	}
66
-	for i := 0; i < len(topic2); i++ {
67
-		if topic1[i] != topic2[i] {
68
-			return -1
69
-		}
59
+func (sc Security) ReEncryptWithoutPrivateKey(toKey int64, target []int64) []int64 {
60
+	for index := range target {
61
+		target[index] = target[index] + toKey
70 62
 	}
71
-	return 0
72
-}
73 63
 
74
-func (sc Security) CompareDigit(topic1 int64, topic2 int64) int {
75
-	if topic1 < topic2 {
76
-		return 1
77
-	} else if topic1 > topic2 {
78
-		return -1
79
-	}
80
-	return 0
64
+	return target
81 65
 }
82 66
 
83
-func (sc Security) CompareAlpha(topic1 []int64, topic2 []int64) int {
84
-	// 길이 같은지 체크
85
-	if len(topic1) != len(topic2) {
86
-		return -1
87
-	}
88
-	for i := 0; i < len(topic2); i++ {
89
-		if topic1[i] != topic2[i] {
90
-			return -1
91
-		}
92
-	}
93
-	return 0
67
+// topic과 value는 m+k로만 존재하므로 ReEnc과정에서 subscriber의 개인키만 더해주면 된다.
68
+func (sc Security) ReEncPubMsg(fromPubMsg PublishMsg, nodeName string) *PublishMsg {
69
+	toKey := sc.GetNodeKey(nodeName)
70
+	fromKey := sc.GetNodeKey(fromPubMsg.Message.From())
71
+
72
+	toPubMsg := new(PublishMsg)
73
+	toPubMsg.Message = fromPubMsg.Message
74
+	toPubMsg.topic = sc.ReEncryptWithoutPrivateKey(toKey, fromPubMsg.topic)
75
+	toPubMsg.value = sc.ReEncryptWithoutPrivateKey(toKey, fromPubMsg.value)
76
+	toPubMsg.content = sc.ReEncrypt(fromKey, toKey, fromPubMsg.content)
77
+
78
+	return toPubMsg
94 79
 }
95 80
 
81
+/**
82
+Compare 함수들은 같으면 0 다르면 -1 (비교가 필요한 경우 오름차순 1 내림차순 -1)
83
+*/
84
+//func (sc Security) CompareTopic(topic1 []int64, topic2 []int64) int {
85
+//	// 길이 같은지 체크
86
+//	if len(topic1) != len(topic2) {
87
+//		return -1
88
+//	}
89
+//	for i := 0; i < len(topic2); i++ {
90
+//		if topic1[i] != topic2[i] {
91
+//			return -1
92
+//		}
93
+//	}
94
+//	return 0
95
+//}
96
+//
97
+//func (sc Security) CompareDigit(topic1 int64, topic2 int64) int {
98
+//	if topic1 < topic2 {
99
+//		return 1
100
+//	} else if topic1 > topic2 {
101
+//		return -1
102
+//	}
103
+//	return 0
104
+//}
105
+//
106
+//func (sc Security) CompareAlpha(topic1 []int64, topic2 []int64) int {
107
+//	// 길이 같은지 체크
108
+//	if len(topic1) != len(topic2) {
109
+//		return -1
110
+//	}
111
+//	for i := 0; i < len(topic2); i++ {
112
+//		if topic1[i] != topic2[i] {
113
+//			return -1
114
+//		}
115
+//	}
116
+//	return 0
117
+//}
118
+
96 119
 // private key 생성 메세지
97 120
 //func (sc Security) keyGenPrivate() KeyGenMsg{
98 121
 //

+ 94
- 4
src/broker/modules/secure_test.go ファイルの表示

@@ -12,11 +12,101 @@ func TestCompare(t *testing.T) {
12 12
 
13 13
 	ksm := KeyShareMsg{Message: Message{from: "1.1.1.1", version: "1", time: "2", kind: 1}, key: "1234"}
14 14
 	sm.RegKey(ksm)
15
-	sm.GetNodeKey(ksm.Message)
16
-	fmt.Println(sm.GetNodeKey(ksm.Message))
15
+	sm.GetNodeKey(ksm.Message.From())
16
+	fmt.Println(sm.GetNodeKey(ksm.Message.From()))
17 17
 	var targetKey []int64
18 18
 	targetKey = []int64{1234, 1235, 1236}
19
-	fmt.Println(sm.ReEncrypt(sm.GetNodeKey(ksm.Message), 0, targetKey))
20
-	fmt.Println(sm.CompareDigit(1236, 1234))
19
+	fmt.Println(sm.ReEncrypt(sm.GetNodeKey(ksm.Message.From()), 0, targetKey))
20
+	//fmt.Println(sm.CompareDigit(1236, 1234))
21 21
 
22 22
 }
23
+
24
+func CreatePubMsg(msg Message, topic string, value string, content string) *PublishMsg {
25
+	toPubMsg := new(PublishMsg)
26
+	toPubMsg.Message = msg
27
+
28
+	intArr := []rune(topic)
29
+	fmt.Print("topic length ")
30
+	fmt.Println(len(intArr))
31
+	fmt.Println(len(toPubMsg.topic))
32
+	for index := 0; index < len(intArr); index++ {
33
+		toPubMsg.topic = append(toPubMsg.topic, int64(intArr[index]))
34
+	}
35
+	fmt.Println(len(toPubMsg.topic))
36
+	intArr = []rune(value)
37
+	for index := 0; index < len(intArr); index++ {
38
+		toPubMsg.value = append(toPubMsg.value, int64(intArr[index]))
39
+	}
40
+	intArr = []rune(content)
41
+	for index := 0; index < len(intArr); index++ {
42
+		toPubMsg.content = append(toPubMsg.content, int64(intArr[index]))
43
+	}
44
+
45
+	return toPubMsg
46
+}
47
+
48
+func EncryptionMsg(msg *PublishMsg, gyKey int64, privateKey int64) PublishMsg {
49
+	for index := range msg.topic {
50
+		msg.topic[index] = msg.topic[index] + gyKey
51
+	}
52
+	for index := range msg.value {
53
+		msg.value[index] = msg.value[index] + gyKey
54
+	}
55
+	for index := range msg.content {
56
+		msg.content[index] = msg.content[index] + gyKey + privateKey
57
+	}
58
+
59
+	return *msg
60
+}
61
+
62
+func DecryptionMsg(msg *PublishMsg, gyKey int64, privateKey int64) {
63
+	for index := range msg.topic {
64
+		msg.topic[index] = msg.topic[index] - gyKey - privateKey
65
+	}
66
+	for index := range msg.value {
67
+		msg.value[index] = msg.value[index] - gyKey - privateKey
68
+	}
69
+	for index := range msg.content {
70
+		msg.content[index] = msg.content[index] - gyKey - privateKey
71
+	}
72
+
73
+	var runeArr []rune
74
+	for index := range msg.topic {
75
+		runeArr = append(runeArr, rune(int(msg.topic[index])))
76
+	}
77
+	fmt.Println("\n\ntopic is: " + string(runeArr))
78
+	runeArr = nil
79
+
80
+	for index := range msg.value {
81
+		runeArr = append(runeArr, rune(int(msg.value[index])))
82
+	}
83
+	fmt.Println("value is: " + string(runeArr))
84
+	runeArr = nil
85
+
86
+	for index := range msg.content {
87
+		runeArr = append(runeArr, rune(int(msg.content[index])))
88
+	}
89
+	fmt.Println("content is: " + string(runeArr))
90
+	runeArr = nil
91
+
92
+}
93
+
94
+// from "1.1.1.1" to "3.3.3.3" node
95
+func TestReEnc(t *testing.T) {
96
+	var security = NewSecurity()
97
+	var sm SecurityManager
98
+	sm = security
99
+	security.KeyMap["1.1.1.1"] = "5678"
100
+	security.KeyMap["3.3.3.3"] = "9999"
101
+
102
+	fmt.Println(sm.GetNodeKey("3.3.3.3"))
103
+	msg := Message{from: "1.1.1.1", version: "1", time: "123", kind: 1}
104
+	fmt.Println(msg)
105
+	publishMsg := CreatePubMsg(msg, "soccer123한글", "playerList", "son and 10 players")
106
+	fmt.Println(publishMsg)
107
+	encPublishMsg := EncryptionMsg(publishMsg, 1234, 5678)
108
+	fmt.Println(encPublishMsg)
109
+	reEncPublishMsg := sm.ReEncPubMsg(encPublishMsg, "3.3.3.3")
110
+	fmt.Println(reEncPublishMsg)
111
+	DecryptionMsg(reEncPublishMsg, 1234, 9999)
112
+}

読み込み中…
キャンセル
保存