twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

ed-compat.js (2335B)


      1 // Copyright 2015 Joyent, Inc.
      2 
      3 module.exports = {
      4 	Verifier: Verifier,
      5 	Signer: Signer
      6 };
      7 
      8 var nacl = require('tweetnacl');
      9 var stream = require('stream');
     10 var util = require('util');
     11 var assert = require('assert-plus');
     12 var Buffer = require('safer-buffer').Buffer;
     13 var Signature = require('./signature');
     14 
     15 function 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 }
     25 util.inherits(Verifier, stream.Writable);
     26 
     27 Verifier.prototype._write = function (chunk, enc, cb) {
     28 	this.chunks.push(chunk);
     29 	cb();
     30 };
     31 
     32 Verifier.prototype.update = function (chunk) {
     33 	if (typeof (chunk) === 'string')
     34 		chunk = Buffer.from(chunk, 'binary');
     35 	this.chunks.push(chunk);
     36 };
     37 
     38 Verifier.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 
     60 function 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 }
     70 util.inherits(Signer, stream.Writable);
     71 
     72 Signer.prototype._write = function (chunk, enc, cb) {
     73 	this.chunks.push(chunk);
     74 	cb();
     75 };
     76 
     77 Signer.prototype.update = function (chunk) {
     78 	if (typeof (chunk) === 'string')
     79 		chunk = Buffer.from(chunk, 'binary');
     80 	this.chunks.push(chunk);
     81 };
     82 
     83 Signer.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 };