base32blob.go 985 B

1234567891011121314151617181920212223242526272829303132333435363738
  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: 2025-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. import (
  15. "encoding/json"
  16. "strings"
  17. )
  18. // Base32Blob is a byte array that JSON serializes to a Base32 string.
  19. type Base32Blob []byte
  20. // MarshalJSON returns this blob marshaled as a byte array or a string.
  21. func (b *Base32Blob) MarshalJSON() ([]byte, error) {
  22. return []byte("\""+Base32.EncodeToString(*b)+"\""), nil
  23. }
  24. // UnmarshalJSON unmarshals this blob from a JSON array or string.
  25. func (b *Base32Blob) UnmarshalJSON(j []byte) error {
  26. var b32 string
  27. err := json.Unmarshal(j, &b32)
  28. if err != nil {
  29. return err
  30. }
  31. *b, err = Base32.DecodeString(strings.TrimSpace(b32))
  32. return err
  33. }