C#计算字符串哈希值(MD5、SHA)的方法小结
程序员文章站
2023-12-01 22:48:16
本文实例讲述了c#计算字符串哈希值(md5、sha)的方法。分享给大家供大家参考。具体如下:
一、关于本文
本文中是一个类库,包括下面几个函数:
① 计算32位md5...
本文实例讲述了c#计算字符串哈希值(md5、sha)的方法。分享给大家供大家参考。具体如下:
一、关于本文
本文中是一个类库,包括下面几个函数:
① 计算32位md5码(大小写):hash_md5_32
② 计算16位md5码(大小写):hash_md5_16
③ 计算32位2重md5码(大小写):hash_2_md5_32
④ 计算16位2重md5码(大小写):hash_2_md5_16
⑤ 计算sha-1码(大小写):hash_sha_1
⑥ 计算sha-256码(大小写):hash_sha_256
⑦ 计算sha-384码(大小写):hash_sha_384
⑧ 计算sha-512码(大小写):hash_sha_512
编译后被打包成文件hashtools.dll,其他程序可以在添加引用后对这些函数进行调用
二、类库中各函数代码
1. 类库结构
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace hashtools { public class hashhelper { //各个函数 } }
2. 计算32位md5码(大小写):hash_md5_32
/// <summary> /// 计算32位md5码 /// </summary> /// <param name="word">字符串</param> /// <param name="toupper">返回哈希值格式 true:英文大写,false:英文小写</param> /// <returns></returns> public static string hash_md5_32(string word, bool toupper = true) { try { system.security.cryptography.md5cryptoserviceprovider md5csp = new system.security.cryptography.md5cryptoserviceprovider(); byte[] bytvalue = system.text.encoding.utf8.getbytes(word); byte[] bythash = md5csp.computehash(bytvalue); md5csp.clear(); //根据计算得到的hash码翻译为md5码 string shash = "", stemp = ""; for (int counter = 0; counter < bythash.count(); counter++) { long i = bythash[counter] / 16; if (i > 9) { stemp = ((char)(i - 10 + 0x41)).tostring(); } else { stemp = ((char)(i + 0x30)).tostring(); } i = bythash[counter] % 16; if (i > 9) { stemp += ((char)(i - 10 + 0x41)).tostring(); } else { stemp += ((char)(i + 0x30)).tostring(); } shash += stemp; } //根据大小写规则决定返回的字符串 return toupper ? shash : shash.tolower(); } catch (exception ex) { throw new exception(ex.message); } }
3. 计算16位md5码(大小写):hash_md5_16
/// <summary> /// 计算16位md5码 /// </summary> /// <param name="word">字符串</param> /// <param name="toupper">返回哈希值格式 true:英文大写,false:英文小写</param> /// <returns></returns> public static string hash_md5_16(string word, bool toupper = true) { try { string shash = hash_md5_32(word).substring(8, 16); return toupper ? shash : shash.tolower(); } catch (exception ex) { throw new exception(ex.message); } }
4. 计算32位2重md5码(大小写):hash_2_md5_32
/// <summary> /// 计算32位2重md5码 /// </summary> /// <param name="word">字符串</param> /// <param name="toupper">返回哈希值格式 true:英文大写,false:英文小写</param> /// <returns></returns> public static string hash_2_md5_32(string word, bool toupper = true) { try { system.security.cryptography.md5cryptoserviceprovider md5csp = new system.security.cryptography.md5cryptoserviceprovider(); byte[] bytvalue = system.text.encoding.utf8.getbytes(word); byte[] bythash = md5csp.computehash(bytvalue); //根据计算得到的hash码翻译为md5码 string shash = "", stemp = ""; for (int counter = 0; counter < bythash.count(); counter++) { long i = bythash[counter] / 16; if (i > 9) { stemp = ((char)(i - 10 + 0x41)).tostring(); } else { stemp = ((char)(i + 0x30)).tostring(); } i = bythash[counter] % 16; if (i > 9) { stemp += ((char)(i - 10 + 0x41)).tostring(); } else { stemp += ((char)(i + 0x30)).tostring(); } shash += stemp; } bytvalue = system.text.encoding.utf8.getbytes(shash); bythash = md5csp.computehash(bytvalue); md5csp.clear(); shash = ""; //根据计算得到的hash码翻译为md5码 for (int counter = 0; counter < bythash.count(); counter++) { long i = bythash[counter] / 16; if (i > 9) { stemp = ((char)(i - 10 + 0x41)).tostring(); } else { stemp = ((char)(i + 0x30)).tostring(); } i = bythash[counter] % 16; if (i > 9) { stemp += ((char)(i - 10 + 0x41)).tostring(); } else { stemp += ((char)(i + 0x30)).tostring(); } shash += stemp; } //根据大小写规则决定返回的字符串 return toupper ? shash : shash.tolower(); } catch (exception ex) { throw new exception(ex.message); } }
5. 计算16位2重md5码(大小写):hash_2_md5_16
/// <summary> /// 计算16位2重md5码 /// </summary> /// <param name="word">字符串</param> /// <param name="toupper">返回哈希值格式 true:英文大写,false:英文小写</param> /// <returns></returns> public static string hash_2_md5_16(string word, bool toupper = true) { try { system.security.cryptography.md5cryptoserviceprovider md5csp = new system.security.cryptography.md5cryptoserviceprovider(); byte[] bytvalue = system.text.encoding.utf8.getbytes(word); byte[] bythash = md5csp.computehash(bytvalue); //根据计算得到的hash码翻译为md5码 string shash = "", stemp = ""; for (int counter = 0; counter < bythash.count(); counter++) { long i = bythash[counter] / 16; if (i > 9) { stemp = ((char)(i - 10 + 0x41)).tostring(); } else { stemp = ((char)(i + 0x30)).tostring(); } i = bythash[counter] % 16; if (i > 9) { stemp += ((char)(i - 10 + 0x41)).tostring(); } else { stemp += ((char)(i + 0x30)).tostring(); } shash += stemp; } shash = shash.substring(8, 16); bytvalue = system.text.encoding.utf8.getbytes(shash); bythash = md5csp.computehash(bytvalue); md5csp.clear(); shash = ""; //根据计算得到的hash码翻译为md5码 for (int counter = 0; counter < bythash.count(); counter++) { long i = bythash[counter] / 16; if (i > 9) { stemp = ((char)(i - 10 + 0x41)).tostring(); } else { stemp = ((char)(i + 0x30)).tostring(); } i = bythash[counter] % 16; if (i > 9) { stemp += ((char)(i - 10 + 0x41)).tostring(); } else { stemp += ((char)(i + 0x30)).tostring(); } shash += stemp; } shash = shash.substring(8, 16); //根据大小写规则决定返回的字符串 return toupper ? shash : shash.tolower(); } catch (exception ex) { throw new exception(ex.message); } }
6. 计算sha-1码(大小写):hash_sha_1
/// <summary> /// 计算sha-1码 /// </summary> /// <param name="word">字符串</param> /// <param name="toupper">返回哈希值格式 true:英文大写,false:英文小写</param> /// <returns></returns> public static string hash_sha_1(string word, bool toupper = true) { try { system.security.cryptography.sha1cryptoserviceprovider sha1csp = new system.security.cryptography.sha1cryptoserviceprovider(); byte[] bytvalue = system.text.encoding.utf8.getbytes(word); byte[] bythash = sha1csp.computehash(bytvalue); sha1csp.clear(); //根据计算得到的hash码翻译为sha-1码 string shash = "", stemp = ""; for (int counter = 0; counter < bythash.count(); counter++) { long i = bythash[counter] / 16; if (i > 9) { stemp = ((char)(i - 10 + 0x41)).tostring(); } else { stemp = ((char)(i + 0x30)).tostring(); } i = bythash[counter] % 16; if (i > 9) { stemp += ((char)(i - 10 + 0x41)).tostring(); } else { stemp += ((char)(i + 0x30)).tostring(); } shash += stemp; } //根据大小写规则决定返回的字符串 return toupper ? shash : shash.tolower(); } catch (exception ex) { throw new exception(ex.message); } }
7. 计算sha-256码(大小写):hash_sha_256
/// <summary> /// 计算sha-256码 /// </summary> /// <param name="word">字符串</param> /// <param name="toupper">返回哈希值格式 true:英文大写,false:英文小写</param> /// <returns></returns> public static string hash_sha_256(string word, bool toupper = true) { try { system.security.cryptography.sha256cryptoserviceprovider sha256csp = new system.security.cryptography.sha256cryptoserviceprovider(); byte[] bytvalue = system.text.encoding.utf8.getbytes(word); byte[] bythash = sha256csp.computehash(bytvalue); sha256csp.clear(); //根据计算得到的hash码翻译为sha-1码 string shash = "", stemp = ""; for (int counter = 0; counter < bythash.count(); counter++) { long i = bythash[counter] / 16; if (i > 9) { stemp = ((char)(i - 10 + 0x41)).tostring(); } else { stemp = ((char)(i + 0x30)).tostring(); } i = bythash[counter] % 16; if (i > 9) { stemp += ((char)(i - 10 + 0x41)).tostring(); } else { stemp += ((char)(i + 0x30)).tostring(); } shash += stemp; } //根据大小写规则决定返回的字符串 return toupper ? shash : shash.tolower(); } catch (exception ex) { throw new exception(ex.message); } }
8. 计算sha-384码(大小写):hash_sha_384
/// <summary> /// 计算sha-384码 /// </summary> /// <param name="word">字符串</param> /// <param name="toupper">返回哈希值格式 true:英文大写,false:英文小写</param> /// <returns></returns> public static string hash_sha_384(string word, bool toupper = true) { try { system.security.cryptography.sha384cryptoserviceprovider sha384csp = new system.security.cryptography.sha384cryptoserviceprovider(); byte[] bytvalue = system.text.encoding.utf8.getbytes(word); byte[] bythash = sha384csp.computehash(bytvalue); sha384csp.clear(); //根据计算得到的hash码翻译为sha-1码 string shash = "", stemp = ""; for (int counter = 0; counter < bythash.count(); counter++) { long i = bythash[counter] / 16; if (i > 9) { stemp = ((char)(i - 10 + 0x41)).tostring(); } else { stemp = ((char)(i + 0x30)).tostring(); } i = bythash[counter] % 16; if (i > 9) { stemp += ((char)(i - 10 + 0x41)).tostring(); } else { stemp += ((char)(i + 0x30)).tostring(); } shash += stemp; } //根据大小写规则决定返回的字符串 return toupper ? shash : shash.tolower(); } catch (exception ex) { throw new exception(ex.message); } }
9. 计算sha-512码(大小写):hash_sha_512
/// <summary> /// 计算sha-512码 /// </summary> /// <param name="word">字符串</param> /// <param name="toupper">返回哈希值格式 true:英文大写,false:英文小写</param> /// <returns></returns> public static string hash_sha_512(string word, bool toupper = true) { try { system.security.cryptography.sha512cryptoserviceprovider sha512csp = new system.security.cryptography.sha512cryptoserviceprovider(); byte[] bytvalue = system.text.encoding.utf8.getbytes(word); byte[] bythash = sha512csp.computehash(bytvalue); sha512csp.clear(); //根据计算得到的hash码翻译为sha-1码 string shash = "", stemp = ""; for (int counter = 0; counter < bythash.count(); counter++) { long i = bythash[counter] / 16; if (i > 9) { stemp = ((char)(i - 10 + 0x41)).tostring(); } else { stemp = ((char)(i + 0x30)).tostring(); } i = bythash[counter] % 16; if (i > 9) { stemp += ((char)(i - 10 + 0x41)).tostring(); } else { stemp += ((char)(i + 0x30)).tostring(); } shash += stemp; } //根据大小写规则决定返回的字符串 return toupper ? shash : shash.tolower(); } catch (exception ex) { throw new exception(ex.message); } }
三、函数调用
建立项目computehash,添加对hashtools.dll库的引用。并添加代码:
复制代码 代码如下:
using hashtools;
然后在main函数中添加下列代码:
static void main(string[] args) { console.writeline("md5 of \"abc\""); console.writeline("md5_32(upper): {0}", hashhelper.hash_md5_32("abc")); console.writeline("md5_32(lower): {0}", hashhelper.hash_md5_32("abc", false)); console.writeline("md5_16(upper): {0}", hashhelper.hash_md5_16("abc")); console.writeline("md5_16(lower): {0}", hashhelper.hash_md5_16("abc", false)); console.writeline("2_md5_32(upper): {0}", hashhelper.hash_2_md5_32("abc")); console.writeline("2_md5_32(lower): {0}", hashhelper.hash_2_md5_32("abc", false)); console.writeline("2_md5_32(upper): {0}", hashhelper.hash_2_md5_16("abc")); console.writeline("2_md5_32(lower): {0}", hashhelper.hash_2_md5_16("abc", false)); console.writeline("sha of \"abc\""); console.writeline("sha-1(upper): {0}", hashhelper.hash_sha_1("abc")); console.writeline("sha-1(lower): {0}", hashhelper.hash_sha_1("abc", false)); console.writeline("sha-256(upper): {0}", hashhelper.hash_sha_256("abc")); console.writeline("sha-256(lower): {0}", hashhelper.hash_sha_256("abc", false)); console.writeline("sha-384(upper): {0}", hashhelper.hash_sha_384("abc")); console.writeline("sha-384(lower): {0}", hashhelper.hash_sha_384("abc", false)); console.writeline("sha-512(upper): {0}", hashhelper.hash_sha_512("abc")); console.writeline("sha-512(lower): {0}", hashhelper.hash_sha_512("abc", false)); console.readline(); }
运行结果如下:
希望本文所述对大家的c#程序设计有所帮助。