c# - How to generate an md5 hash with a plaintext string and a known salt -
this question has answer here:
i have been going crazy trying figure out. there simple way in c# take string "password123" , salt "vfs5%s]m(_*y+tk" , generate single md5 hash. website http://free-online-web-tools.com/tool/md5 c#.
the below given function generates hash given plain text value , returns base64-encoded result. before hash computed, random salt generated , appended plain text. salt stored @ end of hash value, can used later hash verification.
plaintext value hashed. function not check whether parameter null.
name of hash algorithm. allowed values are: "md5", "sha1","sha256", "sha384", , "sha512" (if other value specified md5 hashing algorithm used). value case-insensitive.
in case set hashalgorithm "md5"
salt bytes. parameter can null, in case random salt value generated.
hash value formatted base64-encoded string.
public static string computehash(string plaintext, string hashalgorithm, byte[] saltbytes) { // if salt not specified, generate on fly. if (saltbytes == null) { // define min , max salt sizes. int minsaltsize = 4; int maxsaltsize = 8; // generate random number size of salt. random random = new random(); int saltsize = random.next(minsaltsize, maxsaltsize); // allocate byte array, hold salt. saltbytes = new byte[saltsize]; // initialize random number generator. rngcryptoserviceprovider rng = new rngcryptoserviceprovider(); // fill salt cryptographically strong byte values. rng.getnonzerobytes(saltbytes); } // convert plain text byte array. byte[] plaintextbytes = encoding.utf8.getbytes(plaintext); // allocate array, hold plain text , salt. byte[] plaintextwithsaltbytes = new byte[plaintextbytes.length + saltbytes.length]; // copy plain text bytes resulting array. (int i=0; < plaintextbytes.length; i++) plaintextwithsaltbytes[i] = plaintextbytes[i]; // append salt bytes resulting array. (int i=0; < saltbytes.length; i++) plaintextwithsaltbytes[plaintextbytes.length + i] = saltbytes[i]; // because support multiple hashing algorithms, must define // hash object common (abstract) base class. specify // actual hashing algorithm class later during object creation. hashalgorithm hash; // make sure hashing algorithm name specified. if (hashalgorithm == null) hashalgorithm = ""; // initialize appropriate hashing algorithm class. switch (hashalgorithm.toupper()) { case "sha1": hash = new sha1managed(); break; case "sha256": hash = new sha256managed(); break; case "sha384": hash = new sha384managed(); break; case "sha512": hash = new sha512managed(); break; default: hash = new md5cryptoserviceprovider(); break; } // compute hash value of our plain text appended salt. byte[] hashbytes = hash.computehash(plaintextwithsaltbytes); // create array hold hash , original salt bytes. byte[] hashwithsaltbytes = new byte[hashbytes.length + saltbytes.length]; // copy hash bytes resulting array. (int i=0; < hashbytes.length; i++) hashwithsaltbytes[i] = hashbytes[i]; // append salt bytes result. (int i=0; < saltbytes.length; i++) hashwithsaltbytes[hashbytes.length + i] = saltbytes[i]; // convert result base64-encoded string. string hashvalue = convert.tobase64string(hashwithsaltbytes); // return result. return hashvalue; } for more details refer here.
Comments
Post a Comment