1 |
3a515b92
|
cagy
|
var createHash = require('create-hash')
|
2 |
|
|
var stream = require('stream')
|
3 |
|
|
var inherits = require('inherits')
|
4 |
|
|
var sign = require('./sign')
|
5 |
|
|
var verify = require('./verify')
|
6 |
|
|
|
7 |
|
|
var algorithms = require('./algorithms.json')
|
8 |
|
|
Object.keys(algorithms).forEach(function (key) {
|
9 |
|
|
algorithms[key].id = new Buffer(algorithms[key].id, 'hex')
|
10 |
|
|
algorithms[key.toLowerCase()] = algorithms[key]
|
11 |
|
|
})
|
12 |
|
|
|
13 |
|
|
function Sign (algorithm) {
|
14 |
|
|
stream.Writable.call(this)
|
15 |
|
|
|
16 |
|
|
var data = algorithms[algorithm]
|
17 |
|
|
if (!data) throw new Error('Unknown message digest')
|
18 |
|
|
|
19 |
|
|
this._hashType = data.hash
|
20 |
|
|
this._hash = createHash(data.hash)
|
21 |
|
|
this._tag = data.id
|
22 |
|
|
this._signType = data.sign
|
23 |
|
|
}
|
24 |
|
|
inherits(Sign, stream.Writable)
|
25 |
|
|
|
26 |
|
|
Sign.prototype._write = function _write (data, _, done) {
|
27 |
|
|
this._hash.update(data)
|
28 |
|
|
done()
|
29 |
|
|
}
|
30 |
|
|
|
31 |
|
|
Sign.prototype.update = function update (data, enc) {
|
32 |
|
|
if (typeof data === 'string') data = new Buffer(data, enc)
|
33 |
|
|
|
34 |
|
|
this._hash.update(data)
|
35 |
|
|
return this
|
36 |
|
|
}
|
37 |
|
|
|
38 |
|
|
Sign.prototype.sign = function signMethod (key, enc) {
|
39 |
|
|
this.end()
|
40 |
|
|
var hash = this._hash.digest()
|
41 |
|
|
var sig = sign(hash, key, this._hashType, this._signType, this._tag)
|
42 |
|
|
|
43 |
|
|
return enc ? sig.toString(enc) : sig
|
44 |
|
|
}
|
45 |
|
|
|
46 |
|
|
function Verify (algorithm) {
|
47 |
|
|
stream.Writable.call(this)
|
48 |
|
|
|
49 |
|
|
var data = algorithms[algorithm]
|
50 |
|
|
if (!data) throw new Error('Unknown message digest')
|
51 |
|
|
|
52 |
|
|
this._hash = createHash(data.hash)
|
53 |
|
|
this._tag = data.id
|
54 |
|
|
this._signType = data.sign
|
55 |
|
|
}
|
56 |
|
|
inherits(Verify, stream.Writable)
|
57 |
|
|
|
58 |
|
|
Verify.prototype._write = function _write (data, _, done) {
|
59 |
|
|
this._hash.update(data)
|
60 |
|
|
done()
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
Verify.prototype.update = function update (data, enc) {
|
64 |
|
|
if (typeof data === 'string') data = new Buffer(data, enc)
|
65 |
|
|
|
66 |
|
|
this._hash.update(data)
|
67 |
|
|
return this
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
Verify.prototype.verify = function verifyMethod (key, sig, enc) {
|
71 |
|
|
if (typeof sig === 'string') sig = new Buffer(sig, enc)
|
72 |
|
|
|
73 |
|
|
this.end()
|
74 |
|
|
var hash = this._hash.digest()
|
75 |
|
|
return verify(sig, hash, key, this._signType, this._tag)
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
function createSign (algorithm) {
|
79 |
|
|
return new Sign(algorithm)
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
function createVerify (algorithm) {
|
83 |
|
|
return new Verify(algorithm)
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
module.exports = {
|
87 |
|
|
Sign: createSign,
|
88 |
|
|
Verify: createVerify,
|
89 |
|
|
createSign: createSign,
|
90 |
|
|
createVerify: createVerify
|
91 |
|
|
}
|