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
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