获取字符串的SHA-256字符串
我有一些string
,我想使用C#使用SHA-256哈希函数对其进行哈希处理。 我想要这样的东西:
string hashString = sha256_hash("samplestring");
框架中是否有内置function可以做到这一点?
实施可能就是这样
public static String sha256_hash(String value) { StringBuilder Sb = new StringBuilder(); using (SHA256 hash = SHA256Managed.Create()) { Encoding enc = Encoding.UTF8; Byte[] result = hash.ComputeHash(enc.GetBytes(value)); foreach (Byte b in result) Sb.Append(b.ToString("x2")); } return Sb.ToString(); }
编辑: Linq实现更简洁 ,但可能不太可读 :
public static String sha256_hash(String value) { using (SHA256 hash = SHA256Managed.Create()) { return String.Concat(hash .ComputeHash(Encoding.UTF8.GetBytes(value)) .Select(item => item.ToString("x2"))); } }
编辑2: .NET Core
public static String sha256_hash(string value) { StringBuilder Sb = new StringBuilder(); using (var hash = SHA256.Create()) { Encoding enc = Encoding.UTF8; Byte[] result = hash.ComputeHash(enc.GetBytes(value)); foreach (Byte b in result) Sb.Append(b.ToString("x2")); } return Sb.ToString(); }
我正在寻找一个在线解决方案,并且能够从Dmitry的答案中编译下面的内容:
public static String sha256_hash(string value) { return (System.Security.Cryptography.SHA256.Create() .ComputeHash(Encoding.UTF8.GetBytes(value)) .Select(item => item.ToString("x2"))); }
- 我应该如何使用velX,vely信息来获得当前帧和前一帧之间的X和Y位移?
- 目标框架更改4.0到3.5打破EF模型。 错误111:参照约束错误
- GDI + System.Drawing.Bitmap给出错误参数间歇性无效
- List 没有实现SyncRoot!
- Selenium:如何通过部分href找到元素?
- 自定义属性与非自定义属性?
- 实现IXmlSerializable时,如何只覆盖ReadXml或WriteXml而不是两者?
- 如何正确读取/解释原始C#堆栈跟踪?
- 2015年安装Visual Studio Installer项目后,VS 2015中的Visual Studio 2012安装程序项目“未安装应用程序”