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