1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict'
|
4 |
|
5 | var eccrypto = require('eccrypto')
|
6 |
|
7 | var command = process.argv[0]
|
8 | var script = process.argv[1]
|
9 | var private_key = process.argv[2]
|
10 | var public_key = process.argv[3]
|
11 |
|
12 | if (!(private_key && public_key)) {
|
13 | return process.stdout.write(JSON.stringify({
|
14 | error: 'private key or public key not given'
|
15 | }), function() {
|
16 | process.exit(1)
|
17 | })
|
18 | }
|
19 |
|
20 | var pka, pkb
|
21 |
|
22 | try {
|
23 | pka = Buffer(private_key, 'hex')
|
24 | pkb = Buffer(public_key, 'hex')
|
25 | } catch (err) {
|
26 | return process.stdout.write(JSON.stringify({
|
27 | error: 'public key, message or signature is not a hexadecimal string'
|
28 | }), function() {
|
29 | process.exit(1)
|
30 | })
|
31 | }
|
32 |
|
33 | eccrypto.derive(pka, pkb).then(function(shared_secret) {
|
34 | process.stdout.write(JSON.stringify({
|
35 | shared_secret: shared_secret.toString('hex')
|
36 | }), function() {
|
37 | process.exit(0)
|
38 | })
|
39 | }).catch(function(err) {
|
40 | process.stdout.write(JSON.stringify({
|
41 | eccrypto_error: JSON.stringify(err || true)
|
42 | }), function() {
|
43 | process.exit(1)
|
44 | })
|
45 | })
|