nodeid.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 = luhnify(id)
  32. id = chunkify(id)
  33. return id
  34. }
  35. func (n NodeID) GoString() string {
  36. return n.String()
  37. }
  38. func (n NodeID) Compare(other NodeID) int {
  39. return bytes.Compare(n[:], other[:])
  40. }
  41. func (n NodeID) Equals(other NodeID) bool {
  42. return bytes.Compare(n[:], other[:]) == 0
  43. }
  44. func (n *NodeID) MarshalText() ([]byte, error) {
  45. return []byte(n.String()), nil
  46. }
  47. func (n *NodeID) UnmarshalText(bs []byte) error {
  48. id := string(bs)
  49. id = strings.Trim(id, "=")
  50. id = strings.ToUpper(id)
  51. id = untypeoify(id)
  52. id = unchunkify(id)
  53. var err error
  54. switch len(id) {
  55. case 56:
  56. // New style, with check digits
  57. id, err = unluhnify(id)
  58. if err != nil {
  59. return err
  60. }
  61. fallthrough
  62. case 52:
  63. // Old style, no check digits
  64. dec, err := base32.StdEncoding.DecodeString(id + "====")
  65. if err != nil {
  66. return err
  67. }
  68. copy(n[:], dec)
  69. return nil
  70. default:
  71. return errors.New("node ID invalid: incorrect length")
  72. }
  73. }
  74. func luhnify(s string) string {
  75. if len(s) != 52 {
  76. panic("unsupported string length")
  77. }
  78. res := make([]string, 0, 4)
  79. for i := 0; i < 4; i++ {
  80. p := s[i*13 : (i+1)*13]
  81. l := luhn.Base32Trimmed.Generate(p)
  82. res = append(res, fmt.Sprintf("%s%c", p, l))
  83. }
  84. return res[0] + res[1] + res[2] + res[3]
  85. }
  86. func unluhnify(s string) (string, error) {
  87. if len(s) != 56 {
  88. return "", fmt.Errorf("unsupported string length %d", len(s))
  89. }
  90. res := make([]string, 0, 4)
  91. for i := 0; i < 4; i++ {
  92. p := s[i*14 : (i+1)*14-1]
  93. l := luhn.Base32Trimmed.Generate(p)
  94. if g := fmt.Sprintf("%s%c", p, l); g != s[i*14:(i+1)*14] {
  95. log.Printf("%q; %q", g, s[i*14:(i+1)*14])
  96. return "", errors.New("check digit incorrect")
  97. }
  98. res = append(res, p)
  99. }
  100. return res[0] + res[1] + res[2] + res[3], nil
  101. }
  102. func chunkify(s string) string {
  103. s = regexp.MustCompile("(.{7})").ReplaceAllString(s, "$1-")
  104. s = strings.Trim(s, "-")
  105. return s
  106. }
  107. func unchunkify(s string) string {
  108. s = strings.Replace(s, "-", "", -1)
  109. s = strings.Replace(s, " ", "", -1)
  110. return s
  111. }
  112. func untypeoify(s string) string {
  113. s = strings.Replace(s, "0", "O", -1)
  114. s = strings.Replace(s, "1", "I", -1)
  115. s = strings.Replace(s, "8", "B", -1)
  116. return s
  117. }