fingerprint.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. package zerotier
  14. //#cgo CFLAGS: -O3
  15. //#include "../../native/GoGlue.h"
  16. import "C"
  17. import (
  18. "encoding/base32"
  19. "errors"
  20. "strings"
  21. "unsafe"
  22. )
  23. var ztBase32 = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567").WithPadding(base32.NoPadding)
  24. type Fingerprint struct {
  25. Address Address `json:"address"`
  26. Hash [48]byte `json:"hash"`
  27. }
  28. func NewFingerprintFromString(fps string) (*Fingerprint, error) {
  29. fpb, err := ztBase32.DecodeString(strings.TrimSpace(strings.ToLower(fps)))
  30. if err != nil {
  31. return nil, err
  32. }
  33. if len(fpb) != 53 {
  34. return nil, errors.New("invalid fingerprint length")
  35. }
  36. var fp Fingerprint
  37. fp.Address, _ = NewAddressFromBytes(fpb[0:5])
  38. copy(fp.Hash[:],fpb[5:])
  39. return &fp, nil
  40. }
  41. func (fp *Fingerprint) String() string {
  42. var tmp [53]byte
  43. fp.Address.CopyTo(tmp[0:5])
  44. copy(tmp[5:],fp.Hash[:])
  45. return ztBase32.EncodeToString(tmp[:])
  46. }
  47. func (fp *Fingerprint) apiFingerprint() *C.ZT_Fingerprint {
  48. var apifp C.ZT_Fingerprint
  49. apifp.address = C.uint64_t(fp.Address)
  50. copy((*[48]byte)(unsafe.Pointer(&apifp.hash[0]))[:], fp.Hash[:])
  51. return &apifp
  52. }