1
0

publicToIp6.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. var Crypto = require('crypto');
  16. var numForAscii = [
  17. 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
  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. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,99,99,99,99,99,99,
  21. 99,99,10,11,12,99,13,14,15,99,16,17,18,19,20,99,
  22. 21,22,23,24,25,26,27,28,29,30,31,99,99,99,99,99,
  23. 99,99,10,11,12,99,13,14,15,99,16,17,18,19,20,99,
  24. 21,22,23,24,25,26,27,28,29,30,31,99,99,99,99,99,
  25. ];
  26. // see util/Base32.h
  27. var Base32_decode = function (input) {
  28. var output = [];
  29. var outputIndex = 0;
  30. var inputIndex = 0;
  31. var nextByte = 0;
  32. var bits = 0;
  33. while (inputIndex < input.length) {
  34. var o = input.charCodeAt(inputIndex);
  35. if (o & 0x80) { throw new Error(); }
  36. var b = numForAscii[o];
  37. inputIndex++;
  38. if (b > 31) { throw new Error("bad character " + input[inputIndex] + " in " + input); }
  39. nextByte |= (b << bits);
  40. bits += 5;
  41. if (bits >= 8) {
  42. output[outputIndex] = nextByte & 0xff;
  43. outputIndex++;
  44. bits -= 8;
  45. nextByte >>= 8;
  46. }
  47. }
  48. if (bits >= 5 || nextByte) {
  49. throw new Error("bits is " + bits + " and nextByte is " + nextByte);
  50. }
  51. return new Buffer(output);
  52. };
  53. var convert = module.exports.convert = function (pubKey) {
  54. if (pubKey.substring(pubKey.length-2) !== ".k") { throw new Error("key does not end with .k"); }
  55. keyBytes = Base32_decode(pubKey.substring(0, pubKey.length-2));
  56. var hashOneBuff = new Buffer(Crypto.createHash('sha512').update(keyBytes).digest('hex'), 'hex');
  57. var hashTwo = Crypto.createHash('sha512').update(hashOneBuff).digest('hex');
  58. var first16 = hashTwo.substring(0,32);
  59. var out = [];
  60. for (var i = 0; i < 8; i++) {
  61. out.push(first16.substring(i*4, i*4+4));
  62. }
  63. return out.join(':');
  64. };
  65. //console.log(convert('rjndc8rvg194ddf2j5v679cfjcpmsmhv8p022q3lvpym21cqwyh0.k'));