0

I have an encrypt function but when I return the result it returns an object called Promise with PromiseState and PromiseResult. I want to just save or return PromiseResult's value and save it to a variable How can I do that?(sorry for my amateur question. I am new to programming)

async function encrypt(password, masterpass) {

    console.log("Web Crypto API ran");

    const encoder = new TextEncoder();

    const toBase64 = buffer =>
        btoa(String.fromCharCode(...new Uint8Array(buffer)));

    const PBKDF2 = async (
        password, salt, iterations,
        length, hash, algorithm = 'AES-CTR') => {

        // firstly we need to importKey
        keyMaterial = await window.crypto.subtle.importKey(
            'raw',
            encoder.encode(password),
            {name: 'PBKDF2'},
            false,
            ['deriveKey']
        );

        return await window.crypto.subtle.deriveKey(
            {
                name: 'PBKDF2',
                salt: encoder.encode(salt),
                iterations,
                hash
            },
            keyMaterial,
            {name: algorithm, length},
            false, 
            ['encrypt', 'decrypt'] 
        );
    }
    const salt = window.crypto.getRandomValues(new Uint8Array(16));
    const counter = window.crypto.getRandomValues(new Uint8Array(16));
    const plain_text = encoder.encode(password);
    const derivedKey = await PBKDF2(masterpass, salt, 100000, 256, 'SHA-256');

    const encrypted = await window.crypto.subtle.encrypt(
        {name: "AES-CTR", counter, length: 64},
        derivedKey,
        plain_text
    );

    const concatenated = toBase64([...salt, ...counter, ...new Uint8Array(encrypted)]);

    
    console.log("Encrypt function reached the end", concatenated);

    return concatenated;


}
John
  • 33
  • 10
  • You cannot expect a value that will be available in some *future* to be available *now*. So you'll always end up with a promise of that future value. You need to stick to the asynchronous coding pattern. So the caller of `encrypt` should also use `await` or a `then` callback. – trincot May 02 '21 at 15:25
  • @trincot I call it like this: websiteCredentials.password = encrypt(websiteCredentials.password,websiteCredentials.masterpass); and it saves promise into that while I want to save concatenated. Can you please be more specific – John May 02 '21 at 15:31
  • So, like I said: you cannot expect `.password` to be the result, since that is only available *later*. You must either replace `=` with `= await`, but then you must wrap your main code in an `async` function, or else you must chain a `.then((password => { websiteCredentials.password = password; })`... etc. – trincot May 02 '21 at 15:37

1 Answers1

0

The word "async" before a function name means that it always returns promise. If you want get the value, you can use one of the below code.

encrypt(password, masterpass).then(data => {
 console.log(data);// data is nothing but the value.
});


//OR
let data = await encrypt(password, masterpass);
console.log(data);
Ayaz
  • 2,111
  • 4
  • 13
  • 16