You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.8 KiB

4 years ago
  1. # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
  2. # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc.
  3. #
  4. # Permission to use, copy, modify, and distribute this software and its
  5. # documentation for any purpose with or without fee is hereby granted,
  6. # provided that the above copyright notice and this permission notice
  7. # appear in all copies.
  8. #
  9. # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
  10. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
  12. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  15. # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. """A place to store TSIG keys."""
  17. from dns._compat import maybe_decode, maybe_encode
  18. import base64
  19. import dns.name
  20. def from_text(textring):
  21. """Convert a dictionary containing (textual DNS name, base64 secret) pairs
  22. into a binary keyring which has (dns.name.Name, binary secret) pairs.
  23. @rtype: dict"""
  24. keyring = {}
  25. for keytext in textring:
  26. keyname = dns.name.from_text(keytext)
  27. secret = base64.decodestring(maybe_encode(textring[keytext]))
  28. keyring[keyname] = secret
  29. return keyring
  30. def to_text(keyring):
  31. """Convert a dictionary containing (dns.name.Name, binary secret) pairs
  32. into a text keyring which has (textual DNS name, base64 secret) pairs.
  33. @rtype: dict"""
  34. textring = {}
  35. for keyname in keyring:
  36. keytext = maybe_decode(keyname.to_text())
  37. secret = maybe_decode(base64.encodestring(keyring[keyname]))
  38. textring[keytext] = secret
  39. return textring