a zip code crypto-currency system good for red ONLY

sshpk-verify 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/usr/bin/env node
  2. // -*- mode: js -*-
  3. // vim: set filetype=javascript :
  4. // Copyright 2015 Joyent, Inc. All rights reserved.
  5. var dashdash = require('dashdash');
  6. var sshpk = require('../lib/index');
  7. var fs = require('fs');
  8. var path = require('path');
  9. var Buffer = require('safer-buffer').Buffer;
  10. var options = [
  11. {
  12. names: ['hash', 'H'],
  13. type: 'string',
  14. help: 'Hash algorithm (sha1, sha256, sha384, sha512)'
  15. },
  16. {
  17. names: ['verbose', 'v'],
  18. type: 'bool',
  19. help: 'Display verbose info about key and hash used'
  20. },
  21. {
  22. names: ['identity', 'i'],
  23. type: 'string',
  24. help: 'Path to (public) key to use'
  25. },
  26. {
  27. names: ['file', 'f'],
  28. type: 'string',
  29. help: 'Input filename'
  30. },
  31. {
  32. names: ['format', 't'],
  33. type: 'string',
  34. help: 'Signature format (asn1, ssh, raw)'
  35. },
  36. {
  37. names: ['signature', 's'],
  38. type: 'string',
  39. help: 'base64-encoded signature data'
  40. },
  41. {
  42. names: ['help', 'h'],
  43. type: 'bool',
  44. help: 'Shows this help text'
  45. }
  46. ];
  47. if (require.main === module) {
  48. var parser = dashdash.createParser({
  49. options: options
  50. });
  51. try {
  52. var opts = parser.parse(process.argv);
  53. } catch (e) {
  54. console.error('sshpk-verify: error: %s', e.message);
  55. process.exit(3);
  56. }
  57. if (opts.help || opts._args.length > 1) {
  58. var help = parser.help({}).trimRight();
  59. console.error('sshpk-verify: sign data using an SSH key\n');
  60. console.error(help);
  61. process.exit(3);
  62. }
  63. if (!opts.identity) {
  64. var help = parser.help({}).trimRight();
  65. console.error('sshpk-verify: the -i or --identity option ' +
  66. 'is required\n');
  67. console.error(help);
  68. process.exit(3);
  69. }
  70. if (!opts.signature) {
  71. var help = parser.help({}).trimRight();
  72. console.error('sshpk-verify: the -s or --signature option ' +
  73. 'is required\n');
  74. console.error(help);
  75. process.exit(3);
  76. }
  77. var keyData = fs.readFileSync(opts.identity);
  78. var key;
  79. try {
  80. key = sshpk.parseKey(keyData);
  81. } catch (e) {
  82. console.error('sshpk-verify: error loading key "' +
  83. opts.identity + '": ' + e.name + ': ' + e.message);
  84. process.exit(2);
  85. }
  86. var fmt = opts.format || 'asn1';
  87. var sigData = Buffer.from(opts.signature, 'base64');
  88. var sig;
  89. try {
  90. sig = sshpk.parseSignature(sigData, key.type, fmt);
  91. } catch (e) {
  92. console.error('sshpk-verify: error parsing signature: ' +
  93. e.name + ': ' + e.message);
  94. process.exit(2);
  95. }
  96. var hash = opts.hash || key.defaultHashAlgorithm();
  97. var verifier;
  98. try {
  99. verifier = key.createVerify(hash);
  100. } catch (e) {
  101. console.error('sshpk-verify: error creating verifier: ' +
  102. e.name + ': ' + e.message);
  103. process.exit(2);
  104. }
  105. if (opts.verbose) {
  106. console.error('sshpk-verify: using %s-%s with a %d bit key',
  107. key.type, hash, key.size);
  108. }
  109. var inFile = process.stdin;
  110. var inFileName = 'stdin';
  111. var inFilePath;
  112. if (opts.file) {
  113. inFilePath = opts.file;
  114. } else if (opts._args.length === 1) {
  115. inFilePath = opts._args[0];
  116. }
  117. if (inFilePath)
  118. inFileName = path.basename(inFilePath);
  119. try {
  120. if (inFilePath) {
  121. fs.accessSync(inFilePath, fs.R_OK);
  122. inFile = fs.createReadStream(inFilePath);
  123. }
  124. } catch (e) {
  125. console.error('sshpk-verify: error opening input file' +
  126. ': ' + e.name + ': ' + e.message);
  127. process.exit(2);
  128. }
  129. inFile.pipe(verifier);
  130. inFile.on('end', function () {
  131. var ret;
  132. try {
  133. ret = verifier.verify(sig);
  134. } catch (e) {
  135. console.error('sshpk-verify: error verifying data: ' +
  136. e.name + ': ' + e.message);
  137. process.exit(1);
  138. }
  139. if (ret) {
  140. console.error('OK');
  141. process.exit(0);
  142. }
  143. console.error('NOT OK');
  144. process.exit(1);
  145. });
  146. }