forked from kjur/jsrsasign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataverify
More file actions
executable file
·80 lines (64 loc) · 2.03 KB
/
dataverify
File metadata and controls
executable file
·80 lines (64 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env node
/*
* dataverify - verify signature of text or file
*
* Copyright (c) 2016 Kenji Urushima (kenji.urushima@gmail.com)
*
* This software is licensed under the terms of the MIT License.
* https://kjur.github.io/jsrsasign/license
*
* The above copyright and license notice shall be
* included in all copies or substantial portions of the Software.
*
* Please use '-h' option for this script usage.
* ---------------------------------------------------------
* DESCRIPTION
* This script verifies a signature of a text or file data by
* specified public key and signature algorithm.
*
* USAGE
* % dataverify aaa.txt a.pub a.sig
* % dataverify aaa.txt a.pub a256.sig SHA256withRSA
* % dataverify "test" a.pub test.sig
*
* Resulted signature can also be verified by OpenSSL.
* % openssl dgst -sha1 -verify a.pub -signature a.sig aaa.txt
*/
var program = require('commander');
var rs = require('jsrsasign');
var rsu = require('jsrsasign-util');
var path = require('path');
program
.version('1.0.0 (2016-Nov-19)')
.usage('[options] <text or file> <public key> <input signature file> [sig alg]')
.description('verify signature of text or file with specified public and sig algorithm.')
.parse(process.argv);
if (program.args.length !== 3 && program.args.length !== 4)
throw "wrong number of arguments";
var textOrFile = program.args[0];
var pubFile = program.args[1];
var sigFile = program.args[2];
var hashAlg = "SHA1withRSA";
if (program.args.length === 4) hashAlg = program.args[3];
// 1. public key
var pubPEM = rsu.readFile(pubFile);
var pub = rs.KEYUTIL.getKey(pubPEM);
//console.log(pub);
// 2. data to be verifid
var text;
try {
text = rsu.readFile(textOrFile);
} catch(ex) {
text = textOrFile;
}
// 3. load signature
var sigHex = rsu.readFileHexByBin(sigFile);
var sig = new rs.KJUR.crypto.Signature({alg: hashAlg});
sig.init(pub);
sig.updateString(text);
var isValid = sig.verify(sigHex);
if (isValid) {
console.log("signature is valid");
} else {
console.log("signature is invalid");
}