加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
node_core_utils.js 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
illuspas 提交于 2018-06-20 16:59 . u
//
// 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;
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化