7

Here is my code, I have included following .js files, onpage load it is giving error "ReferenceError: CryptoJS is not defined" why does it give that error when already js references are added. I am making a sharepoint-2013 app using office 365.

<script type="text/javascript" src="../Scripts/sha1.js"></script>
<script type="text/javascript" src="../Scripts/hmac-sha1.js"></script>

  'use strict';

var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();

(function () {

    // This code runs when the DOM is ready and creates a context object which is 
    // needed to use the SharePoint object model
    $(document).ready(function () 
    {
        getUserName();

        $("#button1").click(function()
        {
                paraupdate();   
        });

    });

    // This function prepares, loads, and then executes a SharePoint query to get 
    // the current users information

    function paraupdate()
    {

        var str=""+$("#textbox1").val();
        alert(""+str);
        var message = str+"json539ff0f815ca697c681fe01d32ba52e3";
        var secret = "<my private key>";        
        var crypto = CryptoJS.HmacSHA1(message, secret).toString();
        alert("crypto answer is " + crypto);
        var siteurl="http://pnrbuddy.com/api/station_by_code/code/"+str+"/format/json/pbapikey/539ff0f815ca697c681fe01d32ba52e3/pbapisign/"+crypto;


         $.ajax({
            url: siteurl,
            type: "GET",
            dataType: 'json',

            success: function (data) {
                     alert("IN Success");
                alert(""+data.station_by_code);

            },
            error: function (error) {
                alert("IN Error");
                alert(JSON.stringify(error));
            }
        });       


    }


    function getUserName() 
    {
        context.load(user);
        context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
    }

    // This function is executed if the above call is successful
    // It replaces the contents of the 'message' element with the user name
    function onGetUserNameSuccess() 
    {
        $("#label1").html("Enter Station Code : ");
        $("#button1").val("CLICK");

    }

    // This function is executed if the above call fails
    function onGetUserNameFail(sender, args) {
        alert('Failed to get user name. Error:' + args.get_message());
    }

})();
Niranjan Kulkarni
  • 171
  • 1
  • 1
  • 10
  • From your code it is not possible to tell why `CryptoJS` is not defined. Show a complete example. – phylax Apr 09 '14 at 11:24

4 Answers4

11

include core-min.js before sha256.js

Mahendran
  • 129
  • 1
  • 6
0

There are one of two forms for fixing this:

1: Manual Load, i have more success with this pattern:

$.getScript(scriptbase + "SP.Runtime.js",
            function () {
                $.getScript(scriptbase + "SP.js", execOperation);
            }
        );

Example:

$.getScript("~hostUrl/_layouts/15/SP.RequestExecutor.js", getListDataREST);

2: Script on Demand:

SP.SOD.executeFunc('sp.userprofiles.js', 'SP.ClientContext', loadUserData);

This SharepointExchange posting, gives the usual JSOM implementation for most AppParts: Jquery is not firing on Page load SharePoint 2013

Community
  • 1
  • 1
GoldBishop
  • 2,820
  • 4
  • 47
  • 82
0

Error solved I added online references instead,

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha1.js"></script>

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha1.js"></script>
Niranjan Kulkarni
  • 171
  • 1
  • 1
  • 10
0

Maybe is too late, but:

var CryptoJS = require('crypto-js');
var hash = CryptoJS.HmacSHA256("Message", "secret");
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
console.log(hashInBase64); // qnR8UCqJggD55PohusaBNviGoOJ67HC6Btry4qXLVZc=

Works fine in node.js.

Mustafa
  • 1
  • 1