'How to get a variable in a function

EDIT so i removed the first mention of encrypted, i get a new error. this time it generates a hash however the problem is with the function dec()

node:internal/crypto/cipher:176
    throw new ERR_INVALID_ARG_TYPE(
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined
    at new NodeError (node:internal/errors:371:5)
    at Decipheriv.update (node:internal/crypto/cipher:176:11)
    at dec (C:\Coding\coding\JS\PasswordManager\index.js:31:30)
    at Object.<anonymous> (C:\Coding\coding\JS\PasswordManager\index.js:37:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'ERR_INVALID_ARG_TYPE'
}

Also when i move the contents of dec() to pass() everything works fine. however, this wont really work for me.

I'm making a pasword manager for myself. As a coding challenge to get better and also for better security.

However, I'm trying to call the variable encrypted, which is defined in a function. However I get this error whenever I run my code.

> node .
node:internal/crypto/cipher:176
    throw new ERR_INVALID_ARG_TYPE(
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined
    at new NodeError (node:internal/errors:371:5)
    at Cipheriv.update (node:internal/crypto/cipher:176:11)
    at pass (C:\Coding\coding\JS\PasswordManager\index.js:15:28)
    at Object.<anonymous> (C:\Coding\coding\JS\PasswordManager\index.js:37:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'ERR_INVALID_ARG_TYPE'
}

CODE

function pass(length, username, service) {
    var length = length,
        charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.`~1234567890!@#$%^&*()-_=+{[}]|\;:'/?><",
        retVal = "",
        encrypted = cipher.update(compiled, 'utf8', 'hex') + cipher.final('hex');
    
    for (var i = 0, n = charset.length; i < length; ++i) {
        retVal += charset.charAt(Math.floor(Math.random() * n));
    }
    var compiled = `${service}: ${username} | ${retVal}\n`

    var encrypted = cipher.update(compiled, 'utf8', 'hex') + cipher.final('hex'); // encrypted text
    
    console.log(encrypted);
    fs.appendFile('out.txt', encrypted+`\n`, (err) => {
        if (err) throw err;
        console.log('The login details were appended to file!');
      }) ;
    return encrypted;
    }
function dec() {
    var decrypted = decipher.update(encrypted, 'hex', 'utf8') + decipher.final('utf8'); //deciphered text
    console.log(decrypted);
}

I have been trying to fix this for a while now, what do I do?



Solution 1:[1]

Your issue is that compiled is being accessed before it is initialized.

function pass(length, username, service) {
    var length = length,
        charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.`~1234567890!@#$%^&*()-_=+{[}]|\;:'/?><",
        retVal = "",
        encrypted = cipher.update(compiled, 'utf8', 'hex') + cipher.final('hex'); // Error here
    // Rest of code
}

In order to fix it, you need to initialize compiled before the call to cipher.update().

And then, because you redeclare and initialize encrypted without using this value, you can remove the call completely:

function pass(length, username, service){
    var length = length,
        charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.`~1234567890!@#$%^&*()-_=+{[}]|\;:'/?><",
        retVal = "";
    
    for (var i = 0, n = charset.length; i < length; ++i) {
        retVal += charset.charAt(Math.floor(Math.random() * n));
    }
    var compiled = `${service}: ${username} | ${retVal}\n`

    var encrypted = cipher.update(compiled, 'utf8', 'hex') + cipher.final('hex'); // encrypted text
    
    console.log(encrypted);
    fs.appendFile('out.txt', encrypted+`\n`, (err) => {
        if (err) throw err;
        console.log('The login details were appended to file!');
      }) ;
    return encrypted;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1