कार्यान्वयन कि
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 कोर
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();
}
स्रोत
2013-06-08 15:57:18
http://msdn.microsoft.com/en-us/library/system.security.cryptography.sha256.aspx। -1। –
हैश बाइट्स पर काम करते हैं, स्ट्रिंग नहीं। तो आपको सबसे पहले एक एन्कोडिंग चुनने की आवश्यकता है जो स्ट्रिंग को बाइट्स में बदल देता है। मैं इसके लिए यूटीएफ -8 की सलाह देता हूं। – CodesInChaos
कुछ मुझे बताता है कि इस पोस्ट को पढ़ने वाले लोगों को यह भी जांचना चाहिए: https://stackoverflow.com/questions/4948322/fundamental-difference-between-hashing-and-encryption-algorithms – Ziezi