Compute SHA-1 of byte array

What about: import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Formatter; public static String SHAsum(byte[] convertme) throws NoSuchAlgorithmException{ MessageDigest md = MessageDigest.getInstance(“SHA-1”); return byteArray2Hex(md.digest(convertme)); } private static String byteArray2Hex(final byte[] hash) { Formatter formatter = new Formatter(); for (byte b : hash) { formatter.format(“%02x”, b); } return formatter.toString(); }

Objective C: SHA1

CommonCrypto (an Apple framework) has functions for calculating SHA-1 hashes, including a one-step hash: #include <CommonCrypto/CommonDigest.h> unsigned char digest[CC_SHA1_DIGEST_LENGTH]; NSData *stringBytes = [someString dataUsingEncoding: NSUTF8StringEncoding]; /* or some other encoding */ if (CC_SHA1([stringBytes bytes], [stringBytes length], digest)) { /* SHA-1 hash has been calculated and stored in ‘digest’. */ … } For a set of … Read more

How do you verify an RSA SHA1 signature in Python?

Use M2Crypto. Here’s how to verify for RSA and any other algorithm supported by OpenSSL: pem = “””—–BEGIN PUBLIC KEY—– MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDfG4IuFO2h/LdDNmonwGNw5srW nUEWzoBrPRF1NM8LqpOMD45FAPtZ1NmPtHGo0BAS1UsyJEGXx0NPJ8Gw1z+huLrl XnAVX5B4ec6cJfKKmpL/l94WhP2v8F3OGWrnaEX1mLMoxe124Pcfamt0SPCGkeal VvXw13PLINE/YptjkQIDAQAB —–END PUBLIC KEY—–“”” # your example key from M2Crypto import BIO, RSA, EVP bio = BIO.MemoryBuffer(pem) rsa = RSA.load_pub_key_bio(bio) pubkey = EVP.PKey() pubkey.assign_rsa(rsa) # if you need a different digest than … Read more

Is it possible to reverse a SHA-1 hash?

No, you cannot reverse SHA-1, that is exactly why it is called a Secure Hash Algorithm. What you should definitely be doing though, is include the message that is being transmitted into the hash calculation. Otherwise a man-in-the-middle could intercept the message, and use the signature (which only contains the sender’s key and the timestamp) … Read more

How can I pass git SHA1 to compiler as definition using cmake?

I’ve made some CMake modules that peer into a git repo for versioning and similar purposes – they’re all in my repository at https://github.com/rpavlik/cmake-modules The good thing about these functions is, they will force a re-configure (a rerun of cmake) before a build every time the HEAD commit changes. Unlike doing something just once with … Read more

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

tech