You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
4.4 KiB
176 lines
4.4 KiB
import crypto from 'crypto';
|
|
|
|
export class _Crypto
|
|
{
|
|
constructor(User, Password)
|
|
{
|
|
this.salt = 'H$44Q3RVCd9X8Ef63tB4';
|
|
this.secret = 'mYFUZX9NSx7K74r7Jh@O';
|
|
this.pepper = String.fromCharCode(this.getRandomInt(65, 90));
|
|
this.password = Password;
|
|
this.user = User;
|
|
this.algorithm = 'aes-192-cbc';
|
|
|
|
}
|
|
get Hash() // to use on register
|
|
{
|
|
return this.hash()
|
|
}
|
|
hash(p)
|
|
{
|
|
|
|
return crypto.createHmac('sha256', this.secret).update(this.user+this.password+this.salt + (p?p:this.pepper) ).digest('hex');
|
|
|
|
}
|
|
eHash(Email)
|
|
{
|
|
return new Buffer.from(Email).toString('base64');
|
|
}
|
|
|
|
decrypt(eHash)
|
|
{
|
|
return new Buffer.from(eHash, 'base64').toString('utf8');
|
|
}
|
|
getRandomInt(min, max)
|
|
{
|
|
min = Math.ceil(min);
|
|
max = Math.floor(max);
|
|
return Math.floor(Math.random() * (max - min)) + min;
|
|
}
|
|
//TODO: compare(hash)
|
|
//Get hash from db run through the peper possibilties and see if there is a match.
|
|
//return hash if true, null if false
|
|
|
|
}
|
|
|
|
export class Epoch
|
|
{
|
|
/**
|
|
*
|
|
* @param {Number} Days - Must be and integer.
|
|
*/
|
|
constructor(Days)
|
|
{
|
|
this.Days = (Days*(24*60*60));
|
|
this.epoch = this.epocher();
|
|
}
|
|
epocher()
|
|
{
|
|
return Math.floor(this.Days + Date.now()/1000)
|
|
}
|
|
/**
|
|
* @param {String} time - Must follow the format of '18:12'.
|
|
*/
|
|
sEpoch(time)
|
|
{
|
|
let hours = time.split(':')[0];
|
|
let minutes = time.split(':')[1];
|
|
return Math.floor(this.next(hours, minutes)/1000);
|
|
}
|
|
/**
|
|
* @param {Number} time - Must be and integer.
|
|
*/
|
|
toDate(time)
|
|
{
|
|
//console.log('classes:toDate:', time)
|
|
const d = new Date(time*1000);
|
|
return d.getHours()+':'+ d.getMinutes();
|
|
}
|
|
/**
|
|
* Returns the next possible time for the command to be sent. Meaning if the hours inputed by the user
|
|
* have already gone past this day then the epoch will be set for the next.
|
|
* If not the epoch will be created like usual.
|
|
*
|
|
* @param {Number} hours
|
|
* @param {Number} minutes
|
|
*/
|
|
next(hours, minutes)
|
|
{
|
|
var d= new Date();
|
|
var payload = new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), hours, minutes, 0, 0).getTime();
|
|
//console.log('Classes:Next:',hours, minutes, payload)
|
|
return payload;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Use it to transform an hour value into miliseconds.
|
|
* @param {Number} Hours - Hours to transform
|
|
*/
|
|
function hours(Hours)
|
|
{
|
|
return Hours*60*60*1000;
|
|
}
|
|
/**
|
|
* This function writes a cookie on the client side.
|
|
* Primary use: Store users Hash for every other page.
|
|
* @argument {Response} res - Response: express response object.
|
|
* @argument {String} name - Name of the cookie.
|
|
* @argument {*} value - Value of the cookie.
|
|
*/
|
|
export function baker(res, name, value)
|
|
{
|
|
return new Promise((resolve,reject)=>
|
|
{
|
|
try
|
|
{
|
|
res.cookie(name, '',{maxAge:0});
|
|
res.cookie(name, value, {maxAge:hours(2),path:'/',secure:false,httpOnly:false});
|
|
resolve();
|
|
}
|
|
catch (e)
|
|
{
|
|
reject(e)
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Hash used on register to check if there is already a user with the same credentials.
|
|
* @param {String} user - User's email
|
|
* @param {String} password - User's Password
|
|
* @param {String} hash - User's identifying hash
|
|
*/
|
|
export function bouncer (user, password, hash)
|
|
{
|
|
return new Promise((resolve, reject)=>
|
|
{
|
|
//!Crypto compare needs to be reworked so this is legacy code not supposed to work
|
|
// new _Crypto(user, password).compare().then(m=>
|
|
// {
|
|
// if(hash)
|
|
// {
|
|
// if(m==hash)
|
|
// {
|
|
// resolve(m)
|
|
// }
|
|
// else
|
|
// {
|
|
// reject('You are on a different account.')
|
|
// }
|
|
// }
|
|
// else
|
|
// {
|
|
// resolve(m)
|
|
// }
|
|
// }).catch(m=>
|
|
// {
|
|
// reject(m)
|
|
// })
|
|
})
|
|
}
|
|
|
|
|
|
export function cookieSlicer(cookie, id)
|
|
{
|
|
cookie = cookie.split(';');
|
|
var result = new Map();
|
|
for(var item of cookie)
|
|
{
|
|
let aux = item.split('=');
|
|
result.set(aux[0], aux[1]);
|
|
}
|
|
return id?result.get(id):result;
|
|
} |