deviceid.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright (C) 2014 The Protocol Authors.
  2. package protocol
  3. import (
  4. "bytes"
  5. "encoding/base32"
  6. "encoding/binary"
  7. "errors"
  8. "fmt"
  9. "strings"
  10. "github.com/syncthing/syncthing/lib/sha256"
  11. )
  12. const DeviceIDLength = 32
  13. type DeviceID [DeviceIDLength]byte
  14. type ShortID uint64
  15. var (
  16. LocalDeviceID = repeatedDeviceID(0xff)
  17. GlobalDeviceID = repeatedDeviceID(0xf8)
  18. EmptyDeviceID = DeviceID{ /* all zeroes */ }
  19. )
  20. func repeatedDeviceID(v byte) (d DeviceID) {
  21. for i := range d {
  22. d[i] = v
  23. }
  24. return
  25. }
  26. // NewDeviceID generates a new device ID from the raw bytes of a certificate
  27. func NewDeviceID(rawCert []byte) DeviceID {
  28. var n DeviceID
  29. hf := sha256.New()
  30. hf.Write(rawCert)
  31. hf.Sum(n[:0])
  32. return n
  33. }
  34. func DeviceIDFromString(s string) (DeviceID, error) {
  35. var n DeviceID
  36. err := n.UnmarshalText([]byte(s))
  37. return n, err
  38. }
  39. func DeviceIDFromBytes(bs []byte) DeviceID {
  40. var n DeviceID
  41. if len(bs) != len(n) {
  42. panic("incorrect length of byte slice representing device ID")
  43. }
  44. copy(n[:], bs)
  45. return n
  46. }
  47. // String returns the canonical string representation of the device ID
  48. func (n DeviceID) String() string {
  49. if n == EmptyDeviceID {
  50. return ""
  51. }
  52. id := base32.StdEncoding.EncodeToString(n[:])
  53. id = strings.Trim(id, "=")
  54. id, err := luhnify(id)
  55. if err != nil {
  56. // Should never happen
  57. panic(err)
  58. }
  59. id = chunkify(id)
  60. return id
  61. }
  62. func (n DeviceID) GoString() string {
  63. return n.String()
  64. }
  65. func (n DeviceID) Compare(other DeviceID) int {
  66. return bytes.Compare(n[:], other[:])
  67. }
  68. func (n DeviceID) Equals(other DeviceID) bool {
  69. return bytes.Equal(n[:], other[:])
  70. }
  71. // Short returns an integer representing bits 0-63 of the device ID.
  72. func (n DeviceID) Short() ShortID {
  73. return ShortID(binary.BigEndian.Uint64(n[:]))
  74. }
  75. func (n *DeviceID) MarshalText() ([]byte, error) {
  76. return []byte(n.String()), nil
  77. }
  78. func (s ShortID) String() string {
  79. if s == 0 {
  80. return ""
  81. }
  82. var bs [8]byte
  83. binary.BigEndian.PutUint64(bs[:], uint64(s))
  84. return base32.StdEncoding.EncodeToString(bs[:])[:7]
  85. }
  86. func (n *DeviceID) UnmarshalText(bs []byte) error {
  87. id := string(bs)
  88. id = strings.Trim(id, "=")
  89. id = strings.ToUpper(id)
  90. id = untypeoify(id)
  91. id = unchunkify(id)
  92. var err error
  93. switch len(id) {
  94. case 0:
  95. *n = EmptyDeviceID
  96. return nil
  97. case 56:
  98. // New style, with check digits
  99. id, err = unluhnify(id)
  100. if err != nil {
  101. return err
  102. }
  103. fallthrough
  104. case 52:
  105. // Old style, no check digits
  106. dec, err := base32.StdEncoding.DecodeString(id + "====")
  107. if err != nil {
  108. return err
  109. }
  110. copy(n[:], dec)
  111. return nil
  112. default:
  113. return fmt.Errorf("%q: device ID invalid: incorrect length", bs)
  114. }
  115. }
  116. func (n *DeviceID) ProtoSize() int {
  117. // Used by protobuf marshaller.
  118. return DeviceIDLength
  119. }
  120. func (n *DeviceID) MarshalTo(bs []byte) (int, error) {
  121. // Used by protobuf marshaller.
  122. if len(bs) < DeviceIDLength {
  123. return 0, errors.New("destination too short")
  124. }
  125. copy(bs, (*n)[:])
  126. return DeviceIDLength, nil
  127. }
  128. func (n *DeviceID) Unmarshal(bs []byte) error {
  129. // Used by protobuf marshaller.
  130. if len(bs) < DeviceIDLength {
  131. return fmt.Errorf("%q: not enough data", bs)
  132. }
  133. copy((*n)[:], bs)
  134. return nil
  135. }
  136. func luhnify(s string) (string, error) {
  137. if len(s) != 52 {
  138. panic("unsupported string length")
  139. }
  140. res := make([]byte, 4*(13+1))
  141. for i := 0; i < 4; i++ {
  142. p := s[i*13 : (i+1)*13]
  143. copy(res[i*(13+1):], p)
  144. l, err := luhnBase32.generate(p)
  145. if err != nil {
  146. return "", err
  147. }
  148. res[(i+1)*(13)+i] = byte(l)
  149. }
  150. return string(res), nil
  151. }
  152. func unluhnify(s string) (string, error) {
  153. if len(s) != 56 {
  154. return "", fmt.Errorf("%q: unsupported string length %d", s, len(s))
  155. }
  156. res := make([]byte, 52)
  157. for i := 0; i < 4; i++ {
  158. p := s[i*(13+1) : (i+1)*(13+1)-1]
  159. copy(res[i*13:], p)
  160. l, err := luhnBase32.generate(p)
  161. if err != nil {
  162. return "", err
  163. }
  164. if s[(i+1)*14-1] != byte(l) {
  165. return "", fmt.Errorf("%q: check digit incorrect", s)
  166. }
  167. }
  168. return string(res), nil
  169. }
  170. func chunkify(s string) string {
  171. chunks := len(s) / 7
  172. res := make([]byte, chunks*(7+1)-1)
  173. for i := 0; i < chunks; i++ {
  174. if i > 0 {
  175. res[i*(7+1)-1] = '-'
  176. }
  177. copy(res[i*(7+1):], s[i*7:(i+1)*7])
  178. }
  179. return string(res)
  180. }
  181. func unchunkify(s string) string {
  182. s = strings.Replace(s, "-", "", -1)
  183. s = strings.Replace(s, " ", "", -1)
  184. return s
  185. }
  186. func untypeoify(s string) string {
  187. s = strings.Replace(s, "0", "O", -1)
  188. s = strings.Replace(s, "1", "I", -1)
  189. s = strings.Replace(s, "8", "B", -1)
  190. return s
  191. }
  192. // DeviceIDs is a sortable slice of DeviceID
  193. type DeviceIDs []DeviceID
  194. func (l DeviceIDs) Len() int {
  195. return len(l)
  196. }
  197. func (l DeviceIDs) Less(a, b int) bool {
  198. return l[a].Compare(l[b]) == -1
  199. }
  200. func (l DeviceIDs) Swap(a, b int) {
  201. l[a], l[b] = l[b], l[a]
  202. }