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