1

Good morning,
I want to hash object params and string (concatenation) via sha256., but I do not know how to do it correctly.
My object:

var params = {
   "name": "kacper",
   "age": 23
};
var string = "string to hash";

I used for it library sha256 from npm, but my encode hash is incorectly.
Attempt hash:

var sha256 = require('sha256');
var hashing = sha256(params+stirng);
console.log(hashing);


Thans for yours help.

1 Answer 1

1

Let's first understand what params+string does exactly. params is converted into a string, resulting in [object Object]. Then your final string is [object Object]string to hash.

Instead, you might want to get the entire params object as a string. This can be done with JSON.stringify.

console.log(JSON.stringify(params) + string);

the result is then {"name":"kacper","age":23}string to hash.

Is this what you were looking for? It might be a better practice to make an object with params and string as fields.

var obj = {
  "params": {
    "name": "kacper",
    "age": 23
  },
  "string": "string to hash"
}

console.log(sha256(JSON.stringify(obj)));
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for reply but it does not finish this solution is good, its not working for me, I need to hash this exactly: "Hash SHA256 with concatenation of all post values and private key",it looks like it's good: sha256(JSON.stringify(params) + string), but not working,
maybe it means to concatenate all the values in the object params? i.e. sha256(params.name + params.age)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.