Check if downloaded file is corrupted and delete if corrupted from iPhone - iphone

Problem:
I have to download some files from the server. In between the connection with the server is lost. And when the file is opened it opened without any problem, except that it was blank.
Question
How to check if the downloaded file from the server is corrupted or not? Is there any way to do that?
If the file is corrupted it must be deleted from the documents folder.
Thank you!

You can create a hash of the file and then use that hash to compare the current hash to the new hash.
Here's an example on creating a hash for iOS:
http://iosdevelopertips.com/core-services/create-md5-hash-from-nsstring-nsdata-or-file.html
It should work pretty well because the hash only changes if the file contents change and is unaffected by creation times, modification times, and the file name.
Edit
You can also sign your files with PGP or GPG and use your public key to verify its contents.
Hope this helps :)

Send a hash of the file with the file, and then compare the hashes.

Related

In Resilio Sync, how to add multiple keys at once

With Resilio Sync, I'm looking for a way to add multiple keys at once with a CSV file or run a script or copy-paste to a config file or whatever else.
Currently, I have a list of 10 keys I must set up on a laptop.
For each I have to click the '+' then 'Enter a key then copy paste the path to the folder I created for it.
I would prefer to have a list like this I could just load or parse with a script.
KEY,Folder Path,Selective sync
I6HK76FRZS...,C:\Sync\Folder1,No
76FRI6HKZS...,C:\Sync\Folder2,No
HK76FI6RZS...,C:\Sync\Folder3,No
.....
This would help reduce errors in copying and pasting information.
I've tried exploring the Internet and it looks like no one else has ever needed to do anything like this. :/ Hard to believe.
I've also tried researching the config file to see if I could add the information there.
https://help.resilio.com/hc/en-us/articles/206178884-Running-Sync-in-configuration-mode#windows
Storing the information permanently in raw text on a config file does not sound ideal.

Photorec custom signature

Recently I have lost my database files after server (Qnap) reboot.
I need to recover the MYD files.
I'm using the Photorec to recover my files which uses file signatures.
I know that MYD files doesn't have common file signature.
I've recreated the database from the earlier backup (on local machine) just to check if the signature changes when new data inserted and it seams like the signature bit is not changing.
I'm not sure if that means I have the signature of the file that I'm looking for.
I've tried to create a photorec.sig file and entered MYD 0 0x00010065fe00000100003101aa70010f00 which are the first 34 chars of the hexdump result.
I've saved this file into the current working directory and ~/.photorec.sig and tried to recognize the MYD file with fidentify_static which always returns unknown.
I think there is something wrong with the signature file that I've created. Can anyone tell me how to make sure that the signature file is created correctly and placed to the right place on qnap?
Run fidentify with the file/directory as the parameter:
[kmaster#adsl ~]$ fidentify /home/kmaster/src/testfiles/sample.pfi
See http://www.cgsecurity.org/wiki/Add_your_own_extension_to_PhotoRec

random file access in C --- shrink file?

easy question.
I want to create a set of records (database is an overstatement) on disk. I can open with rb+, move to a random location with fseek, and then fread and fwrite, all interspersed with fflush. all good.
now I want to delete one record. easy---move the last record to the spot where I want to delete another record, and then shorten the file by one record.
but...how do I shorten my existing file?
/iaw
Copy the contents before and after the record to be deleted to
a temporary file.
Delete the original file (which had the record to be deleted).
Rename temporary file
as the original file.
To truncate the existing file that you have opened, seek to the desired file size and then set a new EOF at that position. The problem is that C has no function for that purpose. You would have to use platform-specific APIs, like SetEndOfFile() on Windows.
there is no ANSI solution to this problem, short of copying the whole file anew.
there are OS-specific solutions only. see comments under the answers.
Windows: SetEndOfFile()
Posix: ftruncate() and truncate()

Why do some sites have a md5 string on each file?

On some sites, in their download section each file has a md5. md5 of what? i cant understand the purpose
on phpBB.com for example:
Download phpBB 3.0.6 (zip)
Size: 2.30 MiB
MD5: 63e2bde5bd03d8ed504fe181a70ec97a
It is the signature of the file's hash. The idea is that you can run MD5 against the downloaded file, then compare it against that value to make sure you did not end up with a corrupted download.
This is a checksum, for verifying that the file as-downloaded is intact, without transmission errors. If the checksum listing is on a different server than the download, it also may give a little peace of mind that the download server hasn't been hacked (with the presumption that two servers are harder to hack than one).
It's a hash of the file. Used to ensure file integrity once you download said file. You'd use an md5 checksum tool to verify the file state.
Sites will post checksums so that you can make sure the file downloaded is the same as the file they're offering. This lets you ensure that file has not been corrupted or tampered with.
On most unix operating systems you can run md5 or md5sum on a file to get the hash for it. If the hash you get matches the hash from the website, you can be reasonably certain that the file is intact. A quick Google search will get you md5sum utilities for Windows.
You might also see an SHA-1 hash sometimes. It's the same concept, but a different and more secure algorithm.
This is an md5 hash of the entire binary contents of the file. The point is that if two files have different md5 hashes, they are different. This helps you determine whether a local file on your computer is the same as the file on the website, without having to download it again. For instance:
You downloaded your local copy somewhere else and think there might be a virus inside.
Your connection is lossy and you fear the file might be corrupted by the download.
You have changed the local file name and want to know which version you have.

why are downloads sometimes tagged md5, sha1 and other hash indicators?

I've seen this all over the place:
Download here! SHA1 =
8e1ed2ce9e7e473d38a9dc7824a384a9ac34d7d0
What does it mean? How does a hash come into play as far as downloads and... What use can I make of it? Is this a legacy item where you used to have to verify some checksum after you downloaded the whole file?
It's a security measure. It allows you to verify that the file you just downloaded is the one that the author posted to the site. Note that using hashes from the same website you're getting the files from is not especially secure. Often a good place to get them from is a mailing list announcement where a PGP-signed email contains the link to the file and the hash.
Since this answer has been ranked so highly compared to the others for some reason, I'm editing it to add the other major reason mentioned first by the other authors below, which is to verify the integrity of the file after transferring it over the network.
So:
Security - verify that the file that you downloaded was the one the author originally published
Integrity - verify that the file wasn't damaged during transmission over the network.
When downloading larger files, it's often useful to perform a checksum to ensure your download was successful and not mangled along transport. There's tons of freeware apps that can be used to gen the checksum for you to validate your download. This to me is an interesting mainstreaming of procedures that popular mp3 and warez sites used to use back in the day when distributing files.
SHA1 and MD5 hashes are used to verify the integrity of files you've downloaded. They aren't necessarily a legacy technology, and can be used by tools like those in the openssl to verify whether or not your a file has been corrupted/changed from its original.
It's to ensure that you downloaded the file correctly. If you hash the downloaded the file and it matches the hash on the page, all is well.
A cryptographic hash (such as SH1 or MD5) allows you to verify that file you have has been downloaded correctly and has not been tampered with.
To go along with what everyone here is saying I use HashTab when I need to generate/compare MD5 and SHA1 hashes on Windows. It adds a new tab to the file properties window and will calculate the hashes.
With a has (MD5, SHA-1) one input matches only with one output, and then if you down load the file and calculate the hash again should obtain the same output.
If the output is different the file is corrupt.
If (hash(file) == “Hash in page”)
validFile = true;
else
validFile = false;