forked from illuspas/Node-Media-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_core_utils.js
51 lines (46 loc) · 1.38 KB
/
node_core_utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// Created by Mingliang Chen on 17/8/23.
// illuspas[a]gmail.com
// Copyright (c) 2018 Nodemedia. All rights reserved.
//
const Crypto = require('crypto');
const EventEmitter = require('events');
const context = require('./node_core_ctx');
function generateNewSessionID() {
let sessionID = '';
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWKYZ0123456789';
const numPossible = possible.length;
do {
for (let i = 0; i < 8; i++) {
sessionID += possible.charAt((Math.random() * numPossible) | 0);
}
} while (context.sessions.has(sessionID))
return sessionID;
}
function genRandomName() {
let name = '';
const possible = 'abcdefghijklmnopqrstuvwxyz0123456789';
const numPossible = possible.length;
for (let i = 0; i < 4; i++) {
name += possible.charAt((Math.random() * numPossible) | 0);
}
return name;
}
function verifyAuth(signStr, streamId, secretKey) {
if (signStr === undefined) {
return false;
}
let now = Date.now() / 1000 | 0;
let exp = parseInt(signStr.split('-')[0]);
let shv = signStr.split('-')[1];
let str = streamId + '-' + exp + '-' + secretKey;
if (exp < now) {
return false;
}
let md5 = Crypto.createHash('md5');
let ohv = md5.update(str).digest('hex');
return shv === ohv;
}
module.exports.generateNewSessionID = generateNewSessionID;
module.exports.verifyAuth = verifyAuth;
module.exports.genRandomName = genRandomName;