1

当我尝试在 clojurescript 项目中要求 Sha256 时:

(ns tutorial-client.sha256
  (:require
   [goog.crypt.Sha256 :as hasher]))

我没有收到任何实际的编译错误,但是当我在浏览器中打开 clojurescript->js 输出时,我确实收到了以下内容。

Uncaught Error: Undefined nameToPath for goog.crypt.Sha256 base.js:586
Uncaught ReferenceError: cljs is not defined rendering.js:7
goog.require could not find: goog.crypt.Sha256 base.js:333
Uncaught Error: goog.require could not find: goog.crypt.Sha256 base.js:337
Uncaught ReferenceError: cljs is not defined behavior.js:8
Uncaught ReferenceError: cljs is not defined app.js:100
Uncaught TypeError: Cannot read property 'IE' of undefined eventtype.js:60
Uncaught TypeError: Cannot read property 'entryPointRegistry' of undefined events.js:1084
Uncaught TypeError: Cannot read property 'IE' of undefined browserfeature.js:35
Uncaught TypeError: Cannot read property 'Error' of undefined asserts.js:71
Uncaught TypeError: Cannot read property 'prototype' of undefined base.js:1407
Uncaught Error: Invalid event type events.js:139
Uncaught Error: Invalid event type events.js:139
Uncaught TypeError: Cannot call method 'call' of undefined 

我认为其中大部分可能都可以忽略,问题是它基本上找不到 goog.crypt.Sha256 的首要错误。有趣的是,如果我将代码更改为以下内容,我可以消除所有错误。

(ns tutorial-client.sha256
  (:require
   [goog.net.XhrIo :as hasher]))

无论如何,我只想访问 goog.crypt.sha256 。谢谢你。

4

2 回答 2

2

请记住,在访问原始 javascript 函数时,您需要使用 Clojure 的互操作性功能,就像在 JVM 上一样。这意味着您的命名空间声明更改为:

(ns tutorial-client.sha256
  (:import goog.crypt.Sha256]))

(.log js/console (Sha256.))
;; this will log the instance to the browser console

从这里开始,您应该能够通过调用实例的 javascript 函数来正常使用它:

(def sha-256 (Sha256.))
(.update sha-256 [])

(.log js/console (.digest sha-256))
于 2013-09-17T11:00:38.247 回答
1

嘘,好吧,事实证明我的头脑很柔软。我正在寻找完全错误的事情,真正的问题是 goog.crypt.Sha256 是一个最近的库,它不包含在我使用的 clojurescript 版本中。

来自谷歌消息组: https ://groups.google.com/forum/#!topic/pedestal-users/9iy6YsIWDEo

于 2013-09-19T05:10:54.133 回答