paulmillr / noble-ed25519

Fastest 4KB JS implementation of ed25519 signatures
https://paulmillr.com/noble
MIT License
414 stars 50 forks source link

what is the advantage of distinguishing crypto.web and crypto.node in utils.sha512 #51

Closed wk3368 closed 2 years ago

wk3368 commented 2 years ago
sha512: async (message: Uint8Array): Promise<Uint8Array> => {
  if (crypto.web) {
    const buffer = await crypto.web.subtle.digest('SHA-512', message.buffer);
    return new Uint8Array(buffer);
  } else if (crypto.node) {
    return Uint8Array.from(crypto.node.createHash('sha512').update(message).digest());
  } else {
    throw new Error("The environment doesn't have sha512 function");
  }
},

because

why not delete the crypto.web one, and make it a sync function?

sha512: (message: Uint8Array): Uint8Array => {
  return Uint8Array.from(crypto.node.createHash('sha512').update(message).digest());
},
paulmillr commented 2 years ago

There is no require("crypto") in browsers. It is node.js-only. If your build tool adds require("crypto"), it is non-standard.