|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | const uuid = require('uuid'); |
4 | | -const UMF_VERSION = 'UMF/1.4.5'; |
| 4 | +const crypto = require('crypto'); |
| 5 | +const UMF_VERSION = 'UMF/1.4.6'; |
5 | 6 |
|
6 | 7 | /** |
7 | 8 | * @name UMFMessage |
@@ -45,6 +46,20 @@ class UMFMessage { |
45 | 46 | return (Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)).toString(36); |
46 | 47 | } |
47 | 48 |
|
| 49 | + /** |
| 50 | + * @name signMessage |
| 51 | + * @summary sign message with cryptographic signature |
| 52 | + * @param {string} algo - such as 'sha256' |
| 53 | + * @param {string} sharedSecret - shared secret |
| 54 | + * @return {undefined} |
| 55 | + */ |
| 56 | + signMessage(algo, sharedSecret) { |
| 57 | + (this.message.signature) && delete this.message.signature; |
| 58 | + this.message.signature = crypto |
| 59 | + .createHmac(algo, sharedSecret) |
| 60 | + .update(JSON.stringify(this.message)) |
| 61 | + .digest('hex'); |
| 62 | + } |
48 | 63 |
|
49 | 64 | /** |
50 | 65 | * @name toJSON |
@@ -76,6 +91,9 @@ class UMFMessage { |
76 | 91 | if (this.message['rmid']) { |
77 | 92 | message['rmid'] = this.message['rmid']; |
78 | 93 | } |
| 94 | + if (this.message['signature']) { |
| 95 | + message['sig'] = this.message['signature']; |
| 96 | + } |
79 | 97 | if (this.message['timeout']) { |
80 | 98 | message['tmo'] = this.message['timeout']; |
81 | 99 | } |
@@ -147,6 +165,9 @@ function createMessageInstance(message) { |
147 | 165 | if (message.rmid) { |
148 | 166 | proxy.rmid = message.rmid; |
149 | 167 | } |
| 168 | + if (message.signature || message.sig) { |
| 169 | + proxy.signature = message.signature || message.sig; |
| 170 | + } |
150 | 171 | if (message.timeout || message.tmo) { |
151 | 172 | proxy.timeout = message.timeout || message.tmo; |
152 | 173 | } |
|
0 commit comments