How to hash NSString with SHA1 in Swift?

Your Objective-C code (using a NSString category) can be directly translated to Swift (using a String extension). First you have to create a “bridging header” and add #import <CommonCrypto/CommonCrypto.h> Then: extension String { func sha1() -> String { let data = self.dataUsingEncoding(NSUTF8StringEncoding)! var digest = [UInt8](count:Int(CC_SHA1_DIGEST_LENGTH), repeatedValue: 0) CC_SHA1(data.bytes, CC_LONG(data.length), &digest) let output = NSMutableString(capacity: … Read more

Implementation HMAC-SHA1 in python

Pseudocodish: def sign_request(): from hashlib import sha1 import hmac # key = b”CONSUMER_SECRET&” #If you dont have a token yet key = b”CONSUMER_SECRET&TOKEN_SECRET” # The Base String as specified here: raw = b”BASE_STRING” # as specified by OAuth hashed = hmac.new(key, raw, sha1) # The signature return hashed.digest().encode(“base64”).rstrip(‘\n’) Signature errors usually reside in the base-string, … Read more

Hashing a file in Python

TL;DR use buffers to not use tons of memory. We get to the crux of your problem, I believe, when we consider the memory implications of working with very large files. We don’t want this bad boy to churn through 2 gigs of ram for a 2 gigabyte file so, as pasztorpisti points out, we … Read more

Objective-C sample code for HMAC-SHA1 [closed]

Here’s how you generate an HMAC using SHA-256: NSString *key; NSString *data; const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding]; const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding]; unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH]; CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC); NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)]; NSString *hash = [HMAC base64Encoding]; I’m not aware of an HOTP library, but the algorithm … Read more

How to assign a Git SHA1’s to a file without Git?

This is how Git calculates the SHA1 for a file (or, in Git terms, a “blob”): sha1(“blob ” + filesize + “\0″ + data) So you can easily compute it yourself without having Git installed. Note that “\0” is the NULL-byte, not a two-character string. For example, the hash of an empty file: sha1(“blob 0\0”) … Read more

Is it possible to reverse a SHA-1?

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

Java String to SHA1

UPDATE You can use Apache Commons Codec (version 1.7+) to do this job for you. DigestUtils.sha1Hex(stringToConvertToSHexRepresentation) Thanks to @Jon Onstott for this suggestion. Old Answer Convert your Byte Array to Hex String. Real’s How To tells you how. return byteArrayToHexString(md.digest(convertme)) and (copied from Real’s How To) public static String byteArrayToHexString(byte[] b) { String result = … Read more

Probability of SHA1 collisions

Are the 160 bit hash values generated by SHA-1 large enough to ensure the fingerprint of every block is unique? Assuming random hash values with a uniform distribution, a collection of n different data blocks and a hash function that generates b bits, the probability p that there will be one or more collisions is … Read more