1
0

publicToIp6.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python2
  2. # You may redistribute this program and/or modify it under the terms of
  3. # the GNU General Public License as published by the Free Software Foundation,
  4. # either version 3 of the License, or (at your option) any later version.
  5. #
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. from hashlib import sha512;
  14. # see util/Base32.h
  15. def Base32_decode(input):
  16. output = bytearray(len(input));
  17. numForAscii = [
  18. 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
  19. 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
  20. 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
  21. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,99,99,99,99,99,99,
  22. 99,99,10,11,12,99,13,14,15,99,16,17,18,19,20,99,
  23. 21,22,23,24,25,26,27,28,29,30,31,99,99,99,99,99,
  24. 99,99,10,11,12,99,13,14,15,99,16,17,18,19,20,99,
  25. 21,22,23,24,25,26,27,28,29,30,31,99,99,99,99,99,
  26. ];
  27. outputIndex = 0;
  28. inputIndex = 0;
  29. nextByte = 0;
  30. bits = 0;
  31. while (inputIndex < len(input)):
  32. o = ord(input[inputIndex]);
  33. if (o & 0x80): raise ValueError;
  34. b = numForAscii[o];
  35. inputIndex += 1;
  36. if (b > 31): raise ValueError("bad character " + input[inputIndex]);
  37. nextByte |= (b << bits);
  38. bits += 5;
  39. if (bits >= 8):
  40. output[outputIndex] = nextByte & 0xff;
  41. outputIndex += 1;
  42. bits -= 8;
  43. nextByte >>= 8;
  44. if (bits >= 5 or nextByte):
  45. raise ValueError("bits is " + str(bits) + " and nextByte is " + str(nextByte));
  46. return buffer(output, 0, outputIndex);
  47. def PublicToIp6_convert(pubKey):
  48. if pubKey[-2:] != ".k":
  49. raise ValueError("key does not end with .k")
  50. keyBytes = Base32_decode(pubKey[:-2])
  51. hashOne = sha512(keyBytes).digest()
  52. hashTwo = sha512(hashOne).hexdigest()
  53. return ":".join([hashTwo[i:i+4] for i in range(0, 32, 4)])