source: trip-planner-front/node_modules/sshpk/lib/ed-compat.js@ 84d0fbb

Last change on this file since 84d0fbb was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.3 KB
Line 
1// Copyright 2015 Joyent, Inc.
2
3module.exports = {
4 Verifier: Verifier,
5 Signer: Signer
6};
7
8var nacl = require('tweetnacl');
9var stream = require('stream');
10var util = require('util');
11var assert = require('assert-plus');
12var Buffer = require('safer-buffer').Buffer;
13var Signature = require('./signature');
14
15function Verifier(key, hashAlgo) {
16 if (hashAlgo.toLowerCase() !== 'sha512')
17 throw (new Error('ED25519 only supports the use of ' +
18 'SHA-512 hashes'));
19
20 this.key = key;
21 this.chunks = [];
22
23 stream.Writable.call(this, {});
24}
25util.inherits(Verifier, stream.Writable);
26
27Verifier.prototype._write = function (chunk, enc, cb) {
28 this.chunks.push(chunk);
29 cb();
30};
31
32Verifier.prototype.update = function (chunk) {
33 if (typeof (chunk) === 'string')
34 chunk = Buffer.from(chunk, 'binary');
35 this.chunks.push(chunk);
36};
37
38Verifier.prototype.verify = function (signature, fmt) {
39 var sig;
40 if (Signature.isSignature(signature, [2, 0])) {
41 if (signature.type !== 'ed25519')
42 return (false);
43 sig = signature.toBuffer('raw');
44
45 } else if (typeof (signature) === 'string') {
46 sig = Buffer.from(signature, 'base64');
47
48 } else if (Signature.isSignature(signature, [1, 0])) {
49 throw (new Error('signature was created by too old ' +
50 'a version of sshpk and cannot be verified'));
51 }
52
53 assert.buffer(sig);
54 return (nacl.sign.detached.verify(
55 new Uint8Array(Buffer.concat(this.chunks)),
56 new Uint8Array(sig),
57 new Uint8Array(this.key.part.A.data)));
58};
59
60function Signer(key, hashAlgo) {
61 if (hashAlgo.toLowerCase() !== 'sha512')
62 throw (new Error('ED25519 only supports the use of ' +
63 'SHA-512 hashes'));
64
65 this.key = key;
66 this.chunks = [];
67
68 stream.Writable.call(this, {});
69}
70util.inherits(Signer, stream.Writable);
71
72Signer.prototype._write = function (chunk, enc, cb) {
73 this.chunks.push(chunk);
74 cb();
75};
76
77Signer.prototype.update = function (chunk) {
78 if (typeof (chunk) === 'string')
79 chunk = Buffer.from(chunk, 'binary');
80 this.chunks.push(chunk);
81};
82
83Signer.prototype.sign = function () {
84 var sig = nacl.sign.detached(
85 new Uint8Array(Buffer.concat(this.chunks)),
86 new Uint8Array(Buffer.concat([
87 this.key.part.k.data, this.key.part.A.data])));
88 var sigBuf = Buffer.from(sig);
89 var sigObj = Signature.parse(sigBuf, 'ed25519', 'raw');
90 sigObj.hashAlgorithm = 'sha512';
91 return (sigObj);
92};
Note: See TracBrowser for help on using the repository browser.