Blockchain Mad Scientists

A forum for blockchain developers

Whether you've taken a class on Udacity or somewhere else, you are welcome here.
 Create an account now to learn, teach, and mentor!
User avatar
By tmc
#2
Hey guys, I am having some trouble with the first lesson, can anyone point me in the right direction here please :D

I am getting the following error
/Users/jlroberts/Projects/Blockchain/Lesson1/main.js:25
return JSON.stringify(SHA265(obj));
^

ReferenceError: SHA265 is not defined
at generateHash (/Users/jlroberts/Projects/Blockchain/Lesson1/main.js:25:14)
at Object.<anonymous> (/Users/jlroberts/Projects/Blockchain/Lesson1/main.js:28:29)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3


With the following code

Code: Select all
/**
 * Udacity Lesson 1
 */

var SHA256 = require("crypto-js/sha256");

/**
 * Variables: Do not change variable values to be able to answer the quiz
 */

const data1 = "Blockchain Rock!";
const dataObject = {
	id: 1,
  	body: "With Object Works too",
  	time: new Date().getTime().toString().slice(0,-3)
};

/**
 * Step 3: Add code to the `generate hash function
 * Function that generate the SHA256 Hash
 * @param {*} obj 
 */

function generateHash(obj) {
	return JSON.stringify(SHA265(obj));
}

console.log(`SHA265 Hash: ${generateHash(data1)}`);
console.log("************************************");
console.log(`SHA265 Hash: ${generateHash(dataObject)}`);

/**
 * Run your application using `node app.js`
 */


I suspect I have to import the module into the function directly, but that is not what the instructions said to do...

Any ideas on what I can do to use the imported function SHA256 inside the generateHash function?
User avatar
By tmc
#3
lol @ typo

Ok, so now I am getting
└──╼ node main.js
SHA265 Hash: {"words":[1995316644,315407783,-1837331899,603986356,1980938583,1863042211,437660173,357238088],"sigBytes":32}
************************************
SHA265 Hash: {"words":[1319486728,-1504285066,604324856,-21991468,1467448291,-1790327807,1681305562,1708274446],"sigBytes":32}


Any ideas on why its not returning a hash? :)
User avatar
By tmc
#4
Thank you Abhishek =]

I needed to change
Code: Select all
return JSON.stringify(SHA256(obj));
to
Code: Select all
return SHA256(JSON.stringify(obj));


My final code is
Code: Select all
/**
 * Udacity Lesson 1
 */

var SHA256 = require("crypto-js/sha256");

/**
 * Variables: Do not change variable values to be able to answer the quiz
 */

const data1 = "Blockchain Rock!";
const dataObject = {
	id: 1,
  	body: "With Object Works too",
  	time: new Date().getTime().toString().slice(0,-3)
};

/**
 * Step 3: Add code to the `generate hash function
 * Function that generate the SHA256 Hash
 * @param {*} obj 
 */

function generateHash(obj) {
	return SHA256(JSON.stringify(obj));
}

console.log(`SHA265 Hash: ${generateHash(data1)}`);
console.log("************************************");
console.log(`SHA265 Hash: ${generateHash(dataObject)}`);

/**
 * Run your application using `node app.js`
 */

Are you looking to create your own cryptocurrency? Join now to access private content!