diff --git a/db-models/captcha.js b/db-models/captcha.js deleted file mode 100644 index bc65ae88..00000000 --- a/db-models/captcha.js +++ /dev/null @@ -1,27 +0,0 @@ - -'use strict'; - -const Mongo = require(__dirname+'/../helpers/db.js') - , db = Mongo.client.db('captcha'); - -module.exports = { - - db, - - findOne: async (name) => { - return db.collection('captcha').findOne({ '_id': name }); - }, - - insertOne: async (data) => { - return db.collection('captcha').insertOne(data); - }, - - deleteOne: async (data) => { - return db.collection('captcha').deleteOne(data); - }, - - deleteAll: async () => { - return db.collection('captcha').deleteMany({}); - }, - -} diff --git a/db-models/trips.js b/db-models/trips.js new file mode 100644 index 00000000..81b981a8 --- /dev/null +++ b/db-models/trips.js @@ -0,0 +1,21 @@ + +'use strict'; + +const Mongo = require(__dirname+'/../helpers/db.js') + , db = Mongo.client.db('tripcodes').collection('tripcodes'); + +module.exports = { + + findOne: (password) => { + return db.findOne({ '_id': password }); + }, + + insertOne: (password, trip) => { + return db.insertOne({ '_id': password, 'code': trip }); + }, + + deleteAll: () => { + return db.deleteMany({}); + }, + +} diff --git a/helpers/id-contrast.js b/helpers/id-contrast.js new file mode 100644 index 00000000..50232bbb --- /dev/null +++ b/helpers/id-contrast.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports = (hex) => { + const r = parseInt(hex.substr(0,2), 16); + const g = parseInt(hex.substr(2,2), 16); + const b = parseInt(hex.substr(4,2), 16) + return 0.375 * r + 0.5 * g + 0.125 * b; +} diff --git a/helpers/tripcode.js b/helpers/tripcode.js new file mode 100644 index 00000000..f7736f5c --- /dev/null +++ b/helpers/tripcode.js @@ -0,0 +1,19 @@ +'use strict'; + +const Tripcodes = require(__dirname+'/../db-models/trips.js') + , crypto = require('crypto'); + +module.exports = async (password) => { + + //return existing trip if exists + let existing = await Tripcodes.findOne(password); + if (existing) { + return existing.code; + } + + //same as lynxchan does it, but i dont think this is secure. welcome to change. + const trip = crypto.createHash('sha256').update(password + Math.random()).digest('base64').substring(0, 6); + await Tripcodes.insertOne(password, trip); + return trip; + +}