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.0KB

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