0

I am currently implementing encryption using npm 's "ts-md5". I have written code to encrypt strings in the below manner

import {Md5} from 'ts-md5/dist/md5';

const md5 = new Md5();
console.log(md5.appendStr('hello').end());

what md5 function/mechanism can i use to encrypt my following JSON object?

myObj = { "name":"John", "age":30, "car":null };

my angular version is 5.2

1
  • 4
    MD5 is not an encryption algorithm. Commented May 9, 2018 at 16:02

1 Answer 1

1

As pointed out by @James in the comments, md5 is a hashing algorithm, not a encryption algorithm (meaning that it's one-way, and you can't decrypt an md5 hash back to its original data). Hashing is usually used for data integrity.

Ignoring all that, to answer your question about how to md5 hash an object, you can just use JSON.stringify(obj) to turn the object into a string representation, and then just md5 hash that string:

console.log(md5.appendStr(JSON.stringify(myObj)).end());

One potential downside of this is that the order of serialization from object to JSON string is going to affect the output md5 value, for example:

JSON.stringify({a:1, b:2})
"{"a":1,"b":2}"
JSON.stringify({b:2, a:1})
"{"b":2,"a":1}"

Both strings will have different md5 hash values, even though they represent the same object contents

Sign up to request clarification or add additional context in comments.

Comments

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.