How can I check that my Samsung phone is original and hasn't been modified? How can I check that the mobile device that I am using has not been altered after leaving the factory?

By this I mean no changes whatsoever have been made to the device. Whether it's the whole OS (firmware)or just a single malicious app installed on system partition. 

Is there some way I can check the md5 or sha256sum of the device? And if the firmware for my phone is available on the internet I could compare. Would this work? How could I do this on ubuntu?

Or is there some other way?
The simplest answer is you can't check if they have changed your hardware or not with comparing build numbers. But the easiest way if something is changed or not will come up in an app called CPU-Z it show all the software and hardware informations that the device has.
I recommend everyone to give it a try before buying a device because it helps too much. I also realized in settings it has validation option you might give it a go. It will probably ask your details to the vendor and email it back to you