wormhole/lib/handshake.js

23 lines
No EOL
805 B
JavaScript

const crypto = require('crypto');
const constants = require('./constants');
const generateWebsocketAcceptValue = (websocketKey) => {
if (typeof websocketKey !== 'string' || typeof constants.handshakeGUID !== 'string') {
// TODO: maybe throw error?
return;
}
const concatenated = websocketKey + constants.handshakeGUID;
const sha1HashInBase64 = crypto.createHash('sha1').update(concatenated, 'binary').digest('base64');
return sha1HashInBase64;
};
const validateHeaders = ({ upgradeHeader, websocketVersion }) => {
if (upgradeHeader !== constants.upgradeHeaderRequirement) return false;
if (websocketVersion !== constants.websocketVersionRequirement) return false;
return true;
};
module.exports = { generateWebsocketAcceptValue, validateHeaders };