Is there a way to determine whether the uploaded file has been altered?

Is there a way for me to verify if a file uploaded by the user to my server has been altered since their last upload?

My database includes a log table containing User_id and FileName (each User_id is unique). Once I have read the contents of the file, I delete it from the server.

Answer №1

If you want to ensure the integrity of a file before deleting it, you can store a hash of the file and compare it with the previous hash when uploading. The System.Cryptography namespace provides HashAlgorithm classes like SHA1 that can help you achieve this.

"A cryptographic hash function is a deterministic procedure that takes an arbitrary block of data and returns a fixed-size bit string, the (cryptographic) hash value, such that an accidental or intentional change to the data will change the hash value"

Here's some sample code to get you going. Assuming you have a stream variable named stream containing your file data (you could use FileStream to open it):

var sha = new System.Security.Cryptography.SHA1Managed();
byte [] hash = sha.ComputeHash(stream);

Now, the variable hash will contain the fingerprint of the file contents. Even a slight alteration in the file will result in a different hash value, but hashing the same file will always yield the same hash.

Answer №2

Hash functions are commonly used to detect changes in large chunks of data, such as files. One common method is using a cyclic redundancy check (CRC).

In the Linux operating system, there is a built-in utility called cksum that can be used for this purpose.

To generate a checksum for a file, you can simply run the command cksum filename and save the output. This checksum can then be stored in a database and used to verify the integrity of new incoming files.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Are the new node buffers already populated with information?

Is this a node bug or is it the expected behavior? This issue can be reproduced in versions 0.12.7 and io 3.1.0: > new Buffer(5) <Buffer 00 00 02 00 00> > new Buffer(5) <Buffer 00 00 00 00 00> > new Buffer(5) <Buffer 28 94 00 02 ...

Extracting the value of *data* from my HTML and displaying it in the console with Javascript

I'm struggling to figure out what's going wrong with this code. I've done some research online and it seems like I should just include the window.onload = function() at the beginning of my code. However, no matter what I try, the value alway ...

Sharing information between pages in React through Router

I am struggling to transfer a single string from one page to another using react router and only functional components. I have created a button that links my pages, but I can't seem to pass the string successfully. Here is an example of the code on th ...

What is causing an empty box to appear due to padding? Is there a way to conceal it

There seems to be an issue with adding padding to the results id div box. The padding is causing a blank yellow box to appear at the bottom before any results are submitted. I've tried to hide this box without success by adding a displayResult() funct ...

JavaScript's Array.map function failing to return a value

Here is a snippet of code from an api endpoint in nextJS that retrieves the corresponding authors for a given array of posts. Each post in the array contains an "authorId" key. The initial approach did not yield the expected results: const users = posts.ma ...

Retrieving information from MongoDB through Express, constructing an object, and transmitting it to React

Currently, I find myself trapped in asynchronous chaos. Within my React application, there exists a page /menu that is responsible for fetching data from my MongoDB instance through an Express.js API. Inside my database named "menu," there are collections ...

What strategies can be used to efficiently perform Asynchronous Operations on a high volume of rows in a particular database table?

I am looking to perform Asynchronous Operations on every row of a specific database table, which could potentially contain 500,000, 600,000, or even more rows. My initial approach was: router.get('/users', async (req, res) => { const users = ...

Why does my dialog box only open the first time and not the second time?

I have created my own custom dialog box using jQuery and it seems to be working fine initially. However, after closing it once, when I try to open it again nothing appears. Can anyone help me identify what's causing this issue? Below is the source co ...

Error: Angular does not recognize x2js XML format

I'm attempting to adapt this particular example to utilize xml as json data. However, I am encountering some issues with the code. courses = x2js.xml_str2json(data); console.log(courses.books.course); $scope.todos = courses.books.course; In the XML ...

Using an external JavaScript script may encounter difficulties when loading pages with jQuery

Trying to utilize jQuery for loading html posts into the main html page and enabling external scripts to function on them. Utilizing a script (load.js) to load posts into the index.html page: $(document).ready(function () { $('#header').loa ...

Here's a simple script to display a div or popup only once in the browser using plain vanilla JavaScript

// JavaScript Document I'm attempting to make a popup appear in the browser only once. I believe using local storage is the key, but all I can find are solutions involving jQuery. I really want to achieve this using plain JavaScript without relying ...

How can you fix the "bad value" response in mongodb when utilizing query parameters in the url?

{ "ok": 0, "code": 2, "codeName": "BadValue", "name": "MongoError" } Whenever I attempt to use query parameters skip and limit in the url, this error message pops up. localhost:5 ...

Whenever the useState hook is utilized, it will trigger a re-execution

In my code for form validation in the next.js V1, I am experiencing a problem. The code snippet is as follows: setErrorF((errorF) => { if( errorF.FNameAndLName !== "" || errorF.phoneNumber !== "" || errorF.location1 !== ...

React application not functioning on localhost:3000 despite successful compilation with no errors, only displaying the title on localhost but failing to show any content

I recently started developing a new website with React. Everything was running smoothly on localhost until I made some changes, and now the homepage content is not displaying when I visit localhost:3000. I suspect there may be an issue with my routing or s ...

What is preventing my hidden field from being filled by a JavaScript function?

I've recently developed a JavaScript function that generates a specific string required for another form on the website I'm currently working on. Initially, I decided to store this generated value in a hidden field and then submit it within an HT ...

The Date object in Typescript is represented as a string

My typescript interface includes a variable called start, which is typed as Date | null. This data is retrieved from a dotnet API that returns a DateTime object. The issue arises when the start variable is passed through a function in Date-fns, causing a R ...

React slick does not display arrows when there are 4 or more photos

I am facing an issue where the next and previous arrows are not appearing when I have 4 or more photos on react-slick. However, they show up fine when there are 3 or fewer photos. You can view my code at this link: https://codesandbox.io/s/wyyrl6zz3l ...

How can you retrieve an array of multiple property values from a collection of dynamic objects?

I am currently working with a dynamic JavaScript object array that has varying structures. For example: var someJsonArray = [ {point: 100, level: 3}, {point: 100, level: 3}, {point: 300, level: 6} ]; At times, it may have a different combination lik ...

Getting the mantissa and exponent values from a double variable in C#

Is there a simple way to extract the mantissa and exponent from a double in C# (or .NET)? I came across this particular example through a Google search, but I am uncertain about its robustness. Is it possible for the binary representation of a double to c ...

Sorting data in Javascript can be done efficiently by utilizing the .filter method

Can someone help me identify what I might be doing incorrectly? I have a chained filter under computed that is giving me an error message stating 'product.topic.sort' is not a function. My intention is to use 'select' to provide sortin ...