nodeid.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package protocol
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "encoding/base32"
  6. "errors"
  7. "fmt"
  8. "log"
  9. "regexp"
  10. "strings"
  11. "github.com/calmh/syncthing/luhn"
  12. )
  13. type NodeID [32]byte
  14. var LocalNodeID = NodeID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
  15. // NewNodeID generates a new node ID from the raw bytes of a certificate
  16. func NewNodeID(rawCert []byte) NodeID {
  17. var n NodeID
  18. hf := sha256.New()
  19. hf.Write(rawCert)
  20. hf.Sum(n[:0])
  21. return n
  22. }
  23. func NodeIDFromString(s string) (NodeID, error) {
  24. var n NodeID
  25. err := n.UnmarshalText([]byte(s))
  26. return n, err
  27. }
  28. // String returns the canonical string representation of the node ID
  29. func (n NodeID) String() string {
  30. id := base32.StdEncoding.EncodeToString(n[:])
  31. id = strings.Trim(id, "=")
  32. id, err := luhnify(id)
  33. if err != nil {
  34. // Should never happen
  35. panic(err)
  36. }
  37. id = chunkify(id)
  38. return id
  39. }
  40. func (n NodeID) GoString() string {
  41. return n.String()
  42. }
  43. func (n NodeID) Compare(other NodeID) int {
  44. return bytes.Compare(n[:], other[:])
  45. }
  46. func (n NodeID) Equals(other NodeID) bool {
  47. return bytes.Compare(n[:], other[:]) == 0
  48. }
  49. func (n *NodeID) MarshalText() ([]byte, error) {
  50. return []byte(n.String()), nil
  51. }
  52. func (n *NodeID) UnmarshalText(bs []byte) error {
  53. id := string(bs)
  54. id = strings.Trim(id, "=")
  55. id = strings.ToUpper(id)
  56. id = untypeoify(id)
  57. id = unchunkify(id)
  58. var err error
  59. switch len(id) {
  60. case 56:
  61. // New style, with check digits
  62. id, err = unluhnify(id)
  63. if err != nil {
  64. return err
  65. }
  66. fallthrough
  67. case 52:
  68. // Old style, no check digits
  69. dec, err := base32.StdEncoding.DecodeString(id + "====")
  70. if err != nil {
  71. return err
  72. }
  73. copy(n[:], dec)
  74. return nil
  75. default:
  76. return errors.New("node ID invalid: incorrect length")
  77. }
  78. }
  79. func luhnify(s string) (string, error) {
  80. if len(s) != 52 {
  81. panic("unsupported string length")
  82. }
  83. res := make([]string, 0, 4)
  84. for i := 0; i < 4; i++ {
  85. p := s[i*13 : (i+1)*13]
  86. l, err := luhn.Base32.Generate(p)
  87. if err != nil {
  88. return "", err
  89. }
  90. res = append(res, fmt.Sprintf("%s%c", p, l))
  91. }
  92. return res[0] + res[1] + res[2] + res[3], nil
  93. }
  94. func unluhnify(s string) (string, error) {
  95. if len(s) != 56 {
  96. return "", fmt.Errorf("unsupported string length %d", len(s))
  97. }
  98. res := make([]string, 0, 4)
  99. for i := 0; i < 4; i++ {
  100. p := s[i*14 : (i+1)*14-1]
  101. l, err := luhn.Base32.Generate(p)
  102. if err != nil {
  103. return "", err
  104. }
  105. if g := fmt.Sprintf("%s%c", p, l); g != s[i*14:(i+1)*14] {
  106. log.Printf("%q; %q", g, s[i*14:(i+1)*14])
  107. return "", errors.New("check digit incorrect")
  108. }
  109. res = append(res, p)
  110. }
  111. return res[0] + res[1] + res[2] + res[3], nil
  112. }
  113. func chunkify(s string) string {
  114. s = regexp.MustCompile("(.{7})").ReplaceAllString(s, "$1-")
  115. s = strings.Trim(s, "-")
  116. return s
  117. }
  118. func unchunkify(s string) string {
  119. s = strings.Replace(s, "-", "", -1)
  120. s = strings.Replace(s, " ", "", -1)
  121. return s
  122. }
  123. func untypeoify(s string) string {
  124. s = strings.Replace(s, "0", "O", -1)
  125. s = strings.Replace(s, "1", "I", -1)
  126. s = strings.Replace(s, "8", "B", -1)
  127. return s
  128. }