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{ // //}