When I try to require Sha256 in a clojurescript project:

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

I get no actual compilation errors, but I do get the following when I open the clojurescript->js output in a browser.

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 

I think most of it can probably be ignored, the issue is the top error that it basically can't find goog.crypt.Sha256 . The funny thing is I can make all errors go away if I change the code to the following.

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

Anyway, I just want to have access to goog.crypt.sha256 . Thank you.

有帮助吗?

解决方案

Remember that when accessing raw javascript functions you need to make use of Clojure's interoperability features, just like you would on the JVM. This means your namespace declaration changes to this:

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

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

From here on you should be able to use it normally, by calling the instance's javascript functions:

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

(.log js/console (.digest sha-256))

其他提示

Shucks, well, it turns out I am soft in the head. I was looking that the completely wrong thing and the real problem was that goog.crypt.Sha256 was a recent library, and it was not included in the version of clojurescript I was using.

From the google message group: https://groups.google.com/forum/#!topic/pedestal-users/9iy6YsIWDEo

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top