facebook twitter hatena line email

「Unity/Csharp/md5」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「==サンプルコード== // KeyUtility.md5("hoge"); public class KeyUtility { public static string md5( string srcStr ) { System.Security.Cryptography.MD5 md5 = Sys...」)
 
 
行2: 行2:
 
  // KeyUtility.md5("hoge");
 
  // KeyUtility.md5("hoge");
 
  public class KeyUtility {
 
  public class KeyUtility {
public static string md5( string srcStr ) {
+
public static string md5( string srcStr ) {
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
+
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] srcBytes = System.Text.Encoding.UTF8.GetBytes(srcStr);
+
byte[] srcBytes = System.Text.Encoding.UTF8.GetBytes(srcStr);
byte[] destBytes = md5.ComputeHash(srcBytes);
+
byte[] destBytes = md5.ComputeHash(srcBytes);
System.Text.StringBuilder destStrBuilder;
+
System.Text.StringBuilder destStrBuilder;
destStrBuilder = new System.Text.StringBuilder();
+
destStrBuilder = new System.Text.StringBuilder();
foreach (byte curByte in destBytes) {
+
foreach (byte curByte in destBytes) {
destStrBuilder.Append(curByte.ToString("x2"));
+
destStrBuilder.Append(curByte.ToString("x2"));
}
+
}
return destStrBuilder.ToString();
+
return destStrBuilder.ToString();
}
+
}
 
  }
 
  }
  
 
==参考==
 
==参考==
 
http://nanoappli.com/blog/archives/6347
 
http://nanoappli.com/blog/archives/6347

2018年4月29日 (日) 07:01時点における最新版

サンプルコード

// KeyUtility.md5("hoge");
public class KeyUtility {
	public static string md5( string srcStr ) {
		System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
		byte[] srcBytes = System.Text.Encoding.UTF8.GetBytes(srcStr);
		byte[] destBytes = md5.ComputeHash(srcBytes);
		System.Text.StringBuilder destStrBuilder;
		destStrBuilder = new System.Text.StringBuilder();
		foreach (byte curByte in destBytes) {
			destStrBuilder.Append(curByte.ToString("x2"));
		}
		return destStrBuilder.ToString();
	}
}

参考

http://nanoappli.com/blog/archives/6347