-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5091f2e
commit d2a3f77
Showing
9 changed files
with
507 additions
and
481 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
'use strict' | ||
var sign = require('./sign') | ||
var verify = require('./verify') | ||
var stream = require('stream') | ||
var inherits = require('inherits') | ||
var _algos = require('./algos') | ||
var createHash = require('create-hash') | ||
var algos = {} | ||
Object.keys(_algos).forEach(function (key) { | ||
algos[key] = algos[key.toLowerCase()] = _algos[key] | ||
}) | ||
|
||
exports.createSign = exports.Sign = createSign | ||
|
||
function createSign (algorithm) { | ||
return new Sign(algorithm) | ||
} | ||
|
||
exports.createVerify = exports.Verify = createVerify | ||
|
||
function createVerify (algorithm) { | ||
return new Verify(algorithm) | ||
} | ||
|
||
inherits(Sign, stream.Writable) | ||
|
||
function Sign (algorithm) { | ||
stream.Writable.call(this) | ||
var data = algos[algorithm] | ||
if (!data) | ||
throw new Error('Unknown message digest') | ||
|
||
this._hashType = data.hash | ||
this._hash = createHash(data.hash) | ||
this._tag = data.id | ||
} | ||
|
||
Sign.prototype._write = function _write (data, _, done) { | ||
this._hash.update(data) | ||
done() | ||
} | ||
|
||
Sign.prototype.update = function update (data, enc) { | ||
if (typeof data === 'string') | ||
data = new Buffer(data, enc) | ||
this._hash.update(data) | ||
return this | ||
} | ||
|
||
Sign.prototype.sign = function signMethod (key, enc) { | ||
this.end() | ||
var hash = this._hash.digest() | ||
var sig = sign(Buffer.concat([this._tag, hash]), key, this._hashType) | ||
if (enc) { | ||
sig = sig.toString(enc) | ||
} | ||
return sig | ||
} | ||
|
||
inherits(Verify, stream.Writable) | ||
function Verify (algorithm) { | ||
stream.Writable.call(this) | ||
var data = algos[algorithm] | ||
if (!data) | ||
throw new Error('Unknown message digest') | ||
|
||
this._hash = createHash(data.hash) | ||
this._tag = data.id | ||
} | ||
|
||
Verify.prototype._write = function _write (data, _, done) { | ||
this._hash.update(data) | ||
done() | ||
} | ||
|
||
Verify.prototype.update = function update (data, enc) { | ||
if (typeof data === 'string') | ||
data = new Buffer(data, enc) | ||
|
||
this._hash.update(data) | ||
return this | ||
} | ||
|
||
Verify.prototype.verify = function verifyMethod (key, sig, enc) { | ||
this.end() | ||
var hash = this._hash.digest() | ||
if (typeof sic === 'string') | ||
sig = new Buffer(sig, enc) | ||
|
||
return verify(sig, Buffer.concat([this._tag, hash]), key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
require('./inject')(module.exports, require('crypto')); | ||
var crypto = require('crypto') | ||
|
||
exports.createSign = crypto.createSign | ||
exports.Sign = crypto.Sign | ||
|
||
exports.createVerify = crypto.createVerify | ||
exports.Verify = crypto.Verify |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.