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.
69 lines
1.8 KiB
69 lines
1.8 KiB
const crypto = require('crypto');
|
|
const uModel = require('./models/users')
|
|
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)
|
|
{
|
|
Email = Email?Email:this.user
|
|
return this.user? new Buffer.from(this.user).toString('base64') :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()
|
|
//Get hash from db run through the peper possibilties and see if there is a match.
|
|
//return hash if true, null if false
|
|
async bouncer()
|
|
{
|
|
var Users = await uModel.find()
|
|
if(Users.length===0) return
|
|
var response;
|
|
for(var i = 65; i < 91; i++)
|
|
{
|
|
// eslint-disable-next-line no-loop-func
|
|
((i)=>{
|
|
var auxHash = this.hash(String.fromCharCode(i))
|
|
var auxUsers = Users.filter(x=>x.Hash === auxHash)
|
|
if(auxUsers.length === 1) return response = auxHash;
|
|
})(i)
|
|
}
|
|
return response;
|
|
|
|
}
|
|
async isNew(eHash)
|
|
{
|
|
var User = await uModel.find({eHash});
|
|
return !(User.length>0);
|
|
}
|
|
|
|
|
|
}module.exports._Crypto = _Crypto;
|
|
|
|
|