0

So I have this pre-built Javascript from Vendor which only returns Promise (https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt). Now that I managed to get the response into Console.log. I wish to save this Response to mysql using PHP.

My function looks like below which basically takes rawdata and using public Key via windows crypto and generates a Encryption sent to the vendor for verification:

function encryptfunction(dataPB, pKey, cb) {
  var crypto = window.crypto || window.msCrypto;

  if(crypto.subtle)
  {
      // fetch the part of the PEM string between header and footer
      const pemHeader = "-----BEGIN PUBLIC KEY-----";
      const pemFooter = "-----END PUBLIC KEY-----";
      const pemContents = pKey.substring(pemHeader.length, pKey.length - pemFooter.length);
      // base64 decode the string to get the binary data
      const binaryDerString = window.atob(pemContents);
      // convert from a binary string to an ArrayBuffer
      const binaryDer = str2ab(binaryDerString);
          
      crypto.subtle.importKey(
          "spki", 
          binaryDer, 
          {
              name: "RSA-OAEP", 
              hash: {
                  name: "SHA-512"
              }
          },
          false, ["encrypt"]).then(
          
          key => {
              crypto.subtle.encrypt("RSA-OAEP", dataPB, pinBlock).then(
                  result => cb(btoa(ab2str(result))),
                  reason => console.log('encrypt failed', reason)
              );
          }, 
          reason => console.log('import failed', reason));
  }
  else
  {
      alert("Cryptography API not Supported");
  }     
}

and my HTML

<textarea id="output" rows="20" cols="80"></textarea>';

so when I call my Function:

encryptfunction(pbdata, pKey,r => document.getElementById("output").value = r);

The reponse is shows in the ID (Textarea) properly, but I m having difficulty in storing this value to mysql using PHP due to two reasons.

  1. The value "Response" is a promise which can not be accessed outside the function
  2. The PHP page which runs these code is called into the application via CURL. (which means i just need to return or echo the RESPONSE.

Any tips or suggestion would be highly appreciated.

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
Ramit.K
  • 1
  • 1
  • _"The value "Response" is a promise which can not be accessed outside the function"_ - so? Handle it within then. You are specifying a callback function there as third parameter - so instead of just filling a textarea, have it make an AJAX/fetch request instead. – CBroe Sep 27 '22 at 07:25
  • @CBroe Thanks, Yerp i have applied that. Works well when page is directly ran from a browser. Let Say if I CURL to the same page and let PHP run this script, all it returns is HTML and raw response. while I m aware .js is client side, is there a way i can return the value from my function to PHP, and it wraps to CURL response? – Ramit.K Sep 27 '22 at 21:40
  • You'll need a headless browser then, to have it load the page and execute your JS. – CBroe Sep 28 '22 at 05:59
  • Yes, i have been searching on that. hope to strive it! however any lead on existing topics or code on Stack yet? – Ramit.K Sep 28 '22 at 07:22

0 Answers0