|
| 1 | +#!/usr/bin/env node |
| 2 | +import * as fs from 'fs' |
| 3 | +import { program } from 'commander' |
| 4 | +import { SSHAgentClient } from './lib/ssh_agent_client.js' |
| 5 | + |
| 6 | +program.name('ssh-crypt').description('Encryption through SSH Agent') |
| 7 | + |
| 8 | +program |
| 9 | + .command('encrypt') |
| 10 | + .description('Encrypt a file with your ssh-agent private key') |
| 11 | + .argument('<source>', 'file to encrypt') |
| 12 | + .argument('[destination]', 'output path (default to stdout)') |
| 13 | + .requiredOption('-k, --key <string>', 'select the first matching pubkey in the ssh-agent') |
| 14 | + .requiredOption('-s, --seed <string>', 'is used to generate the secret') |
| 15 | + .action(async (source, destination, options) => { |
| 16 | + const agent = new SSHAgentClient() |
| 17 | + const key = await agent.getIdentity(options.key) |
| 18 | + if (!key) { |
| 19 | + throw new Error(`No SSH key found for "${options.key}"!`) |
| 20 | + } |
| 21 | + const data = fs.readFileSync(source) |
| 22 | + const output = await agent.encrypt(key, options.seed, data) |
| 23 | + if (destination) { |
| 24 | + fs.writeFileSync(destination, output) |
| 25 | + } else { |
| 26 | + process.stdout.write(output) |
| 27 | + } |
| 28 | + }) |
| 29 | + |
| 30 | +program |
| 31 | + .command('decrypt') |
| 32 | + .description('Decrypt a file with your ssh-agent private key') |
| 33 | + .argument('<source>', 'file to decrypt') |
| 34 | + .argument('[destination]', 'output path (default to stdout)') |
| 35 | + .requiredOption('-k, --key <string>', 'select the first matching pubkey in the ssh-agent') |
| 36 | + .requiredOption('-s, --seed <string>', 'is used to generate the secret') |
| 37 | + .action(async (source, destination, options) => { |
| 38 | + const agent = new SSHAgentClient() |
| 39 | + const key = await agent.getIdentity(options.key) |
| 40 | + if (!key) { |
| 41 | + throw new Error(`No SSH key found for "${options.key}"!`) |
| 42 | + } |
| 43 | + const data = fs.readFileSync(source, { encoding: 'ascii' }) |
| 44 | + const output = await agent.decrypt(key, options.seed, data) |
| 45 | + if (destination) { |
| 46 | + fs.writeFileSync(destination, output) |
| 47 | + } else { |
| 48 | + process.stdout.write(output) |
| 49 | + } |
| 50 | + }) |
| 51 | + |
| 52 | +program.parse() |
0 commit comments