How do I do a SHA1 File Checksum in C#?

using (FileStream fs = new FileStream(@”C:\file\location”, FileMode.Open)) using (BufferedStream bs = new BufferedStream(fs)) { using (SHA1Managed sha1 = new SHA1Managed()) { byte[] hash = sha1.ComputeHash(bs); StringBuilder formatted = new StringBuilder(2 * hash.Length); foreach (byte b in hash) { formatted.AppendFormat(“{0:X2}”, b); } } } formatted contains the string representation of the SHA-1 hash. Also, by using … Read more

How to generate an MD5 checksum for a file in Android?

This code is from the CMupdater, from the CyanogenMod 10.2 android ROM. It tests the downloaded ROMs into the updater App. code: https://github.com/CyanogenMod/android_packages_apps_CMUpdater/blob/cm-10.2/src/com/cyanogenmod/updater/utils/MD5.java It works like a charm: /* * Copyright (C) 2012 The CyanogenMod Project * * * Licensed under the GNU GPLv2 license * * The text of the license can be found … Read more

How does Git compute file hashes?

Git prefixes the object with “blob “, followed by the length (as a human-readable integer), followed by a NUL character $ echo -e ‘blob 14\0Hello, World!’ | shasum 8ab686eafeb1f44702738c8b0f24f2567c36da6d Source: http://alblue.bandlem.com/2011/08/git-tip-of-week-objects.html

How to perform checksums during a SFTP file transfer for data integrity?

With the SFTP, running over an encrypted SSH session, there’s negligible chance the file contents could get corrupted while transferring. The SSH itself does data integrity verification. So unless the contents gets corrupted, when reading the local file or writing the remote file, you can be pretty sure that the file was uploaded correctly, if … Read more

Generating an MD5 checksum of a file

You can use hashlib.md5() Note that sometimes you won’t be able to fit the whole file in memory. In that case, you’ll have to read chunks of 4096 bytes sequentially and feed them to the md5 method: import hashlib def md5(fname): hash_md5 = hashlib.md5() with open(fname, “rb”) as f: for chunk in iter(lambda: f.read(4096), b””): … Read more

Getting a File’s MD5 Checksum in Java

There’s an input stream decorator, java.security.DigestInputStream, so that you can compute the digest while using the input stream as you normally would, instead of having to make an extra pass over the data. MessageDigest md = MessageDigest.getInstance(“MD5”); try (InputStream is = Files.newInputStream(Paths.get(“file.txt”)); DigestInputStream dis = new DigestInputStream(is, md)) { /* Read decorated stream (dis) to … Read more