Calculate a MD5 hash from a string

As per MSDN Create MD5: public static string CreateMD5(string input) { // Use input string to calculate MD5 hash using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create()) { byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input); byte[] hashBytes = md5.ComputeHash(inputBytes); return Convert.ToHexString(hashBytes); // .NET 5 + // Convert the byte array to hexadecimal string prior to .NET 5 // StringBuilder sb = … Read more

Simple (non-secure) hash function for JavaScript? [duplicate]

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

How do I create an MD5 Hash of a string in Cocoa?

This is the category I use: NSString+MD5.h @interface NSString (MD5) – (NSString *)MD5String; @end NSString+MD5.m #import <CommonCrypto/CommonDigest.h> @implementation NSString (MD5) – (NSString *)MD5String { const char *cStr = [self UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5( cStr, (CC_LONG)strlen(cStr), result ); return [NSString stringWithFormat: @”%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X”, result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], … Read more

MD5 hash with salt for keeping password in DB in C#

You can use the HMACMD5 class: var hmacMD5 = new HMACMD5(salt); var saltedHash = hmacMD5.ComputeHash(password); Works with SHA-1, SHA256, SHA384, SHA512 and RIPEMD160 as well: var hmacSHA1 = new HMACSHA1(salt); var saltedHash = hmacSHA1.ComputeHash(password); Both salt and password are expected as byte arrays. If you have strings you’ll have to convert them to bytes first: … Read more

Possible to calculate MD5 (or other) hash with buffered reads?

You use the TransformBlock and TransformFinalBlock methods to process the data in chunks. // Init MD5 md5 = MD5.Create(); int offset = 0; // For each block: offset += md5.TransformBlock(block, 0, block.Length, block, 0); // For last block: md5.TransformFinalBlock(block, 0, block.Length); // Get the has code byte[] hash = md5.Hash; Note: It works (at least … Read more