UNPKG

1.38 kBPlain TextView Raw
1#!/usr/bin/env node
2
3'use strict'
4
5var stdin = require('get-stdin')
6var eccrypto = require('eccrypto')
7
8var command = process.argv[0]
9var script = process.argv[1]
10var public_key = process.argv[2]
11var message = process.argv[3]
12
13var pk, m
14
15stdin.buffer().then(function(s) {
16
17 if (!s.length && typeof message === 'undefined') {
18 return process.stdout.write(JSON.stringify({
19 error: 'public key or message not given'
20 }), function() {
21 process.exit(1)
22 })
23 }
24
25 m = s || message
26
27 if (!public_key) {
28 return process.stdout.write(JSON.stringify({
29 error: 'public key or message not given'
30 }), function() {
31 process.exit(1)
32 })
33 }
34
35 try {
36 pk = Buffer(public_key, 'hex')
37 } catch (err) {
38 return process.stdout.write(JSON.stringify({
39 error: 'public key is not a hexadecimal string'
40 }), function() {
41 process.exit(1)
42 })
43 }
44
45 return eccrypto.encrypt(pk, m)
46
47}).then(function(encrypted) {
48 process.stdout.write(JSON.stringify({
49 iv: encrypted.iv.toString('hex'),
50 ephemPublicKey: encrypted.ephemPublicKey.toString('hex'),
51 ciphertext: encrypted.ciphertext.toString('hex'),
52 mac: encrypted.mac.toString('hex')
53 }), function() {
54 process.exit(0)
55 })
56}).catch(function(err) {
57 process.stdout.write(JSON.stringify({
58 eccrypto_error: JSON.stringify(err)
59 }), function() {
60 process.exit(1)
61 })
62})