How do I create a SHA1 hash in ruby?
require ‘digest/sha1’ Digest::SHA1.hexdigest ‘foo’
require ‘digest/sha1’ Digest::SHA1.hexdigest ‘foo’
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(); }
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
Using apache common codec library: DigestUtils.sha1Hex(“aff”) The result is 0c05aa56405c447e6678b7f3127febde5c3a9238 That’s it 🙂
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
It’s an old question but one thing needs some clarification: This question and the answers below talk about the Git hash of a file which is not exactly the same as “the SHA1 of this file” as asked in the question. In short: If you want to get the Git hash of the file in … Read more
Use ASCIIEncoding instead of UnicodeEncoding. PHP uses ASCII charset for hash calculations.
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
I didn’t verify this myself, but you can look at this JavaScript implementation of Java’s String.hashCode() method. Seems reasonably short. With this prototype you can simply call .hashCode() on any string, e.g. “some string”.hashCode(), and receive a numerical hash code (more specifically, a Java equivalent) such as 1395333309. String.prototype.hashCode = function() { var hash = … Read more
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