diff --git a/index.js b/index.js index 23e3834..b3f1f1d 100644 --- a/index.js +++ b/index.js @@ -1,7 +1 @@ -var sign = require('./sign'); -var Writable = require('readable-stream').Writable; -var inherits = require('inherits'); -exports.createSign = createSign; -function createSign(algorithm) { - -} +require('./inject')(module.exports, require('crypto')); \ No newline at end of file diff --git a/inject.js b/inject.js new file mode 100644 index 0000000..540f632 --- /dev/null +++ b/inject.js @@ -0,0 +1,61 @@ +var sign = require('./sign'); +var verify = require('./verify'); +var Writable = require('readable-stream').Writable; +var inherits = require('inherits'); +var algos = require('./algos'); +module.exports = function (exports, crypto) { + exports.createSign = createSign; + function createSign(algorithm) { + var data = algos[algorithm]; + return new Sign(crypto.createHash(data.hash)); + } + exports.createVerify = createVerify; + function createVerify(algorithm) { + var data = algos[algorithm]; + return new Verify(crypto.createHash(data.hash)); + } +}; +inherits(Sign, Writable); +function Sign(hash) { + Writable.call(this) + this._hash = hash; +}; +Sign.prototype._write = function _write(data, _, done) { + this._hash.update(data); + done(); +}; +Sign.prototype.update = function update(data) { + this.write(data); +}; + +Sign.prototype.sign = function sign(key, enc) { + this.end(); + var hash = this._hash.digest(); + var sig = sign(hash, key); + if (enc) { + sig = sig.toString(enc); + } + return sig; +}; + +inherits(Verify, Writable); +function Verify(hash) { + Writable.call(this) + this._hash = hash; +}; +Verify.prototype._write = function _write(data, _, done) { + this._hash.update(data); + done(); +}; +Verify.prototype.update = function update(data) { + this.write(data); +}; + +Verify.prototype.verify = function verify(key, sig, enc) { + this.end(); + var hash = this._hash.digest(); + if (!Buffer.isBuffer(sig)) { + sig = new Buffer(sig, enc); + } + return verify(sig, hash, key); +}; \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..9778f42 --- /dev/null +++ b/readme.md @@ -0,0 +1,15 @@ +browserify-sign +=== + +a package to duplicate the functionality of node's crypto public key functions, much of this is based on [Fedor Indutny's](https://github.com/indutny) work on [tls.js](https://github.com/indutny/tls.js). + +# done + +- basic rsa signing and verifying with the right api + +# todo + +- tests to make sure we actually did it +- chinese remainder theorom? +- eliptical curve signing +- publicEncrypt and privateDecrypt? \ No newline at end of file