nodeid.go 2.9 KB

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