1 // Copyright 2015 Joyent, Inc.
9 var stream = require('stream');
10 var util = require('util');
11 var assert = require('assert-plus');
12 var Signature = require('./signature');
14 function Verifier(key, hashAlgo) {
15 if (nacl === undefined)
16 nacl = require('tweetnacl');
18 if (hashAlgo.toLowerCase() !== 'sha512')
19 throw (new Error('ED25519 only supports the use of ' +
25 stream.Writable.call(this, {});
27 util.inherits(Verifier, stream.Writable);
29 Verifier.prototype._write = function (chunk, enc, cb) {
30 this.chunks.push(chunk);
34 Verifier.prototype.update = function (chunk) {
35 if (typeof (chunk) === 'string')
36 chunk = new Buffer(chunk, 'binary');
37 this.chunks.push(chunk);
40 Verifier.prototype.verify = function (signature, fmt) {
42 if (Signature.isSignature(signature, [2, 0])) {
43 if (signature.type !== 'ed25519')
45 sig = signature.toBuffer('raw');
47 } else if (typeof (signature) === 'string') {
48 sig = new Buffer(signature, 'base64');
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'));
56 return (nacl.sign.detached.verify(
57 new Uint8Array(Buffer.concat(this.chunks)),
59 new Uint8Array(this.key.part.R.data)));
62 function Signer(key, hashAlgo) {
63 if (nacl === undefined)
64 nacl = require('tweetnacl');
66 if (hashAlgo.toLowerCase() !== 'sha512')
67 throw (new Error('ED25519 only supports the use of ' +
73 stream.Writable.call(this, {});
75 util.inherits(Signer, stream.Writable);
77 Signer.prototype._write = function (chunk, enc, cb) {
78 this.chunks.push(chunk);
82 Signer.prototype.update = function (chunk) {
83 if (typeof (chunk) === 'string')
84 chunk = new Buffer(chunk, 'binary');
85 this.chunks.push(chunk);
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';