代码之家  ›  专栏  ›  技术社区  ›  tigrou

是否可以从工作人员调用子类方法?

  •  2
  • tigrou  · 技术社区  · 6 年前

    我想调用的方法 SubtleCrypto 来自一个工人。 通常,可以通过窗口上下文中可用的crypto.minute属性:

    如: window.crypto.subtle.encrypt()

    在Worker中,窗口不可用,但仍可以通过以下方式访问加密:

    self.crypto
    

    然而 self.crypto.subtle 总是返回未定义。 这是正常的行为(例如:出于安全目的而禁用)还是有可能从Worker调用SubletryPTO方法?

    我创造了一个复制行为的jsiddle here . 我用铬。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Kosh    6 年前

    按照 Deprecations and Removals in Chrome 60 :

    crypto.minary现在需要一个安全的来源

    这个 Web Crypto API 自从Chrome37在非安全源代码上工作以来,就一直得到支持。因为铬的长期政策 preferring secure origins for powerful features , crypto.subtle 现在只在安全来源上可见。

    Intent to Remove γ Chromium Bug

    在https服务器上放置以下代码时, 密码学的 而且工作得很好

    <!DOCTYPE html>
    <html>
      <body>
        <input id="start" type="button" value="Start">
    
        <script>
          function getWorkerJS() {
            var js = `
                onmessage = function(e) {
                    var jwkKey = {
                        kty: "oct",
                        k: "lckjnFLIEas7yf65ca6saksjhcajs554s5cajshgGGG"
                    };
                    crypto.subtle.importKey(
                        "jwk", jwkKey, {name: "AES-CBC"}, true,
                        ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey']
                    )
                    .then(
                        function (result) {
                            postMessage({ success: true});
                        },
                        function (error) {
                            postMessage({ message: error.message });
                        }
                    );
                };
            `;
            var blob = new Blob([js], {"type": "text/plain"});
            return URL.createObjectURL(blob);
          }
    
          var ww = new Worker(getWorkerJS());
    
          ww.onmessage = function(msg) {
            console.log(msg.data);
          };
    
          document.getElementById('start').addEventListener('click', start, false);
    
          function start() {
            ww.postMessage('start');
          }
    
        </script>
      </body>
    </html>
    
    推荐文章