Created
June 7, 2012 15:37
-
-
Save pcperini/2889493 to your computer and use it in GitHub Desktop.
Hashing Functions in Multiple Languages / Environments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// # Assume salt, password, and encodedPassword are all strings. | |
// # If you don't have a salt, just have a random number generator spit out > 16B of data, and turn that into a string. | |
// Java - SHA256 with Salt | |
import java.security.MessageDigest; | |
import sun.misc.BASE64Encoder; | |
MessageDigest digest = MessageDigest.getInstance("SHA-256"); | |
digest.reset(); | |
byte[] byteBuffer = digest.digest((password + salt).getBytes("UTF-8")); | |
for (int i = 0; i < ITERATION_COUNT; i++) | |
{ | |
digest.reset(); | |
byteBuffer = digest.digest(byteBuffer); | |
} | |
BASE64Encoder coder = new BASE64Encoder(); | |
encodedPassword = coder.encode(byteBuffer); | |
// Javascript - SHA256 with Salt | |
<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha256.js"></script> | |
<script> | |
encodedPassword = CryptoJS.HmacSHA256(password + salt).toString(); | |
</script> | |
# PHP - SHA256 with Salt | |
encodedPassword = hash("sha256", password.salt); | |
# Python - SHA256 with Salt | |
import hashlib | |
encodedPassword = hashlib.sha256(password + salt).hexdigest() | |
// Objective-C SHA256 with Salt | |
#include <CommonCrypto/CommonDigest.h> | |
unsigned char hashedCharacters[CC_SHA256_DIGEST_LENGTH]; | |
NSMutableData *passwordData = [[password dataUsingEncoding: NSUTF8StringEncoding] mutableCopy]; | |
[passwordData appendData: [salt dataUsingEncoding: NSUTF8StringEncoding]]; | |
CC_SHA256([passwordData bytes], [passwordData length], hashedCharacters); | |
encodedPassword = [NSString stringWithUTF8String: hashedCharacters]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
I used this example in Python and Objective-c but in Objective-c this returned me "null". I copied exactly your example with password "1234" and salt "salt".
Thanks!