Skip to content

Commit d32f69c

Browse files
committed
Add verify to CLI and Readme
This changes the `signcode` script to expect one of two commands, 'sign' or 'verify'. Uses an up-to-date version of yargs to simplify argument processing in `cli.js`.
1 parent 270714d commit d32f69c

File tree

3 files changed

+97
-70
lines changed

3 files changed

+97
-70
lines changed

README.md

+19-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,17 @@ signcode.sign(options, function (error) {
3636
console.log(options.path + ' is now signed')
3737
}
3838
})
39+
40+
signcode.verify({ path: '/Users/kevin/apps/myapp.exe' }, function (error) {
41+
if (error) {
42+
console.error('Not signed', error.message)
43+
} else {
44+
console.log(options.path + ' is signed')
45+
}
46+
})
3947
```
4048

41-
## Options
49+
### Signing Options
4250

4351
| Name | Type | Required | Description |
4452
| :------------- | :-------- | :------- | :-------------------------- |
@@ -52,14 +60,22 @@ signcode.sign(options, function (error) {
5260
| `passwordPath` | `String` | No | Path to a file containing the password for the certificate or key. |
5361
| `site` | `String` | No | Website URL to include in the signature. |
5462

55-
## Command Line
63+
### Verification Options
64+
65+
| Name | Type | Required | Description |
66+
| :------------- | :-------- | :------- | :-------------------------- |
67+
| `path` | `String` | Yes | File path to executable to verify. |
68+
| `hash` | `String` | No | Certificate fingerprint to expect on executable. |
69+
70+
### Command Line Example
5671

5772
```sh
58-
signcode /Users/kevin/apps/myapp.exe \
73+
signcode sign /Users/kevin/apps/myapp.exe \
5974
--cert /Users/kevin/certs/cert.p12 \
6075
--prompt \
6176
--name 'My App' \
6277
--url 'http://birthday.pizza'
78+
signcode verify /Users/kevin/apps/myapp.exe
6379
```
6480

6581
Run `signcode -h` to see all the supported options.

cli.js

+77-66
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,44 @@ var yargs = require('yargs')
77

88
var metadata = require('./package')
99

10-
var args = parseArgs()
11-
var fileToSign = args.argv._[0]
12-
if (!fileToSign) {
13-
args.showHelp()
14-
process.exit(1)
15-
}
16-
fileToSign = path.resolve(fileToSign)
10+
processCommand()
11+
12+
function sign (argv) {
13+
var options = {
14+
cert: path.resolve(argv.cert),
15+
hash: ['sha1', 'sha256'],
16+
key: argv.key ? path.resolve(argv.key) : argv.key,
17+
name: argv.name,
18+
overwrite: true,
19+
password: argv.password,
20+
path: path.resolve(argv.file_to_sign),
21+
site: argv.url
22+
}
1723

18-
var options = {
19-
cert: path.resolve(args.argv.cert),
20-
hash: ['sha1', 'sha256'],
21-
key: args.argv.key ? path.resolve(args.argv.key) : args.argv.key,
22-
name: args.argv.name,
23-
overwrite: true,
24-
password: args.argv.password,
25-
path: fileToSign,
26-
site: args.argv.url
24+
if (argv.prompt) {
25+
promptForPassword(function (password) {
26+
options.password = password
27+
signcode.sign(options, exitIfError)
28+
})
29+
} else {
30+
signcode.sign(options, exitIfError)
31+
}
2732
}
2833

29-
if (args.argv.prompt) {
30-
promptForPassword(function (password) {
31-
options.password = password
32-
sign(options)
33-
})
34-
} else {
35-
sign(options)
34+
function verify (argv) {
35+
var options = {
36+
path: path.resolve(argv.file_to_verify),
37+
hash: argv.hash
38+
}
39+
40+
signcode.verify(options, exitIfError)
3641
}
3742

38-
function sign (options) {
39-
signcode.sign(options, function (error) {
40-
if (error) {
41-
console.error(error.message || error)
42-
process.exit(1)
43-
}
44-
})
43+
function exitIfError (error) {
44+
if (error) {
45+
console.error(error.message || error)
46+
process.exit(1)
47+
}
4548
}
4649

4750
function promptForPassword (callback) {
@@ -56,45 +59,53 @@ function promptForPassword (callback) {
5659
}
5760
prompt.start()
5861
prompt.get(promptConfig, function (error, result) {
59-
if (error) {
60-
console.error(error.message || error)
61-
process.exit(1)
62-
}
62+
exitIfError(error)
6363
callback(result.password)
6464
})
6565
}
6666

67-
function parseArgs () {
68-
return yargs
69-
.usage(metadata.name + ' file_to_sign [args]\n\nSign Windows executables from a Mac.\nVersion ' + metadata.version)
70-
.option('cert', {
71-
alias: 'c',
72-
demand: true,
73-
describe: 'Path to a .pem, .pfx, or .p12 certificate file',
74-
type: 'string'
75-
})
76-
.option('key', {
77-
alias: 'k',
78-
describe: 'Path to .pem key file',
79-
type: 'string'
80-
})
81-
.option('name', {
82-
alias: 'n',
83-
describe: 'Application name',
84-
type: 'string'
85-
})
86-
.option('password', {
87-
describe: 'Password to use for certificate/key pair',
88-
type: 'string'
89-
})
90-
.option('prompt', {
91-
describe: 'Prompt for a password',
92-
type: 'boolean'
93-
})
94-
.option('url', {
95-
alias: 'u',
96-
describe: 'Application URL',
97-
type: 'string'
98-
})
67+
function processCommand () {
68+
yargs
69+
.usage(metadata.name + ' <command> path_to_executable [args]\n\nSign Windows executables from a Mac.\nVersion ' + metadata.version)
70+
.command('sign <file_to_sign>', 'sign an executable', {
71+
cert: {
72+
alias: 'c',
73+
demand: true,
74+
describe: 'Path to a .pem, .pfx, or .p12 certificate file',
75+
type: 'string'
76+
},
77+
key: {
78+
alias: 'k',
79+
describe: 'Path to .pem key file',
80+
type: 'string'
81+
},
82+
name: {
83+
alias: 'n',
84+
describe: 'Application name',
85+
type: 'string'
86+
},
87+
password: {
88+
describe: 'Password to use for certificate/key pair',
89+
type: 'string'
90+
},
91+
prompt: {
92+
describe: 'Prompt for a password',
93+
type: 'boolean'
94+
},
95+
url: {
96+
alias: 'u',
97+
describe: 'Application URL',
98+
type: 'string'
99+
}
100+
}, sign)
101+
.command('verify <file_to_verify> [args]', 'verify the signature on an executable', {
102+
hash: {
103+
alias: 'h',
104+
describe: 'Certificate fingerprint to expect on executable',
105+
type: 'string'
106+
}
107+
}, verify)
108+
.demandCommand()
99109
.help('help')
110+
.argv
100111
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
},
2020
"dependencies": {
2121
"prompt": "^1.0.0",
22-
"yargs": "^4.6.0"
22+
"yargs": "^7.0.2"
2323
}
2424
}

0 commit comments

Comments
 (0)