| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- package modules
-
- import (
- "fmt"
- "strconv"
- )
-
- // 키관리 부분, 노드 입력받고 키 반환하는 부분 구현
- type Security struct {
- KeyMap map[string]string
- }
-
- func NewSecurity() *Security {
- security := &Security{map[string]string{}}
- return security
- }
-
- type SecurityManager interface {
- RegKey(ksm KeyShareMsg)
- GetNodeKey(nodeName string) int64
- ReEncrypt(fromKey int64, toKey int64, target []int64) []int64
- ReEncPubMsg(fromPubMsg PublishMsg, nodeName string) *PublishMsg
- //CompareTopic(topic1 []int64, topic2 []int64) int
- //CompareDigit(topic1 int64, topic2 int64) int
- //CompareAlpha(topic1 []int64, topic2 []int64) int
- }
-
- /**
- keyShareMsg 에서 각 노드의 private 키를 받아 keyMap 에 저장
- */
- func (sc Security) RegKey(ksm KeyShareMsg) {
- sc.KeyMap[ksm.Message.From()] = ksm.key
- }
-
- /**
- 각 노드의 키를 주소를 이용하여 맵에서 가져옴
- */
- func (sc Security) GetNodeKey(nodeName string) int64 {
-
- messageStringKey := sc.KeyMap[nodeName]
- mKey, err := strconv.ParseInt(messageStringKey, 10, 64)
- if err != nil {
- fmt.Println("reEncrypt Error: key string to int64 parsing error.")
- }
- return mKey
- }
-
- /**
- reEncrypt 해서 슬라이스 반환
- */
- func (sc Security) ReEncrypt(fromKey int64, toKey int64, target []int64) []int64 {
- for index := range target {
- target[index] = target[index] - fromKey + toKey
- }
-
- return target
- }
-
- func (sc Security) ReEncryptWithoutPrivateKey(toKey int64, target []int64) []int64 {
- for index := range target {
- target[index] = target[index] + toKey
- }
-
- return target
- }
-
- // topic과 value는 m+k로만 존재하므로 ReEnc과정에서 subscriber의 개인키만 더해주면 된다.
- func (sc Security) ReEncPubMsg(fromPubMsg PublishMsg, nodeName string) *PublishMsg {
- toKey := sc.GetNodeKey(nodeName)
- fromKey := sc.GetNodeKey(fromPubMsg.Message.From())
-
- toPubMsg := new(PublishMsg)
- toPubMsg.Message = fromPubMsg.Message
- toPubMsg.topic = sc.ReEncryptWithoutPrivateKey(toKey, fromPubMsg.topic)
- toPubMsg.value = sc.ReEncryptWithoutPrivateKey(toKey, fromPubMsg.value)
- toPubMsg.content = sc.ReEncrypt(fromKey, toKey, fromPubMsg.content)
-
- return toPubMsg
- }
-
- /**
- Compare 함수들은 같으면 0 다르면 -1 (비교가 필요한 경우 오름차순 1 내림차순 -1)
- */
- //func (sc Security) CompareTopic(topic1 []int64, topic2 []int64) int {
- // // 길이 같은지 체크
- // if len(topic1) != len(topic2) {
- // return -1
- // }
- // for i := 0; i < len(topic2); i++ {
- // if topic1[i] != topic2[i] {
- // return -1
- // }
- // }
- // return 0
- //}
- //
- //func (sc Security) CompareDigit(topic1 int64, topic2 int64) int {
- // if topic1 < topic2 {
- // return 1
- // } else if topic1 > topic2 {
- // return -1
- // }
- // return 0
- //}
- //
- //func (sc Security) CompareAlpha(topic1 []int64, topic2 []int64) int {
- // // 길이 같은지 체크
- // if len(topic1) != len(topic2) {
- // return -1
- // }
- // for i := 0; i < len(topic2); i++ {
- // if topic1[i] != topic2[i] {
- // return -1
- // }
- // }
- // return 0
- //}
-
- // private key 생성 메세지
- //func (sc Security) keyGenPrivate() KeyGenMsg{
- //
- //}
|