How can I compute a SHA-2 (ideally SHA 256 or SHA 512) hash in iOS?

This is what I’m using for SHA1: #import <CommonCrypto/CommonDigest.h> + (NSData *)sha1:(NSData *)data { unsigned char hash[CC_SHA1_DIGEST_LENGTH]; if ( CC_SHA1([data bytes], [data length], hash) ) { NSData *sha1 = [NSData dataWithBytes:hash length:CC_SHA1_DIGEST_LENGTH]; return sha1; } return nil; } Replace CC_SHA1 with CC_SHA256 (or whichever you need), as well as CC_SHA1_DIGEST_LENGTH with CC_SHA256_DIGEST_LENGTH.

How to use OpenSSL’s SHA256 functions

You make a very common beginners mistake… Putting the libraries you link with in the wrong place on the command line when you build. Dependencies are reversed on the command line, so something that depends on something else should actually be put before what it depends on on the command line. In your example, you … Read more

Generate sha256 with OpenSSL and C++

Here’s how I did it: void sha256_hash_string (unsigned char hash[SHA256_DIGEST_LENGTH], char outputBuffer[65]) { int i = 0; for(i = 0; i < SHA256_DIGEST_LENGTH; i++) { sprintf(outputBuffer + (i * 2), “%02x”, hash[i]); } outputBuffer[64] = 0; } void sha256_string(char *string, char outputBuffer[65]) { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; SHA256_Init(&sha256); SHA256_Update(&sha256, string, strlen(string)); SHA256_Final(hash, &sha256); int … Read more

Hash String via SHA-256 in Java

To hash a string, use the built-in MessageDigest class: import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.nio.charset.StandardCharsets; import java.math.BigInteger; public class CryptoHash { public static void main(String[] args) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance(“SHA-256”); String text = “Text to hash, cryptographically.”; // Change this to UTF-16 if needed md.update(text.getBytes(StandardCharsets.UTF_8)); byte[] digest = md.digest(); String hex = … Read more

Hashing passwords with MD5 or sha-256 C#

Don’t use a simple hash, or even a salted hash. Use some sort of key-strengthening technique like bcrypt (with a .NET implementation here) or PBKDF2 (with a built-in implementation). Here’s an example using PBKDF2. To generate a key from your password… string password = GetPasswordFromUserInput(); // specify that we want to randomly generate a 20-byte … Read more

Why are the RSA-SHA256 signatures I generate with OpenSSL and Java different?

openssl dgst -sha256 < data.txt produces something like: (stdin)= b39eaeb437e33087132f01c2abc60c6a16904ee3771cd7b0d622d01061b40729 notice the (stdin)=‘? you don’t want that to be part of your hash, if you need to create a digest, use the -binary option. try using this to sign your data: openssl sha -sha256 -sign private.pem < data.txt This does everything you need. edit – … Read more

SHA256 in swift

You have to convert explicitly between Int and CC_LONG, because Swift does not do implicit conversions, as in (Objective-)C. You also have to define hash as an array of the required size. func sha256(data : NSData) -> NSData { var hash = [UInt8](count: Int(CC_SHA256_DIGEST_LENGTH), repeatedValue: 0) CC_SHA256(data.bytes, CC_LONG(data.length), &hash) let res = NSData(bytes: hash, length: … Read more

Are there any SHA-256 javascript implementations that are generally considered trustworthy? [closed]

OUTDATED: Many modern browsers now have first-class support for crypto operations. See Vitaly Zdanevich’s answer below. The Stanford JS Crypto Library contains an implementation of SHA-256. While crypto in JS isn’t really as well-vetted an endeavor as other implementation platforms, this one is at least partially developed by, and to a certain extent sponsored by, … Read more