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.

90 lines
1.9 KiB

7 years ago
  1. # Copyright 2013 Donald Stufft and individual contributors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import, division, print_function
  15. import base64
  16. import binascii
  17. class RawEncoder(object):
  18. @staticmethod
  19. def encode(data):
  20. return data
  21. @staticmethod
  22. def decode(data):
  23. return data
  24. class HexEncoder(object):
  25. @staticmethod
  26. def encode(data):
  27. return binascii.hexlify(data)
  28. @staticmethod
  29. def decode(data):
  30. return binascii.unhexlify(data)
  31. class Base16Encoder(object):
  32. @staticmethod
  33. def encode(data):
  34. return base64.b16encode(data)
  35. @staticmethod
  36. def decode(data):
  37. return base64.b16decode(data)
  38. class Base32Encoder(object):
  39. @staticmethod
  40. def encode(data):
  41. return base64.b32encode(data)
  42. @staticmethod
  43. def decode(data):
  44. return base64.b32decode(data)
  45. class Base64Encoder(object):
  46. @staticmethod
  47. def encode(data):
  48. return base64.b64encode(data)
  49. @staticmethod
  50. def decode(data):
  51. return base64.b64decode(data)
  52. class URLSafeBase64Encoder(object):
  53. @staticmethod
  54. def encode(data):
  55. return base64.urlsafe_b64encode(data)
  56. @staticmethod
  57. def decode(data):
  58. return base64.urlsafe_b64decode(data)
  59. class Encodable(object):
  60. def encode(self, encoder=RawEncoder):
  61. return encoder.encode(bytes(self))

Powered by TurnKey Linux.