publicToIp6.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <https://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 Buffer.from(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. //console.log(keyBytes.toString('hex'));
  57. var hashOneBuff = Buffer.from(Crypto.createHash('sha512').update(keyBytes).digest('hex'), 'hex');
  58. var hashTwo = Crypto.createHash('sha512').update(hashOneBuff).digest('hex');
  59. var first16 = hashTwo.substring(0,32);
  60. var out = [];
  61. for (var i = 0; i < 8; i++) {
  62. out.push(first16.substring(i*4, i*4+4));
  63. }
  64. return out.join(':');
  65. };
  66. if (!module.parent) {
  67. process.argv.slice(2).map(function (arg, i) {
  68. if (arg.length !== 54) {
  69. console.error("argument %s [%s] is too short to be a valid public key", i + 2, arg);
  70. process.exit(1);
  71. }
  72. if (arg.slice(-2) !== ".k") {
  73. console.error("argument %s [%s] does not end with .k", i + 2, arg);
  74. process.exit(1);
  75. }
  76. return arg;
  77. }).forEach(function (arg) { console.log(convert(arg)); });
  78. }
  79. //console.log(convert('rjndc8rvg194ddf2j5v679cfjcpmsmhv8p022q3lvpym21cqwyh0.k'));