Discord
Login
Community
DARK THEME

JS Async not working with Microstudio? - SHA256

I wanted to write a little cryptographie lib, so I was implementing some hash functions.

E.g: SHA256

//javascript

async function sha256(message) {
  // encode as UTF-8
  const msgBuffer = new TextEncoder().encode(message);

  // hash the message
  const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);

  // convert ArrayBuffer to Array
  const hashArray = Array.from(new Uint8Array(hashBuffer));

  // convert bytes to hex string
  const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
  
  return hashHex;
}
global.sha256=sha256

When I do smth like:

const plaintext = 'Hello, World!';
generateHash(plaintext).then(hash => console.log('SHA-256 Hash:', hash));

everthing works fine. But if I call this method sha256() from init() in microstudio - its getting an empty object as respose. Can anyone help me there?

Weird . It works for me .

You are using a different function name than the one you declared. In MicroScript version 2 the usage looks like this >>>

init = function()
  test = false
  plaintext = 'Hello, World!'
  sha256(plaintext).then(function(hash)
    print('SHA-256 Hash:'+ hash)
  end)
end

update = function()
  if not test then 
    sha256(plaintext).then(function(hash)
       print('SHA-256 Hash:'+ hash)
    end)
    test = true
  end
end

draw = function()
end

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community