rewrite to a module
This commit is contained in:
+18
-17
@@ -1,5 +1,4 @@
|
||||
const crypto = require('crypto');
|
||||
|
||||
import crypto from "crypto";
|
||||
/**
|
||||
* hashes with the secure hashing algorithm 256
|
||||
* @param {string} str string to hash
|
||||
@@ -7,19 +6,21 @@ const crypto = require('crypto');
|
||||
* @param {number} num amount of times to hash, defaults to 1
|
||||
* @returns {string} base64 digested hash
|
||||
*/
|
||||
function SHA256(str,salt,num) {
|
||||
if(!num && num!==0)num=1;
|
||||
if(!str)return;
|
||||
let ret = str;
|
||||
for (let i = 0; i < num; i++) {
|
||||
ret = crypto
|
||||
.createHash("sha256")
|
||||
.update(ret+salt)
|
||||
.digest("base64");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
SHA256: SHA256
|
||||
function SHA256(str, salt, num) {
|
||||
if (!num && num !== 0)
|
||||
num = 1;
|
||||
if (!str)
|
||||
return;
|
||||
let ret = str;
|
||||
for (let i = 0; i < num; i++) {
|
||||
ret = crypto
|
||||
.createHash("sha256")
|
||||
.update(ret + salt)
|
||||
.digest("base64");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
export { SHA256 };
|
||||
export default {
|
||||
SHA256: SHA256
|
||||
};
|
||||
|
||||
+9
-10
@@ -1,15 +1,14 @@
|
||||
const fs = require('fs');
|
||||
const config = JSON.parse(fs.readFileSync("server_config.json"))
|
||||
|
||||
import fs from "fs";
|
||||
const config = JSON.parse(fs.readFileSync("server_config.json"));
|
||||
/**
|
||||
* gets ip of a request
|
||||
* @param {request} req
|
||||
* @param {request} req
|
||||
* @returns ip of the given request, after taking preferred headers into account
|
||||
*/
|
||||
function getIP(req) {
|
||||
function getIP(req) {
|
||||
let ip = req.socket.remoteAddress;
|
||||
if(req.headers[config.preferred_ip_header] != undefined && ip == config.only_prefer_when_ip)ip = req.headers[config.preferred_ip_header]
|
||||
return ip
|
||||
}
|
||||
|
||||
module.exports = getIP
|
||||
if (req.headers[config.preferred_ip_header] != undefined && ip == config.only_prefer_when_ip)
|
||||
ip = req.headers[config.preferred_ip_header];
|
||||
return ip;
|
||||
}
|
||||
export default getIP;
|
||||
|
||||
+34
-33
@@ -1,7 +1,7 @@
|
||||
const signature = require('cookie-signature')
|
||||
const fs = require('fs');
|
||||
const cookiesecret = fs.readFileSync("cookiesecret.txt").toString()
|
||||
const getIP = require("./getip.js")
|
||||
import * as signature from "cookie-signature";
|
||||
import fs from "fs";
|
||||
import getIP from "./getip.js";
|
||||
const cookiesecret = fs.readFileSync("cookiesecret.txt").toString();
|
||||
/**
|
||||
* usignes a string
|
||||
* @param {string} text text to unsign
|
||||
@@ -9,40 +9,41 @@ const getIP = require("./getip.js")
|
||||
* @param {response} res response object
|
||||
* @return {string/boolean} unsigned text, or if unsigning was unsuccessful, false
|
||||
*/
|
||||
function unsign(text,req,res) {
|
||||
let ip = getIP(req)
|
||||
let unsigned = signature.unsign(text,cookiesecret+ip)
|
||||
if(!unsigned) {
|
||||
return false
|
||||
}
|
||||
return unsigned
|
||||
function unsign(text, req, res) {
|
||||
let ip = getIP(req);
|
||||
let unsigned = signature.unsign(text, cookiesecret + ip);
|
||||
if (!unsigned) {
|
||||
return false;
|
||||
}
|
||||
return unsigned;
|
||||
}
|
||||
|
||||
/**
|
||||
* unsignes the auth cookie of a request, also sends json response if auth cookie was invalid
|
||||
* @param {request} req request object
|
||||
* @param {response} res response object
|
||||
* @return {string/boolean} unsigned cookie, or if unsigning was unsuccessful, false
|
||||
*/
|
||||
function getunsigned(req,res) {
|
||||
let cookie = req.cookies.AUTH_COOKIE
|
||||
if(!cookie){
|
||||
res.status(400)
|
||||
res.json({"error":"you are not logged in! (no cookie)"})
|
||||
return
|
||||
}
|
||||
let unsigned = unsign(cookie,req,res)
|
||||
if(!unsigned){
|
||||
try {
|
||||
res.status(400)
|
||||
res.json({"error":"Bad auth cookie set"})
|
||||
} catch (ignored) {} //sometimes it errors, gotta debug soon
|
||||
return false
|
||||
}
|
||||
return decodeURIComponent(unsigned)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
unsign: unsign,
|
||||
getunsigned: getunsigned
|
||||
function getunsigned(req, res) {
|
||||
let cookie = req.cookies.AUTH_COOKIE;
|
||||
if (!cookie) {
|
||||
res.status(400);
|
||||
res.json({ "error": "you are not logged in! (no cookie)" });
|
||||
return;
|
||||
}
|
||||
let unsigned = unsign(cookie, req, res);
|
||||
if (!unsigned) {
|
||||
try {
|
||||
res.status(400);
|
||||
res.json({ "error": "Bad auth cookie set" });
|
||||
}
|
||||
catch (ignored) { } //sometimes it errors, gotta debug soon
|
||||
return false;
|
||||
}
|
||||
return decodeURIComponent(unsigned);
|
||||
}
|
||||
export { unsign };
|
||||
export { getunsigned };
|
||||
export default {
|
||||
unsign: unsign,
|
||||
getunsigned: getunsigned
|
||||
};
|
||||
|
||||
+9
-17
@@ -1,27 +1,19 @@
|
||||
function XOR_hex(a, b) {
|
||||
var res = "",
|
||||
i = a.length,
|
||||
j = b.length;
|
||||
while (i-->0 && j-->0)
|
||||
var res = "", i = a.length, j = b.length;
|
||||
while (i-- > 0 && j-- > 0)
|
||||
res = (parseInt(a.charAt(i), 16) ^ parseInt(b.charAt(j), 16)).toString(16) + res;
|
||||
return res;
|
||||
}
|
||||
|
||||
function hexEncode(a){
|
||||
function hexEncode(a) {
|
||||
let hex;
|
||||
|
||||
let result = "";
|
||||
for (let i=0; i<a.length; i++) {
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
hex = a.charCodeAt(i).toString(16);
|
||||
result += ("000"+hex).slice(-4);
|
||||
result += ("000" + hex).slice(-4);
|
||||
}
|
||||
|
||||
return result
|
||||
return result;
|
||||
}
|
||||
|
||||
function xor(a,b) {
|
||||
return XOR_hex(hexEncode(a),hexEncode(b)).toString("hex")
|
||||
function xor(a, b) {
|
||||
return XOR_hex(hexEncode(a), hexEncode(b)).toString("hex");
|
||||
}
|
||||
|
||||
module.exports = xor
|
||||
|
||||
export default xor;
|
||||
|
||||
Reference in New Issue
Block a user