Merge branch 'hotfix/21.56.9' into master
[gitter.git] / server / web / validated-message.js
blob36636858bffe05cede4ae77db7d97194444ee5f6
1 'use strict';
3 var env = require('gitter-web-env');
4 var config = env.config;
5 var crypto = require('crypto');
7 var secret = config.get('web:messageSecret');
9 function validate(unvalidatedMessage, check, defaultMessage) {
10 if (!unvalidatedMessage || !check) {
11 return defaultMessage;
14 unvalidatedMessage = String(unvalidatedMessage);
15 check = String(check);
16 var p = check.split(':');
17 if (p.length !== 2) {
18 return defaultMessage;
21 var checksum = p[0];
22 var salt = p[1];
24 var calculatedCheck = crypto
25 .createHash('md5')
26 .update(secret + salt + unvalidatedMessage, 'utf8')
27 .digest('base64');
29 if (calculatedCheck === checksum) {
30 return unvalidatedMessage;
33 return defaultMessage;
36 function getCheck(message) {
37 if (!message) return '';
39 var salt = crypto.randomBytes(6).toString('base64');
41 var check = crypto
42 .createHash('md5')
43 .update(secret + salt + message, 'utf8')
44 .digest('base64');
46 return check + ':' + salt;
49 module.exports = {
50 validate: validate,
51 getCheck: getCheck