1
0
Fork 0

chore(backend): Cleaned up comments

This commit is contained in:
joostdecock 2022-10-29 22:37:37 +02:00
parent 2c9d69c979
commit f6809e92e4

View file

@ -33,13 +33,13 @@ export const encryption = (stringKey, salt = 'FreeSewing') => {
/* /*
* This will encrypt almost anything, but undefined we cannot encrypt. * This will encrypt almost anything, but undefined we cannot encrypt.
* We could side-step this by assigning a default to data, but that would * We could side-step this by assigning a default to data, but that would
* lead to confusing bugs when people thing they pass in data and instead * lead to confusing bugs when people think they pass in data and instead
* get an encrypted default. So instead, let's bail out loudly * get an encrypted default. So instead, let's bail out loudly
*/ */
if (typeof data === 'undefined') throw 'Undefined cannot be uncrypted' if (typeof data === 'undefined') throw 'Undefined cannot be uncrypted'
/* /*
* With undefined out of the way, there's still thing we cannot encrypt. * With undefined out of the way, there's still some things we cannot encrypt.
* Essentially, anything that can't be serialized to JSON, such as functions. * Essentially, anything that can't be serialized to JSON, such as functions.
* So let's catch the JSON.stringify() call and once again bail out if things * So let's catch the JSON.stringify() call and once again bail out if things
* go off the rails here. * go off the rails here.
@ -51,17 +51,19 @@ export const encryption = (stringKey, salt = 'FreeSewing') => {
} }
/* /*
* Even with the same salt, this initialization vector avoid that two * Even with the same salt, this initialization vector avoids that
* identical input strings would generate the same cyphertext * two identical input strings would generate the same ciphertext
*/ */
const iv = randomBytes(16) const iv = randomBytes(16)
/* /*
* The thing that does the thing * The thing that does the encrypting
*/ */
const cipher = createCipheriv(algorithm, key, iv) const cipher = createCipheriv(algorithm, key, iv)
// Always return a string so we can store this in SQLite no problemo /*
* Always return a string so we can store this in SQLite no problemo
*/
return JSON.stringify({ return JSON.stringify({
iv: iv.toString('hex'), iv: iv.toString('hex'),
encrypted: Buffer.concat([cipher.update(data), cipher.final()]).toString('hex'), encrypted: Buffer.concat([cipher.update(data), cipher.final()]).toString('hex'),
@ -69,7 +71,7 @@ export const encryption = (stringKey, salt = 'FreeSewing') => {
}, },
decrypt: (data) => { decrypt: (data) => {
/* /*
* Don't blindly assume this data is properly formatted cyphertext * Don't blindly assume this data is properly formatted ciphertext
*/ */
try { try {
data = JSON.parse(data) data = JSON.parse(data)
@ -81,11 +83,14 @@ export const encryption = (stringKey, salt = 'FreeSewing') => {
} }
/* /*
* The thing that does the thing * The thing that does the decrypting
*/ */
const decipher = createDecipheriv(algorithm, key, Buffer.from(data.iv, 'hex')) const decipher = createDecipheriv(algorithm, key, Buffer.from(data.iv, 'hex'))
// Parse this string as JSON so we return what was passed to encrypt() /*
* Parse this string as JSON
* so we return the same type as what was passed to encrypt()
*/
return JSON.parse( return JSON.parse(
Buffer.concat([ Buffer.concat([
decipher.update(Buffer.from(data.encrypted, 'hex')), decipher.update(Buffer.from(data.encrypted, 'hex')),