Skip to content
This repository was archived by the owner on Sep 24, 2024. It is now read-only.

Commit efdc4d4

Browse files
committed
Version 2.0.0
1 parent 6e0c8ba commit efdc4d4

File tree

6 files changed

+60
-7555
lines changed

6 files changed

+60
-7555
lines changed

api/routes/crypto.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ router
66
const { query: { data } } = req;
77
if (!data) return res.sendError(4)
88
try {
9-
return res.status(200).json({ data: CCrypto.e(data) })
9+
return res.status(200).json({ data: CCrypto.encrypt(data) })
1010
} catch (err) {
1111
return res.sendError(10)
1212
}
@@ -15,7 +15,7 @@ router
1515
const { query: { data } } = req;
1616
if (!data) return res.sendError(4)
1717
try {
18-
return res.status(200).json({ data: CCrypto.d(data) })
18+
return res.status(200).json({ data: CCrypto.decrypt(data) })
1919
} catch (err) {
2020
return res.sendError(11)
2121
}

doc/api-documentation.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
openapi: "3.0.0"
22
info:
3-
version: "1.3.0"
3+
version: "2.0.0"
44
title: "FemDevs API Documentation"
55
description: "The documentation for the FemDevs API."
66
contact:

middleware/errpages.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

middleware/routeLogger.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module.exports = function (mreq, mres, next) {
7474
const data = {
7575
ip: ['::1', '127.0.0.1'].includes(mreq.ip.replace('::ffff:', '')) ? 'localhost' : (mreq.ip || 'unknown').replace('::ffff:', ''),
7676
method: req.method,
77-
url: new URL(mreq.originalUrl, 'https://thefemdevs.com/').pathname,
77+
url: new URL(mreq.originalUrl, `${mreq.protocol}://${mreq.hostname}`).pathname,
7878
status: res.statusCode,
7979
time: time.toFixed(2),
8080
bytes: String(res.getHeader('Content-Length') || 0),

modules/CCrypt.js

+55-38
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,59 @@
1-
module.exports = class {
2-
static _c = require('node:crypto')
3-
static _dCrypt = { h: { alg: 'RSA-RIPEMD160' }, c: { alg: 'chacha20-poly1305', s: { k: 32, iv: 12 } } }
4-
static _dEnc = 'base64url'
5-
static _dbobj = () => ({ _a: '', _b: '', _c: '', _d: '', _e: '' })
6-
static _h = () => this._c.createHash(this._dCrypt.h.alg)
7-
static _enc = (x, y) => this._c.createCipheriv(this._dCrypt.c.alg, x, y)
8-
static _dec = (x, y) => this._c.createDecipheriv(this._dCrypt.c.alg, x, y)
9-
static _sttd(a, b, _e = new Array(0)) {
10-
Object.keys(this._dbobj()).forEach((k) => { if (!new Object(JSON.parse(Buffer.from(a, b).toString('utf-8'))).hasOwnProperty(k)) _e.push(`Missing ${k} value`) })
11-
return (_e.length > 0) ? _e.forEach(console.error) : JSON.parse(Buffer.from(a, b).toString('utf-8'));
12-
}
13-
static e(d) {
1+
const crypto = require('node:crypto');
2+
const assert = require('node:assert/strict');
3+
4+
const cryptoDefaults = {
5+
hashAlgorithm: 'RSA-RIPEMD160',
6+
crypt: crypto.getCipherInfo("chacha20-poly1305"),
7+
encoding: 'base64url',
8+
};
9+
10+
class CCrypto {
11+
static encrypt(inputData) {
1412
const
15-
_d = this._dbobj,
16-
_in = Buffer.from(d),
17-
_giv = this._c.randomBytes(this._dCrypt.c.s.iv),
18-
_k = this._c.randomBytes(this._dCrypt.c.s.k)
19-
_d._c = this._h().update(_in).digest(this._dEnc)
20-
_d._a = _giv.toString(this._dEnc)
21-
_d._b = _k.toString(this._dEnc)
22-
const _encdata = this._enc(_k, _giv).update(_in)
23-
_d._d = this._h().update(_encdata).digest(this._dEnc)
24-
_d._e = _encdata.toString(this._dEnc)
25-
return Buffer.from(JSON.stringify(_d), 'utf8').toString(this._dEnc)
13+
data = { iv: '', key: '', prehash: '', posthash: '', data: '' },
14+
input = Buffer.from(inputData),
15+
generatedIV = crypto.randomBytes(cryptoDefaults.crypt.ivLength),
16+
key = crypto.randomBytes(cryptoDefaults.crypt.keyLength)
17+
data.prehash = crypto
18+
.createHash(cryptoDefaults.hashAlgorithm)
19+
.update(input)
20+
.digest(cryptoDefaults.encoding)
21+
data.iv = generatedIV.toString(cryptoDefaults.encoding)
22+
data.key = key.toString(cryptoDefaults.encoding)
23+
const encdata = crypto
24+
.createCipheriv(cryptoDefaults.crypt.name, key, generatedIV)
25+
.update(input)
26+
data.posthash = crypto
27+
.createHash(cryptoDefaults.hashAlgorithm)
28+
.update(encdata)
29+
.digest(cryptoDefaults.encoding)
30+
data.data = encdata.toString(cryptoDefaults.encoding)
31+
return Buffer.from(JSON.stringify(data), 'utf8').toString(cryptoDefaults.encoding)
2632
}
27-
static d(d) {
28-
const _d = this._sttd(d, this._dEnc)
29-
if (!_d) return;
30-
const _fd = Buffer.from(_d._e, this._dEnc)
31-
const _psvh = this._h().update(_fd).digest(this._dEnc);
32-
if (!(_d._d == _psvh)) throw new TypeError("The Post-Verification Checksum failed to resolve to the provided data")
33+
static decrypt(inputData) {
34+
const data = JSON.parse(Buffer.from(inputData, cryptoDefaults.encoding).toString('utf-8'))
35+
for (const key of Object.keys({ iv: '', key: '', prehash: '', posthash: '', data: '' })) {
36+
if (!Object.hasOwn(data, key)) assert.fail(`Missing ${key} value`)
37+
}
38+
const formattedData = Buffer.from(data.data, cryptoDefaults.encoding)
39+
const postVerificationHash = crypto
40+
.createHash(cryptoDefaults.hashAlgorithm)
41+
.update(formattedData)
42+
.digest(cryptoDefaults.encoding);
43+
assert.equal(data.posthash, postVerificationHash);
3344
const
34-
_giv = Buffer.from(_d._a, this._dEnc),
35-
_k = Buffer.from(_d._b, this._dEnc),
36-
_outData = this._dec(_k, _giv).update(_fd),
37-
_pevh = this._h().update(_outData).digest(this._dEnc);
38-
if (!(_d._c == _pevh)) throw new TypeError("The Pre-Verification Checksum failed to resolve to the provided data")
39-
const _out = _outData.toString('utf-8')
40-
return _out
45+
generatedIV = Buffer.from(data.iv, cryptoDefaults.encoding),
46+
key = Buffer.from(data.key, cryptoDefaults.encoding),
47+
outData = crypto
48+
.createDecipheriv(cryptoDefaults.crypt.name, key, generatedIV)
49+
.update(formattedData),
50+
preVerificationHash = crypto
51+
.createHash(cryptoDefaults.hashAlgorithm)
52+
.update(outData)
53+
.digest(cryptoDefaults.encoding);
54+
assert.equal(data.prehash, preVerificationHash);
55+
return outData.toString('utf-8')
4156
}
42-
}
57+
}
58+
59+
module.exports = CCrypto

0 commit comments

Comments
 (0)