Skip to content

Commit 2030ae1

Browse files
authored
Support for HMAC signatures on UMF messages (#159)
1 parent 29f1d18 commit 2030ae1

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/umfmessage.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

33
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';
56

67
/**
78
* @name UMFMessage
@@ -45,6 +46,20 @@ class UMFMessage {
4546
return (Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)).toString(36);
4647
}
4748

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+
}
4863

4964
/**
5065
* @name toJSON
@@ -76,6 +91,9 @@ class UMFMessage {
7691
if (this.message['rmid']) {
7792
message['rmid'] = this.message['rmid'];
7893
}
94+
if (this.message['signature']) {
95+
message['sig'] = this.message['signature'];
96+
}
7997
if (this.message['timeout']) {
8098
message['tmo'] = this.message['timeout'];
8199
}
@@ -147,6 +165,9 @@ function createMessageInstance(message) {
147165
if (message.rmid) {
148166
proxy.rmid = message.rmid;
149167
}
168+
if (message.signature || message.sig) {
169+
proxy.signature = message.signature || message.sig;
170+
}
150171
if (message.timeout || message.tmo) {
151172
proxy.timeout = message.timeout || message.tmo;
152173
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hydra",
3-
"version": "1.4.27",
3+
"version": "1.4.28",
44
"license": "MIT",
55
"author": "Carlos Justiniano",
66
"contributors": "https://github.com/flywheelsports/hydra/graphs/contributors",

0 commit comments

Comments
 (0)