Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Uncaught TypeError: crypto.createHash is not a function #270

Closed
vijayee opened this issue May 25, 2016 · 7 comments
Closed

Uncaught TypeError: crypto.createHash is not a function #270

vijayee opened this issue May 25, 2016 · 7 comments
Labels
kind/bug A bug in existing code (including security flaws)

Comments

@vijayee
Copy link

vijayee commented May 25, 2016

When performing and object.get command in the browser...
Uncaught TypeError: crypto.createHash is not a function ....is thrown by

function gsha2_256() {
      return crypto.createHash('sha256');
    }

in from the webpacked dist of ipfs

function(module, exports, __webpack_require__) {

    "use strict";
    /* WEBPACK VAR INJECTION */(function(Buffer) {'use strict';

    var multihash = __webpack_require__(121);
    var crypto = __webpack_require__(747);

    var mh = module.exports = Multihashing;

    mh.Buffer = Buffer; // for browser things

    function Multihashing(buf, func, length) {
      return multihash.encode(mh.digest(buf, func, length), func, length);
    }

    // expose multihash itself, to avoid silly double requires.
    mh.multihash = multihash;

    mh.digest = function (buf, func, length) {
      var digest = mh.createHash(func).update(buf).digest();

      if (length) {
        digest = digest.slice(0, length);
      }

      return digest;
    };

    mh.createHash = function (func, length) {
      func = multihash.coerceCode(func);
      if (!mh.functions[func]) {
        throw new Error('multihash function ' + func + ' not yet supported');
      }

      return mh.functions[func]();
    };
  var self= this
    mh.functions = {
      0x11: gsha1.bind(self),
      0x12: gsha2_256.bind(self),
      0x13: gsha2_512.bind(self)
      // 0x14: gsha3 // not implemented yet
      // 0x40: blake2b, // not implemented yet
      // 0x40: blake2b, // not implemented yet
      // 0x41: blake2s, // not implemented yet
    };

    function gsha1() {
      return crypto.createHash('sha1');
    }

    function gsha2_256() {
      return crypto.createHash('sha256');
    }

    function gsha2_512() {
      return crypto.createHash('sha512');
    }
    /* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(0).Buffer))

I'm not sure what module this is but it seems the crypto module never makes it into the code it instead sees window.crypto.

@dignifiedquire
Copy link
Member

Interesting, the module in question is https://github.com/jbenet/js-multihashing, and crypto should be replaced by https://github.com/diasdavid/webcrypto

@dignifiedquire dignifiedquire added the kind/bug A bug in existing code (including security flaws) label May 26, 2016
@daviddias
Copy link
Member

@vijayee we haven't been able to replicate this issue. Could you confirm if this is still a problem for you? If it happens again, can you try with the dist version, it might be a problem on your transpilation step.

@vijayee
Copy link
Author

vijayee commented Aug 15, 2016

@diasdavid I'll check this out later this evening and get back to you tomorrow on this.

@daviddias
Copy link
Member

@vijayee I'll assume that you got this one figured out :) Let me know if not and we will reopen

@imrefazekas
Copy link

I'm using webpack 2 and tried to use crypto causing the same issue @vijayee has seen.
I moved to webcrypto and I see this:

ERROR in ./~/diffie-hellman/lib/primes.json
Module parse failed: /Ruse/Skyfeld/s-admin/node_modules/diffie-hellman/lib/primes.json Unexpected token (2:11)
You may need an appropriate loader to handle this file type.
| {
|     "modp1": {
|         "gen": "02",
|         "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a3620ffffffffffffffff"
 @ ./~/diffie-hellman/browser.js 2:13-41
 @ ./~/crypto-browserify/index.js
 @ ./~/webcrypto/src/index.js
 @ ./~/clerobee/lib/clerobee.js
 @ ./src/exts/Logic.js
 @ ./src/exts nonrecursive .(js)$
 @ ./src/admin.js

ERROR in ./~/elliptic/package.json
Module parse failed: /Ruse/Skyfeld/s-admin/node_modules/elliptic/package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.
| {
|   "_args": [
|     [
|       {
 @ ./~/elliptic/lib/elliptic.js 5:19-45
 @ ./~/create-ecdh/browser.js
 @ ./~/crypto-browserify/index.js
 @ ./~/webcrypto/src/index.js
 @ ./~/clerobee/lib/clerobee.js
 @ ./src/exts/Logic.js
 @ ./src/exts nonrecursive .(js)$
 @ ./src/admin.js

ERROR in ./~/parse-asn1/aesid.json
Module parse failed: /Ruse/Skyfeld/s-admin/node_modules/parse-asn1/aesid.json Unexpected token (1:25)
You may need an appropriate loader to handle this file type.
| {"2.16.840.1.101.3.4.1.1": "aes-128-ecb",
| "2.16.840.1.101.3.4.1.2": "aes-128-cbc",
| "2.16.840.1.101.3.4.1.3": "aes-128-ofb",
 @ ./~/parse-asn1/index.js 2:12-35
 @ ./~/browserify-sign/sign.js
 @ ./~/browserify-sign/browser.js
 @ ./~/crypto-browserify/index.js
 @ ./~/webcrypto/src/index.js
 @ ./~/clerobee/lib/clerobee.js
 @ ./src/exts/Logic.js
 @ ./src/exts nonrecursive .(js)$
 @ ./src/admin.js

Any idea how to solve these messages?

@nginnever
Copy link
Member

At first glance this looks like you need a json loader? https://www.npmjs.com/package/json-loader

{ test: /\.json?$/, loader: 'json-loader' },

@imrefazekas
Copy link

yes, had it with excluding node_modules .... changed to include and worked :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/bug A bug in existing code (including security flaws)
Projects
None yet
Development

No branches or pull requests

5 participants