Project Moscato Team Messaging Middleware Implemetation Message Middleware by Golang Operate as Secure, Effectively
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

secure.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package modules
  2. import (
  3. "fmt"
  4. "log"
  5. "strconv"
  6. )
  7. // 키관리 부분, 노드 입력받고 키 반환하는 부분 구현
  8. type Security struct {
  9. KeyMap map[string]string
  10. }
  11. func NewSecurity() *Security {
  12. security := &Security{map[string]string{}}
  13. fmt.Println("security setting complete.")
  14. return security
  15. }
  16. type SecurityManager interface {
  17. RegKey(rm RegisterMsg)
  18. GetNodeKey(nodeName string) int64
  19. ReEncrypt(fromKey int64, toKey int64, target []int64) []int64
  20. ReEncPubMsg(fromPubMsg PublishMsg, nodeName string) PublishMsg
  21. RemoveSecureKey(nodeName string) bool
  22. //CompareTopic(topic1 []int64, topic2 []int64) int
  23. //CompareDigit(topic1 int64, topic2 int64) int
  24. //CompareAlpha(topic1 []int64, topic2 []int64) int
  25. }
  26. /**
  27. keyShareMsg 에서 각 노드의 private 키를 받아 keyMap 에 저장
  28. */
  29. func (sc Security) RegKey(rm RegisterMsg) {
  30. sc.KeyMap[rm.Message.From] = strconv.FormatInt(rm.PrivateKey, 10)
  31. }
  32. /**
  33. 각 노드의 키를 주소를 이용하여 맵에서 가져옴
  34. */
  35. func (sc Security) GetNodeKey(nodeName string) int64 {
  36. messageStringKey := sc.KeyMap[nodeName]
  37. mKey, err := strconv.ParseInt(messageStringKey, 10, 64)
  38. if err != nil {
  39. fmt.Println("GetNodeKey Error: key string to int64 parsing error.")
  40. }
  41. return mKey
  42. }
  43. /**
  44. reEncrypt 해서 슬라이스 반환
  45. */
  46. func (sc Security) ReEncrypt(fromKey int64, toKey int64, target []int64) []int64 {
  47. var tmpTarget []int64
  48. for index := range target {
  49. tmpTarget = append(tmpTarget, target[index]-fromKey+toKey)
  50. }
  51. return tmpTarget
  52. }
  53. func (sc Security) ReEncryptWithoutPrivateKey(toKey int64, target []int64) []int64 {
  54. var tmpTarget []int64
  55. for index := range target {
  56. tmpTarget = append(tmpTarget, target[index]+toKey)
  57. }
  58. return tmpTarget
  59. }
  60. // topic과 value는 m+k로만 존재하므로 ReEnc과정에서 subscriber의 개인키만 더해주면 된다.
  61. func (sc Security) ReEncPubMsg(fromPubMsg PublishMsg, nodeName string) PublishMsg {
  62. toKey := sc.GetNodeKey(nodeName)
  63. fromKey := sc.GetNodeKey(fromPubMsg.Message.From)
  64. toPubMsg := PublishMsg{}
  65. toPubMsg.Message = fromPubMsg.Message
  66. toPubMsg.Topic = sc.ReEncryptWithoutPrivateKey(toKey, fromPubMsg.Topic)
  67. toPubMsg.Value = sc.ReEncryptWithoutPrivateKey(toKey, fromPubMsg.Value)
  68. toPubMsg.Content = sc.ReEncrypt(fromKey, toKey, fromPubMsg.Content)
  69. return toPubMsg
  70. }
  71. func (sc *Security) RemoveSecureKey(nodeName string) bool {
  72. //삭제 전 존재여부 확인
  73. _, exists := sc.KeyMap[nodeName]
  74. if exists {
  75. delete(sc.KeyMap, nodeName)
  76. log.Println("[" + nodeName + "] : delete Key successful")
  77. return true
  78. } else {
  79. return false
  80. }
  81. }
  82. /**
  83. Compare 함수들은 같으면 0 다르면 -1 (비교가 필요한 경우 오름차순 1 내림차순 -1)
  84. */
  85. //func (sc Security) CompareTopic(topic1 []int64, topic2 []int64) int {
  86. // // 길이 같은지 체크
  87. // if len(topic1) != len(topic2) {
  88. // return -1
  89. // }
  90. // for i := 0; i < len(topic2); i++ {
  91. // if topic1[i] != topic2[i] {
  92. // return -1
  93. // }
  94. // }
  95. // return 0
  96. //}
  97. //
  98. //func (sc Security) CompareDigit(topic1 int64, topic2 int64) int {
  99. // if topic1 < topic2 {
  100. // return 1
  101. // } else if topic1 > topic2 {
  102. // return -1
  103. // }
  104. // return 0
  105. //}
  106. //
  107. //func (sc Security) CompareAlpha(topic1 []int64, topic2 []int64) int {
  108. // // 길이 같은지 체크
  109. // if len(topic1) != len(topic2) {
  110. // return -1
  111. // }
  112. // for i := 0; i < len(topic2); i++ {
  113. // if topic1[i] != topic2[i] {
  114. // return -1
  115. // }
  116. // }
  117. // return 0
  118. //}
  119. // private key 생성 메세지
  120. //func (sc Security) keyGenPrivate() KeyGenMsg{
  121. //
  122. //}